Control class that return a Display.Group object ?

wow sir you are fast… and precise.

So those object removed from worldGroup, that’s what happened.

So i do as you suggested, just put the whole worldGroup to scene view. It went well. Many thanks.

Gosh i did over complicate things… T-T

Maybe you could help me with that nil event too… :slight_smile:

Well if your earlier code is still the same, this is your problem:

--you name the object 'thisSky' local thisSky = world[1] --but try to move an object called 'sky' not 'thisSky' sky.x = sky.x - 10 -- this gave me nil

@wilychristian:

It also might be easier to put the move function inside of the world object:

[lua]

local world = {}

function world.new()

    local worldGroup = display.newGroup()

  

    local sky = display.newImageRect( “sky.png”, 480,360 )

    sky:setReferencePoint( display.TopLeftReferencePoint )

    sky.x = 0; sky.y = 0

    worldGroup:insert(sky)

  

    local ground = display.newImageRect( “ground.png”, 480,360 )

    ground:setReferencePoint( display.TopLeftReferencePoint )

    ground.x = 0; ground.y = 0

    worldGroup:insert(ground)

        function worldGroup.move()

            – Now when you want to move it, just use the reference you have to the worldGroup itself

        end

    return worldGroup

end

return world

[/lua]

  • C

@Alan

Ehm its quite different actually. :move now is waiting for touch action.

[lua]

function world:move(event)

   if (event.phase == “began”) then  – i believe this is where event = nil

   …

   end

end

[/lua]

Other object has been successfully passed in.

@Caleb

Thanks for your suggestion. I got question:

  1. Why is it easier? What are the differences with my version?

  2. In your code above, how can i call worldGroup.move() from main.lua?

  3. How do you implement it as touch event listener?

Lets say this is main.lua

[lua]

local World = require “world”

local thisWorld = World:new()

thisWorld:move()

– 2) will this call :move one time ?

somebutton:addEventListener(“touch”, World.move)

– 3) is this how you add the touch listener? It cant be this simple…

–     how did it pass the ‘event’ parameter?

[/lua]

I havent tested your code though… hehe sorry for bluntly asking.

  1. It’s easier because you don’t have to add an entry to your World class - it keeps things local, clean, and neat.

[lua]

local World = require(“world”)

local thisWorld = World.new()

thisWorld.move()

[/lua]

  1. Just put it in your touch function:

[lua]

local function onTouch(event)

  if “began” == event.phase then

    thisWorld.move()

  end

end

[/lua]

Or something to that effect. Check out the docs for events: http://docs.coronalabs.com/guide/events/detectEvents/index.html

  • C
  1. The diference between ours is you put your function :move inside function :new, while i don’t.

I thought mine was local already. Well thanks for your input Caleb.

  1. ok

  2. I have done that, but somehow the ‘event’ parameter pass nil value.

I use external lua file, and passing that ‘event’ from main.lua to anpther lua.

Oh well i’ll try some more and look on the forum again. Hopefully find something similar about this.

Thanks again Caleb.

  1. Adding the .move function inside of the .new means that your objects don’t have to be added to your library (World[1]) . Having multiple references to one object is something that can get very dangerous, very fast (memory wise). Example: you delete the thisWorld object but forget to delete the World[1] object - which are the same thing - creating a memory leak. When I said “local”, that was a slip up - I meant not somewhere in the World :D 

  2. Usually something not passing an event parameter is because of a collision between dots and colons. Make sure you’ve got all of your function declarations right:

[lua]

function world.move()

end

function world:touch(event)

  – touch function

end

world:addEventListener(“touch”)

[/lua]

Hello everybody

Please help me.

I tried to get the snippet of the solutions together with part in composer and I don’t get over this error:

 “\ scene1.lua:38: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’”.

This is all the code I put together from the snippets.

https://github.com/galahad9/AccessClassObjectInComposer

main.lua

local composer = require "composer" -- load first scene composer.gotoScene( "scene1", "fade", 400 )

world.lua (class/module)

local world = {} function world.new() local worldGroup = display.newGroup() local sky = display.newRect(350,50, 50,100 ) worldGroup:insert(sky) local ground = display.newRect(250,120, 30,36 ) worldGroup:insert(ground) world.worldGroup = worldGroup return worldGroup end function world:move() local thisSky = world.worldGroup[1] thisSky.x = thisSky.x - 10 -- this gave me nil end return world

scene1.lua (here is the error)

local composer = require( "composer" ) local scene = composer.newScene() local image, text1, text2, text3, memTimer local function onSceneTouch( self, event ) if event.phase == "began" then composer.gotoScene( "scene2", "slideLeft", 800 ) return true end end function scene:create( event ) local sceneGroup = self.view image = display.newImage( "bg.jpg" ) image.x = display.contentCenterX image.y = display.contentCenterY sceneGroup:insert( image ) image.touch = onSceneTouch text1 = display.newText( "Scene 1", 0, 0, native.systemFontBold, 24 ) text1:setFillColor( 255 ) text1.x, text1.y = display.contentWidth \* 0.5, 50 sceneGroup:insert( text1 ) text2 = display.newText( "MemUsage: ", 0, 0, native.systemFont, 16 ) text2:setFillColor( 255 ) text2.x, text2.y = display.contentWidth \* 0.5, display.contentHeight \* 0.5 sceneGroup:insert( text2 ) text3 = display.newText( "Touch to continue.", 0, 0, native.systemFontBold, 18 ) text3:setFillColor( 255 ); text3.isVisible = false text3.x, text3.y = display.contentWidth \* 0.5, display.contentHeight - 100 sceneGroup:insert( text3 ) -- ACCESSING MODULE world.lua local World = require "world" local thisWorld = World.new() sceneGroup:insert(thisWorld.worldGroup) print( "\n1: create event") end function scene:show( event ) local phase = event.phase if "did" == phase then print( "1: show event, phase did" ) -- remove previous scene's view composer.removeScene( "scene2" ) -- Update Lua memory text display local showMem = function() image:addEventListener( "touch", image ) text3.isVisible = true text2.text = text2.text .. collectgarbage("count")/1000 .. "MB" text2.x = display.contentWidth \* 0.5 end memTimer = timer.performWithDelay( 1000, showMem, 1 ) end end function scene:hide( event ) local phase = event.phase if "will" == phase then print( "1: hide event, phase will" ) -- remove touch listener for image image:removeEventListener( "touch", image ) -- cancel timer timer.cancel( memTimer ); memTimer = nil; -- reset label text text2.text = "MemUsage: " end end function scene:destroy( event ) print( "((destroying scene 1's view))" ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

scene2.lua

 local composer = require( "composer" ) local scene = composer.newScene() local image, text1, text2, text3, memTimer local function onSceneTouch( self, event ) if event.phase == "began" then composer.gotoScene( "scene1", "fade", 400 ) return true end end function scene:create( event ) local sceneGroup = self.view image = display.newImage( "bg2.jpg" ) image.x = display.contentCenterX image.y = display.contentCenterY sceneGroup:insert( image ) image.touch = onSceneTouch text1 = display.newText( "Scene 2", 0, 0, native.systemFontBold, 24 ) text1:setFillColor( 255 ) text1.x, text1.y = display.contentWidth \* 0.5, 50 sceneGroup:insert( text1 ) text2 = display.newText( "MemUsage: ", 0, 0, native.systemFont, 16 ) text2:setFillColor( 255 ) text2.x, text2.y = display.contentWidth \* 0.5, display.contentHeight \* 0.5 sceneGroup:insert( text2 ) text3 = display.newText( "Touch to continue.", 0, 0, native.systemFontBold, 18 ) text3:setFillColor( 255 ); text3.isVisible = false text3.x, text3.y = display.contentWidth \* 0.5, display.contentHeight - 100 sceneGroup:insert( text3 ) print( "\n2: create event" ) end function scene:show( event ) local phase = event.phase if "did" == phase then print( "2: show event, phase did" ) -- remove previous scene's view composer.removeScene( "scene1" ) -- Update Lua memory text display local showMem = function() image:addEventListener( "touch", image ) text3.isVisible = true text2.text = text2.text .. collectgarbage("count")/1000 .. "MB" text2.x = display.contentWidth \* 0.5 end memTimer = timer.performWithDelay( 1000, showMem, 1 ) end end function scene:hide( event ) local phase = event.phase if "will" == phase then print( "2: hide event, phase will" ) image:removeEventListener( "touch", image ) timer.cancel( memTimer ); memTimer = nil; text2.text = "MemUsage: " end end function scene:destroy( event ) print( "((destroying scene 2's view))" ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

local World = require “world”    – ‘World’ is handle to the table ‘world’ that is returned from the "require(“world”) world.lua file 

local thisWorld = World.new()    –  ‘thisWorld’ is a handle to the actual display group ‘worldGroup’ because that is what you

returned from the ‘new()’ function.  SO, do this:

sceneGroup:insert(thisWorld)  NOT  sceneGroup:insert(thisWorld.worldGroup)

Even though you did assign the ‘worldGroup’ display group to the ‘world’ table, in that ‘new()’ function, you are returning the displayGroup itself from that ‘new()’ function. 

OR, do this:

remove the line ‘return worldGroup’ from the ‘new()’ function

in the scene, instead of    local thisWorld = World.new(),  just do   ‘World.new()’

then below that line

sceneGroup:insert(World.worldGroup)

Either way will work… in the first option you are returning the display group directly from the ‘new()’ function call and assigning a handle ‘thisWorld’ to it…

The second way, in the 'new() function you are assigning the display group to the ‘world’ table, so you do not have to return the display group on the ‘new()’ function call.  You can just access it by calling the handle ‘World’ and ‘.worldGroup’  

for example:   World.worldGroup

Hope this is helpful.

Hi Matteo

You can modify your function world.new()

function world.new() local worldGroup = display.newGroup() local sky = display.newRect(350,50, 50,100 ) worldGroup:insert(sky) local ground = display.newRect(250,120, 30,36 ) worldGroup:insert(ground) world.worldGroup = worldGroup return world -- it was 'return worldGroup' before end

It should return the object itself.

In your case the object was named world, because you declared it on world.lua line1 as local world = {}

And thank you cyberparkstudios for your explanation. As you said, your solutions work as well.

A big thank you for finding this old post.

I have an understanding of modules and variables (or data) but handling display objects and modules was proving a bit difficult to get my head around - (am attempting to create various modules for things to help manage code).

This post i would not have found using my normal google search technique. I haven’t tried things out but it gives me some ideas to try!

Thanks

T.

Thanks everybody!

It helped me a lot.

Matt

Hello everybody

Please help me.

I tried to get the snippet of the solutions together with part in composer and I don’t get over this error:

 “\ scene1.lua:38: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’”.

This is all the code I put together from the snippets.

https://github.com/galahad9/AccessClassObjectInComposer

main.lua

local composer = require "composer" -- load first scene composer.gotoScene( "scene1", "fade", 400 )

world.lua (class/module)

local world = {} function world.new() local worldGroup = display.newGroup() local sky = display.newRect(350,50, 50,100 ) worldGroup:insert(sky) local ground = display.newRect(250,120, 30,36 ) worldGroup:insert(ground) world.worldGroup = worldGroup return worldGroup end function world:move() local thisSky = world.worldGroup[1] thisSky.x = thisSky.x - 10 -- this gave me nil end return world

scene1.lua (here is the error)

local composer = require( "composer" ) local scene = composer.newScene() local image, text1, text2, text3, memTimer local function onSceneTouch( self, event ) if event.phase == "began" then composer.gotoScene( "scene2", "slideLeft", 800 ) return true end end function scene:create( event ) local sceneGroup = self.view image = display.newImage( "bg.jpg" ) image.x = display.contentCenterX image.y = display.contentCenterY sceneGroup:insert( image ) image.touch = onSceneTouch text1 = display.newText( "Scene 1", 0, 0, native.systemFontBold, 24 ) text1:setFillColor( 255 ) text1.x, text1.y = display.contentWidth \* 0.5, 50 sceneGroup:insert( text1 ) text2 = display.newText( "MemUsage: ", 0, 0, native.systemFont, 16 ) text2:setFillColor( 255 ) text2.x, text2.y = display.contentWidth \* 0.5, display.contentHeight \* 0.5 sceneGroup:insert( text2 ) text3 = display.newText( "Touch to continue.", 0, 0, native.systemFontBold, 18 ) text3:setFillColor( 255 ); text3.isVisible = false text3.x, text3.y = display.contentWidth \* 0.5, display.contentHeight - 100 sceneGroup:insert( text3 ) -- ACCESSING MODULE world.lua local World = require "world" local thisWorld = World.new() sceneGroup:insert(thisWorld.worldGroup) print( "\n1: create event") end function scene:show( event ) local phase = event.phase if "did" == phase then print( "1: show event, phase did" ) -- remove previous scene's view composer.removeScene( "scene2" ) -- Update Lua memory text display local showMem = function() image:addEventListener( "touch", image ) text3.isVisible = true text2.text = text2.text .. collectgarbage("count")/1000 .. "MB" text2.x = display.contentWidth \* 0.5 end memTimer = timer.performWithDelay( 1000, showMem, 1 ) end end function scene:hide( event ) local phase = event.phase if "will" == phase then print( "1: hide event, phase will" ) -- remove touch listener for image image:removeEventListener( "touch", image ) -- cancel timer timer.cancel( memTimer ); memTimer = nil; -- reset label text text2.text = "MemUsage: " end end function scene:destroy( event ) print( "((destroying scene 1's view))" ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

scene2.lua

 local composer = require( "composer" ) local scene = composer.newScene() local image, text1, text2, text3, memTimer local function onSceneTouch( self, event ) if event.phase == "began" then composer.gotoScene( "scene1", "fade", 400 ) return true end end function scene:create( event ) local sceneGroup = self.view image = display.newImage( "bg2.jpg" ) image.x = display.contentCenterX image.y = display.contentCenterY sceneGroup:insert( image ) image.touch = onSceneTouch text1 = display.newText( "Scene 2", 0, 0, native.systemFontBold, 24 ) text1:setFillColor( 255 ) text1.x, text1.y = display.contentWidth \* 0.5, 50 sceneGroup:insert( text1 ) text2 = display.newText( "MemUsage: ", 0, 0, native.systemFont, 16 ) text2:setFillColor( 255 ) text2.x, text2.y = display.contentWidth \* 0.5, display.contentHeight \* 0.5 sceneGroup:insert( text2 ) text3 = display.newText( "Touch to continue.", 0, 0, native.systemFontBold, 18 ) text3:setFillColor( 255 ); text3.isVisible = false text3.x, text3.y = display.contentWidth \* 0.5, display.contentHeight - 100 sceneGroup:insert( text3 ) print( "\n2: create event" ) end function scene:show( event ) local phase = event.phase if "did" == phase then print( "2: show event, phase did" ) -- remove previous scene's view composer.removeScene( "scene1" ) -- Update Lua memory text display local showMem = function() image:addEventListener( "touch", image ) text3.isVisible = true text2.text = text2.text .. collectgarbage("count")/1000 .. "MB" text2.x = display.contentWidth \* 0.5 end memTimer = timer.performWithDelay( 1000, showMem, 1 ) end end function scene:hide( event ) local phase = event.phase if "will" == phase then print( "2: hide event, phase will" ) image:removeEventListener( "touch", image ) timer.cancel( memTimer ); memTimer = nil; text2.text = "MemUsage: " end end function scene:destroy( event ) print( "((destroying scene 2's view))" ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

local World = require “world”    – ‘World’ is handle to the table ‘world’ that is returned from the "require(“world”) world.lua file 

local thisWorld = World.new()    –  ‘thisWorld’ is a handle to the actual display group ‘worldGroup’ because that is what you

returned from the ‘new()’ function.  SO, do this:

sceneGroup:insert(thisWorld)  NOT  sceneGroup:insert(thisWorld.worldGroup)

Even though you did assign the ‘worldGroup’ display group to the ‘world’ table, in that ‘new()’ function, you are returning the displayGroup itself from that ‘new()’ function. 

OR, do this:

remove the line ‘return worldGroup’ from the ‘new()’ function

in the scene, instead of    local thisWorld = World.new(),  just do   ‘World.new()’

then below that line

sceneGroup:insert(World.worldGroup)

Either way will work… in the first option you are returning the display group directly from the ‘new()’ function call and assigning a handle ‘thisWorld’ to it…

The second way, in the 'new() function you are assigning the display group to the ‘world’ table, so you do not have to return the display group on the ‘new()’ function call.  You can just access it by calling the handle ‘World’ and ‘.worldGroup’  

for example:   World.worldGroup

Hope this is helpful.

Hi Matteo

You can modify your function world.new()

function world.new() local worldGroup = display.newGroup() local sky = display.newRect(350,50, 50,100 ) worldGroup:insert(sky) local ground = display.newRect(250,120, 30,36 ) worldGroup:insert(ground) world.worldGroup = worldGroup return world -- it was 'return worldGroup' before end

It should return the object itself.

In your case the object was named world, because you declared it on world.lua line1 as local world = {}

And thank you cyberparkstudios for your explanation. As you said, your solutions work as well.

A big thank you for finding this old post.

I have an understanding of modules and variables (or data) but handling display objects and modules was proving a bit difficult to get my head around - (am attempting to create various modules for things to help manage code).

This post i would not have found using my normal google search technique. I haven’t tried things out but it gives me some ideas to try!

Thanks

T.

Thanks everybody!

It helped me a lot.

Matt