adding objects OUTSIDE of scene:createScene() function

like, let’s say I have a function that creates bullets outside of the createScene() function

how would I go about adding each new bullet to the group?

[lua]function createBullet()
local bullet = display.newImage(“bullet.png”);
end

function scene:createScene( event )
local group = self.view
background = display.newImage(“background_example.png”);
group:insert(background);
end[/lua] [import]uid: 49300 topic_id: 25973 reply_id: 325973[/import]

Or let’s say I have a bullet_holder array.

[lua]function scene:createScene( event )
local group = self.view
background = display.newImage(“background_example.png”);
bullet_holder = {};
group:insert(bullet_holder);
group:insert(background);
end[/lua]

?? [import]uid: 49300 topic_id: 25973 reply_id: 105106[/import]

got it [import]uid: 49300 topic_id: 25973 reply_id: 105107[/import]

Can you post how you did it?

In theory outside of the scene event functions you should have scene.view which is what the self.view means inside the function available to you.

Also you can do something like:

  
local myGroup  
local function scene:createScene(event)  
 group = self.view  
 myGroup = group  
...  
end  

At that point, myGroup is available module wide and holds the reference to the scene’s group. [import]uid: 19626 topic_id: 25973 reply_id: 105128[/import]

[lua]bullet_holder = {};
function createNewBullet()
local bullet = display.newImage(“bullet.png”);
table.insert(bullet_holder,bullet);
World:insert(bullet);
print(bullet_holder[1].x);
print(bullet_holder[1].y);
end

function scene:createScene(event)
group = self.view;
World = display.newGroup();
group:insert(World);
end[/lua]

Just create a new display group (For example: World). From there you can add anything to the group: world, and it’ll be added into scene’s group.

[lua]function exampleInitMenu()
example_logo = display.newImage(“example_logo.png”);
World:insert(example_logo);
end

function scene:createScene(event)
group = self.view;
World = display.newGroup();
group:insert(World);
end

function scene:enterScene(event)
exampleInitMenu();
end[/lua]

What’s cool is you can still add things to World and it’ll be in effect even AFTER the execution of this line:

[lua]group:insert(World);[/lua] [import]uid: 49300 topic_id: 25973 reply_id: 105133[/import]

Thank you, guys. Reading your posts helped me understand Storyboard API further. About scene creation function, why not do this?

[lua]local myGroup
function scene:createScene(event)
myGroup = self.view

end[/lua]

Naomi
[import]uid: 67217 topic_id: 25973 reply_id: 105182[/import]

@Lucky, is your “group” variable local or global? I mean, do you forward reference “group” with local somewhere up top?

Naomi
[import]uid: 67217 topic_id: 25973 reply_id: 105193[/import]

If you just follow the template you should be fine:

  
local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
function scene:createScene( event )  
 local group = self.view  
end  
  

scene is local to this module. Therefore scene.view is local to the entire module.

In side the create, enter, exit and destroy handlers, those are part of the local scene object, but they are public functions as far as overall program visibility goes.

The template then gives you a short cut to self.view (scene.view) called “group”. That shortcut is only visible within the event handler.

If you need to access “group” outside of those functions you can either just directly reference scene.view or set up your own short cut scoped to the scene (and not global) [import]uid: 19626 topic_id: 25973 reply_id: 105198[/import]

Ah, yes, sorry–a typo, group is local inside of that createScene function. Well I usually will just never talk to “group” again after I put all the layers (display objects) inside of them. I’ll just talk to “group”'s global kids–World, Layer_UI, LayerNightAndDayEffect. [import]uid: 49300 topic_id: 25973 reply_id: 105199[/import]

I just don’t want to mess with that group = self.view :stuck_out_tongue:

Plus using different groups can allow depth sorting much easier. For example:

[lua]function scene:createScene(event)
local group = self.view;
Layer_UI = display.newGroup();
World = display.newGroup();
Layer_NightAndDayEffect = display.newGroup();
group:insert(World);
group:insert(Layer_NightAndDayEffect);
group:insert(Layer_UI);
end[/lua]

I’ll put my HP bar and joystick or whatever in Layer_UI.

If I wanted a black color tint over world, I’d put it in Layer_NightAndDayEffect, it would only affect World and not Layer_UI as the ordering above tells UI to be the highest depth.

Lastly, I put my player, monsters, bullets in World. [import]uid: 49300 topic_id: 25973 reply_id: 105191[/import]

Ah, it all makes sense. Thanks guys!

Naomi [import]uid: 67217 topic_id: 25973 reply_id: 105204[/import]

GENIAL!!!
Ahora entiendo el cómo funcionan los grupos dentro de los grupos… muchíiiiisimas gracias… [import]uid: 121547 topic_id: 25973 reply_id: 105281[/import]