object.x, object.y and require ()

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.

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

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

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

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:

[lua]

local m = {}

m.myModuleVariable = 50

return m

[/lua]

Used as follows:

[lua]

local theModule = require(“myModule”)

print (theModule.myModuleVariable)

theModule.myModuleVariable = 60

print (theModule.myModuleVariable)

[/lua]

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.

Thanks!

And again i don’t understand what’s wrong. This code looks fine (because Corona shown not any errors):

--bg4 start ----------------------------------------------------------------------------------------- local imgW = display.contentWidth local imgH = display.contentHeight local scrollSpeed = -1.5 --bg4 move local bg4a = display.newImageRect("img/bg/bg4.png", 1024, 768) bg4a.x = imgW\*0.5; bg4a.y= imgH/2.4; local bg4b = display.newImageRect("img/bg/bg4.png", 1024, 768) bg4b.x = imgW\*1.5; bg4b.y= imgH/2.4; local bg4c = display.newImageRect("img/bg/bg4.png", 1024, 768) bg4c.x = imgW\*2.5; bg4c.y= imgH/2.4; local moveBool = true if moveBool == true then local function move( event ) -- move backgrounds to the left by scrollSpeed, default is 2 bg4a.x = bg4a.x + scrollSpeed bg4b.x = bg4b.x + scrollSpeed bg4c.x = bg4c.x + scrollSpeed -- Set up listeners so when backgrounds hits a certain point off the screen, -- move the background to the right off screen if bg4a.x \< -512 then bg4a:translate( 2048, 0 ) end if bg4b.x \< -512 then bg4b:translate( 2048, 0 ) end if bg4c.x\< -512 then bg4c:translate( 2048, 0 ) end end -- Create a runtime event to move backgrounds Runtime:addEventListener( "enterFrame", move ) end if moveBool == false then local function move( event ) -- move backgrounds to the left by scrollSpeed, default is 2 bg4a.x = bg4a.x + 0 bg4b.x = bg4b.x + 0 bg4c.x = bg4c.x + 0 end -- Create a runtime event to move backgrounds Runtime:addEventListener( "enterFrame", move ) end return moveBool ----------------------------------------------------------------------------------------- --bg4 end

Further i’m try to connect it into main.lua with false  state for  moveBool  (in bg4_move.lua above that’s  true initially):

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- display.setStatusBar(display.HiddenStatusBar) --disable phone ui local physics = require( "physics" ) physics.start() --connect modules start ----------------------------------------------------------------------------------------- require ("bg1\_static") require ("bg2\_move") require ("bg3\_move") local player = require ("player\_run") player.x = player.x + 512 player.y = player.y + 510 local bg4 = require ("bg4\_move") bg4.moveBool = false ----------------------------------------------------------------------------------------- --connect modules end --player controls start ----------------------------------------------------------------------------------------- --local function PlrFastRun() --player:setSequence( "fastRun" ) --player:play() --end --player:addEventListener( "tap", PlrFastRun ) ----------------------------------------------------------------------------------------- local function PlrStopRun() player:setSequence( "stopRun" ) player:play() end player:addEventListener( "tap", PlrStopRun ) ----------------------------------------------------------------------------------------- --player contros end

And get an error (pic in attach). What’s i do wrong now?

You are returning moveBool from bg4_move. Therefore bg4 in main is not an image, it is a bool. You then try to access what amounts to moveBool.moveBool, which is obviously nil.

You don’t see any errors in bg4_move because you’re not doing anything illegal. Corona says, 'hey, you want to create some images and then return a bool from this module? No problem."

Only when you try to do something with a bool that you think is an image does an error occur.

I change code, now it’s not image or still? By the idea bool declares earler than any images and value also returns to bool. Or not? Error still shows anyway.

--bg4 start ----------------------------------------------------------------------------------------- local imgW = display.contentWidth local imgH = display.contentHeight local scrollSpeed = -1.5 local moveBool = true if moveBool == true then scrollSpeed = -1.5 else scrollSpeed = 0 end --bg4 move local bg4a = display.newImageRect("img/bg/bg4.png", 1024, 768) bg4a.x = imgW\*0.5; bg4a.y= imgH/2.4; local bg4b = display.newImageRect("img/bg/bg4.png", 1024, 768) bg4b.x = imgW\*1.5; bg4b.y= imgH/2.4; local bg4c = display.newImageRect("img/bg/bg4.png", 1024, 768) bg4c.x = imgW\*2.5; bg4c.y= imgH/2.4; local function move( event ) -- move backgrounds to the left by scrollSpeed, default is 2 bg4a.x = bg4a.x + scrollSpeed bg4b.x = bg4b.x + scrollSpeed bg4c.x = bg4c.x + scrollSpeed -- Set up listeners so when backgrounds hits a certain point off the screen, -- move the background to the right off screen if bg4a.x \< -512 then bg4a:translate( 2048, 0 ) end if bg4b.x \< -512 then bg4b:translate( 2048, 0 ) end if bg4c.x\< -512 then bg4c:translate( 2048, 0 ) end end -- Create a runtime event to move backgrounds Runtime:addEventListener( "enterFrame", move ) return moveBool ----------------------------------------------------------------------------------------- --bg4 end

In main.lua you have this code:

[lua]

local bg4 = require (“bg4_move”)

[/lua]

bg4 will be equal to whatever you return from bg4_move, just with a different name. Therefore:

  • moveBool is a boolean set to true

  • bg4 is a boolean set to true

  • they are the same object with different names

Therefore, when you try to access bg4.moveBool, that does not exist, because moveBool.moveBool does not exist.

If you want to return everything from bg4_move.lua, and have access to it all, you need to do something like this:

[lua]

local m = {}

m.moveBool = true

m.bg4a = …

m.bg4b = …

m.bg4c = …

return m

[/lua]

Now, bg4.moveBool will exist, as will bg4.bg4a, bg4.bg4b and bg4.gb4c.

If you never need access to the images again, just leave them as local rather than attaching them to ‘m’.

Now i have no any errors, but also no any affects when i try to set false for bg4:

--main.lua local bg4 = require ("bg4\_move") bg4.moveBool = false

--bg4\_move.lua local bg4State = {} local scrollSpeed = -1.5 bg4State.moveBool = true if bg4State.moveBool == true then scrollSpeed = -1.5 else scrollSpeed = 0 end return bg4State

Probably something wrong in the conditions block? If that is, why i can set false in bg4_move.lua and this will work and not when i set it in the main.lua?

Is that all the code? The code that checks whether moveBool is true or false only runs once. You need to either put it in an enterFrame listener that checks every frame, or create a function that is called manually each time you update moveBool.

Thanks again!

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

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

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

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:

[lua]

local m = {}

m.myModuleVariable = 50

return m

[/lua]

Used as follows:

[lua]

local theModule = require(“myModule”)

print (theModule.myModuleVariable)

theModule.myModuleVariable = 60

print (theModule.myModuleVariable)

[/lua]

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.

Thanks!

And again i don’t understand what’s wrong. This code looks fine (because Corona shown not any errors):

--bg4 start ----------------------------------------------------------------------------------------- local imgW = display.contentWidth local imgH = display.contentHeight local scrollSpeed = -1.5 --bg4 move local bg4a = display.newImageRect("img/bg/bg4.png", 1024, 768) bg4a.x = imgW\*0.5; bg4a.y= imgH/2.4; local bg4b = display.newImageRect("img/bg/bg4.png", 1024, 768) bg4b.x = imgW\*1.5; bg4b.y= imgH/2.4; local bg4c = display.newImageRect("img/bg/bg4.png", 1024, 768) bg4c.x = imgW\*2.5; bg4c.y= imgH/2.4; local moveBool = true if moveBool == true then local function move( event ) -- move backgrounds to the left by scrollSpeed, default is 2 bg4a.x = bg4a.x + scrollSpeed bg4b.x = bg4b.x + scrollSpeed bg4c.x = bg4c.x + scrollSpeed -- Set up listeners so when backgrounds hits a certain point off the screen, -- move the background to the right off screen if bg4a.x \< -512 then bg4a:translate( 2048, 0 ) end if bg4b.x \< -512 then bg4b:translate( 2048, 0 ) end if bg4c.x\< -512 then bg4c:translate( 2048, 0 ) end end -- Create a runtime event to move backgrounds Runtime:addEventListener( "enterFrame", move ) end if moveBool == false then local function move( event ) -- move backgrounds to the left by scrollSpeed, default is 2 bg4a.x = bg4a.x + 0 bg4b.x = bg4b.x + 0 bg4c.x = bg4c.x + 0 end -- Create a runtime event to move backgrounds Runtime:addEventListener( "enterFrame", move ) end return moveBool ----------------------------------------------------------------------------------------- --bg4 end

Further i’m try to connect it into main.lua with false  state for  moveBool  (in bg4_move.lua above that’s  true initially):

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- display.setStatusBar(display.HiddenStatusBar) --disable phone ui local physics = require( "physics" ) physics.start() --connect modules start ----------------------------------------------------------------------------------------- require ("bg1\_static") require ("bg2\_move") require ("bg3\_move") local player = require ("player\_run") player.x = player.x + 512 player.y = player.y + 510 local bg4 = require ("bg4\_move") bg4.moveBool = false ----------------------------------------------------------------------------------------- --connect modules end --player controls start ----------------------------------------------------------------------------------------- --local function PlrFastRun() --player:setSequence( "fastRun" ) --player:play() --end --player:addEventListener( "tap", PlrFastRun ) ----------------------------------------------------------------------------------------- local function PlrStopRun() player:setSequence( "stopRun" ) player:play() end player:addEventListener( "tap", PlrStopRun ) ----------------------------------------------------------------------------------------- --player contros end

And get an error (pic in attach). What’s i do wrong now?