"Spawning" a new display object outside of "scene:create"

Hi all.

I’m messing with some code from the modified “Many crates” from the “How to Display and Save Scores” tutorial, but I’m running into an issue when using the code with composer - here’s modified Many Crates:

https://www.dropbox.com/s/u3vz4cquely690s/Scores.zip

Obviously, if I create a new display group within scene:create (and insert the “apple” into the group, from within the newApple function), my touch event listener will remove all display objects within said group - without inserting them into the scene:create, the touch event listener completely ignores them.

Is there an alternative way I can insert them (individually, not a group) into scene:create from within the function? Or Is there something very obvious I’m missing, and it’s an easy fix?

Here’s the code I’ve modified:

local function newApple() rand = math.random( 200 ) local apple if (rand \< 80) then apple = display.newImageRect("assets/apple1.png", 70, 75); apple.y = 90 + math.random( 200 ) apple.x = -25 timer.performWithDelay(2000, function() display.remove( apple ); end) physics.addBody( apple, "dynamic", {filter=appleCollisionFilter} ) apple:applyLinearImpulse( 0.5, 0.2, apple.x, apple.y ) apple.value = 50 elseif (rand \< 60) then apple = display.newImageRect("assets/apple4.png", 70, 75) apple.y = 90 + math.random( 200 ) apple.x = -25 timer.performWithDelay(2000, function() display.remove( apple ); end) physics.addBody( apple, "dynamic", {filter=apple2CollisionFilter} ) apple:applyLinearImpulse( 0.5, 0.2, apple.x, apple.y ) apple.value = 100 elseif (rand \<100) then apple = display.newImageRect("assets/apple3.png", 70, 75); apple.y = 90 + math.random( 200 ) apple.x = -25 timer.performWithDelay(2000, function() display.remove( apple ); end) physics.addBody( apple, "dynamic", {filter=apple4CollisionFilter} ) apple:applyLinearImpulse( 0.5, 0.2, apple.x, apple.y ) apple.value = 100 else apple = display.newImageRect("assets/apple2.png", 70, 75); apple.y = 90 + math.random( 200 ) apple.x = -25 timer.performWithDelay(2000, function() display.remove( apple ); end) physics.addBody( apple, "dynamic", {filter=apple3CollisionFilter} ) apple:applyLinearImpulse( 0.5, 0.2, apple.x, apple.y ) apple.value = 500 return apple end end

Any help will be greatly appreciated!

Kind regards,

Lewis

Hi @incentivegaminglp,

I would suggest this:

  1. Create a new display group outside (and above) all “scene:” functions, for proper upvalue scope.

  2. In “scene:create()”, insert that display group into the scene’s main view (self.view).

  3. In the “newApple()” function, insert new apples into the group you created in step 1.

This will make it so that the apples are contained in their own group, and since that group is a child of the scene’s core group, all apples will be removed when you exit/destroy the scene.

Hope this helps,

Brent

Hi @incentivegaminglp,

I would suggest this:

  1. Create a new display group outside (and above) all “scene:” functions, for proper upvalue scope.

  2. In “scene:create()”, insert that display group into the scene’s main view (self.view).

  3. In the “newApple()” function, insert new apples into the group you created in step 1.

This will make it so that the apples are contained in their own group, and since that group is a child of the scene’s core group, all apples will be removed when you exit/destroy the scene.

Hope this helps,

Brent