Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

object.x, object.y and require ()
Started by omg1024 Nov 10 2018 06:57 AM

5 replies to this topic
object.x object.y require
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

omg1024

[GLOBAL: userInfoPane.html]
omg1024
  • Observer

  • 3 posts
  • Corona SDK

Hello. I'm trying to build simple scene through modules. Working code looks like this:

		require ("bg1_static")
		require ("bg2_move")
		require ("bg3_move")

		require ("player_run")

		require ("bg4_move")

No errors allowed. But if i try to set position for "player_run" through function:

		require ("bg1_static")
		require ("bg2_move")
		require ("bg3_move")

		local player = require ("player_run")
		player.x = 100
		player.y = 100
		
		require ("bg4_move")

This leads to an error in the attach. I'm totally noob in Corona SDK, Lua, and programming as is, so i need to understand what kind of error this is - lack of Lua knowledge (minimal language basis for example), Corona SDK functions or both? Can someone explaind what exactelly wrong and give advice to learn direction which related with module builds? Thanks.

 

Attached Files

  • Attached File  123.png   108.04KB   0 downloads


[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,860 posts
  • Corona Staff

Can you share your player_run.lua file? I suspect you're not returning a table with the player object at the end. Make sure to use the blue <> button when sharing code.

 

Rob



[TOPIC: post.html]
#3

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,610 posts
  • Corona SDK

It is telling you that player is not a display object like you think it is, it is a boolean (true/false) value.

Therefore the player object is not a table and does not have an .x property or any other properties.

We can't tell you what's causing it without seeing player_run.lua

[TOPIC: post.html]
#4

omg1024

[GLOBAL: userInfoPane.html]
omg1024
  • Observer

  • 3 posts
  • Corona SDK

From this tutorial:
 
--Player run animation start
-----------------------------------------------------------------------------------------	
displayContentWidth = 1024
displayContentHeight = 768

local sheetData = {
		width=256,
		height=256,
		numFrames=4,
		sheetContentWidth=512,
		sheetContentHeight=512 }
		
local mySheet = graphics.newImageSheet ( "img/player/run/run_sheet.png", sheetData )

local sequenceData = {
		{ name = "normalRun", start=1, count=4, time=400 },
		{ name = "fastRun", frames={ 1, 2, 3, 4}, time=200 }
	}
	
	local animation = display.newSprite( mySheet, sequenceData )
	animation.x = displayContentWidth/2
	animation.y = displayContentHeight/2
	
	animation:setSequence( "normalRun" )
	--animation:setSequence( "fastRun" )
	animation:play()
-----------------------------------------------------------------------------------------	
--Player run animation end


[TOPIC: post.html]
#5

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,610 posts
  • Corona SDK

Ok, a Lua module must return something in order for it to be used by the scene that required it. Otherwise the code will just run and that's it - you've lost any references to the objects created. 

 

How does Corona know what 'player' is? Is it sheetData, mySheet, sequenceData, animation? 

 

This is a basic module:

 

 
local m = {}
 
m.myModuleVariable = 50
 
return m
 

 

Used as follows:

 

 
local theModule = require("myModule")
 
print (theModule.myModuleVariable)
theModule.myModuleVariable = 60
print (theModule.myModuleVariable)
 

 

So, in this case you have two choices. You can just return animation at the end of the player_run file if you don't care about accessing the rest of the stuff in there, or attach everything to m and return it (like I did with the myModuleVariable ), and then access your player sprite from that.



[TOPIC: post.html]
#6

omg1024

[GLOBAL: userInfoPane.html]
omg1024
  • Observer

  • 3 posts
  • Corona SDK

Thanks!




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: object.x, object.y, require