Adding object to display group from external module.

Hi all,

In my game module I have a display group called “arena_group”.  When a new level is created it calls arena.new() function in my arena.lua module.  What I’m struggling with is how to add the objects created in arena.new to the arena_group.  If do the following (as an example)

example=display.newRect(arena\_group, 100,100,20,100)

I get the following error

Bad argument #1 to ‘newRect’ (number expected, got nil)

I know this is basic stuff but for whatever reason I just can’t fathom it.

Thanks in advance

Hi Appletreeman,

I would do something like this

  1. Pass in group paramater to new method

    –arena.lua function arena.new( options )     options = options or {}     group = options.group or composer.getScene( composer.getSceneName( ‘current’ ) )    – some code – Add new object to appropriate display group local object = display.newRect( group, 100, 100, 50 ,50 ) end

    –game.lua (your game module) local arena_group = display.newGroup() … arena.new( { group = arena_group } )

  2. Return newly created object and add it to arena_group group

    –arena.lua function arena.new()     --some code local object = display.newRect( 100, 100, 50 ,50 )     return object end

    –game.lua (your game module) local arena_group = display.newGroup() … local object = arena.new() – Add new object to appropriate display group arena_group:insert( object )

Have a nice day:)

ldurniat

Hi Appletreeman,

I would do something like this

  1. Pass in group paramater to new method

    –arena.lua function arena.new( options )     options = options or {}     group = options.group or composer.getScene( composer.getSceneName( ‘current’ ) )    – some code – Add new object to appropriate display group local object = display.newRect( group, 100, 100, 50 ,50 ) end

    –game.lua (your game module) local arena_group = display.newGroup() … arena.new( { group = arena_group } )

  2. Return newly created object and add it to arena_group group

    –arena.lua function arena.new()     --some code local object = display.newRect( 100, 100, 50 ,50 )     return object end

    –game.lua (your game module) local arena_group = display.newGroup() … local object = arena.new() – Add new object to appropriate display group arena_group:insert( object )

Have a nice day:)

ldurniat