Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Adding object to display group from external module.
Started by Appletreeman Jul 22 2018 05:12 AM

1 reply to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Appletreeman

[GLOBAL: userInfoPane.html]
Appletreeman
  • Contributor

  • 396 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 361 posts
  • Corona SDK

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


Edited by ldurniat, 22 July 2018 - 07:53 AM.



[topic_controls]
[/topic_controls]