Spawning Multiple Objects

Hi,
I have set up a spawning feature in my game and it works great but as the levels progress I want more beach balls to fall sooner. Ex. one ball is spawned then soon after that another is spawned so it more are falling to make it tougher and not just one…then another…then another etc.

Here’s a part of the code that makes multiple objects fall but there’s a problem with it…

timer.performWithDelay( 100, spawnBall, 2 )  

when I have it set to 2 it spawns them both at once and, because of my collision code, it detects a collision and then goes crazy by spawning all of the beach balls on the screen.

if event.other.name == "pig" then  
 timer.performWithDelay(100, function() self:removeSelf() end)  
 elseif event.other.name == "extraLine" then  
 timer.performWithDelay(100, function() self:removeSelf() end)  
elseif event.other.name == "line" then  
 timer.performWithDelay(100, function() self:removeSelf() end)  

And it throws up errors saying…

…rs/dannykilkenny/Desktop/Adam’s Apple - Fixed/L7.lua:134: ERROR: Attempt to remove an object that’s already been removed from the stage or whose parent/ancestor group has already been removed.
stack traceback:
[C]: ?
[C]: in function ‘removeSelf’
…rs/dannykilkenny/Desktop/Adam’s Apple - Fixed/L7.lua:134: in function ‘_listener’
?: in function <?:446>
?: in function <?:215>
Any Ideas? [import]uid: 59140 topic_id: 18311 reply_id: 318311[/import]

Achoo! Oops :stuck_out_tongue: do you think I’m going to have to rework the entire code? [import]uid: 59140 topic_id: 18311 reply_id: 70268[/import]

Hi Danny,

Sounds like you’re trying to remove something you’ve already removed.

Also, with the ball spawning, you’re spawning them inside each other I think - maybe you should adjust where they spawn slightly? (As in side by side or the like?)

Peach :slight_smile: [import]uid: 52491 topic_id: 18311 reply_id: 70287[/import]

Hey Peach,
When I try to spawn more than on object at a time then yes I am trying to remove more than one object and that is the problem.
Also, deeper in the code, I have them randomly appearing at different points on the screen. [import]uid: 59140 topic_id: 18311 reply_id: 70294[/import]

Erm, just had a thought - how are you spawning them? Do they both have the same name?

For trying to remove more than one object, that shouldn’t be an issue - the only issue I can think of is if you’re naming them both “ball” or the like. Is that the case?

Peach :slight_smile: [import]uid: 52491 topic_id: 18311 reply_id: 70302[/import]