Spawning Objects at The Same Time (just check out the post :)

I have set up spawning so the apples in my game are falling and everything is working like a charm. However, I want a black apple (bad apple) to show up every so often to add difficulty. Is there a way I can easily tack that code in somewhere.

spawnApple = function()   
  
if n \>= maxApples then  
  
 noMoreApples()  
 pauseBtn.isVisible = false   
 pig.isVisible = false  
 return true  
 end  
 physics.setGravity(0, 9.81\*spawn\_speed)  
 local apple = display.newImage ( "Red Apple.png" )  
 physics.addBody( apple, { density = 3.0 } ,{ isSensor = true} )  
 apple.x = math.random(40, 260)  
 apple.y = math.random(60, 210)  
 apple.name = "apple"  
 localGroup:insert(apple)  
  
 apple.collision = onLocalCollision  
 apple:addEventListener( "collision", apple )  
 n = n + 1 -- This is where the counter variable gets incremented  
end  
  
--this increase the rate at which red apples fall  
spawn\_speed = spawn\_speed + 0.05  
  
--and this determines when to spawn the next red apple  

Thanks,
Danny [import]uid: 59140 topic_id: 21994 reply_id: 321994[/import]

You could spawn one depending on value of n or you could have a little if statement tied to a math.random() where there is a 1 in 10 chance of spawning a bad apple rather than good.

That’s the simplest way IMHO.

Peach :slight_smile: [import]uid: 52491 topic_id: 21994 reply_id: 87433[/import]

maybe you could try something like this

[lua]local case = math.random( 10 )
local obj
if case == 10 then
obj = display.newImage( “blackApple.png” )
obj.type = “black”
else
obj = display.newImage( “redApple.png” )
obj.type = “red”
end
obj.name = “apple”[/lua] [import]uid: 114118 topic_id: 21994 reply_id: 87486[/import]

Ok so I edited the code (below) and I have it appearing randomly but I have some questions.

spawnApple = function()   
  
if n == math.random( 3 ) then  
 Blackapple = display.newImage ( "Black Apple.png" )  
 physics.addBody( Blackapple, { density = 3.0 } ,{ isSensor = true} )  
 Blackapple.x = math.random(40, 260)  
 Blackapple.y = math.random(60, 210)  
  
 Blackapple.collision = onLocalCollision  
 Blackapple:addEventListener( "collision", Blackapple )  
 end  
  
 physics.setGravity(0, 9.81\*spawn\_speed)  
 local apple = display.newImage ( "Red Apple.png" )  
 physics.addBody( apple, { density = 3.0 } ,{ isSensor = true} )  
 apple.x = math.random(40, 260)  
 apple.y = math.random(60, 210)  
 apple.name = "apple"  
 localGroup:insert(apple)  
  
 apple.collision = onLocalCollision  
 apple:addEventListener( "collision", apple )  
  
 n = n + 1   
end  

First, is there a way to stop the black apple from randomly falling at the beginning of the game other than putting a bigger number in the the math.random and then make it happen more frequently as the game goes on?

Second, do you know how I could eventually get two red apples to start falling in quick succession but not at the same time?

Third, I keep getting an error when the black apple falls because of the collision code and I can’t figure it out…

local function onLocalCollision( self,event )  
 if self.name == "apple" then  
 -- To avoid further collissions  
 local next\_apple = false  
 self:removeEventListener( "collision", apple )  
 if event.other.name == "pig" then  
 timer.performWithDelay(0, function() self:removeSelf() end)  
 scoreText.text = score  
 score = score + 1  
 play\_sound(pop)  
 spawn\_speed = spawn\_speed + 0.05  
 next\_apple = true  
 elseif event.other.name == "extraLine" then  
 timer.performWithDelay(1000, function() self:removeSelf() end)  
 scoreText.text = score  
 score = score + 1  
 next\_apple = true  
 elseif event.other.name == "line" then  
 timer.performWithDelay(0, function() self:removeSelf() end)  
 lives\_imgs[lives].isVisible = false  
 if lives == 1 then  
 noMoreApples()  
 pauseBtn.isVisible = false   
 pig.isVisible = false  
 else  
 lives = lives-1  
 next\_apple = true  
 end  
 end  
 if next\_apple then  
 timer.performWithDelay( 250/spawn\_speed, spawnApple, 1 )  
 end  
 end  
end  
   
apple.collision = onLocalCollision  
apple:addEventListener( "collision", apple )  
  
Blackapple.collision = onLocalCollision  
 Blackapple:addEventListener( "collision", Blackapple )  

I keep getting this error
Runtime error
…s/dannykilkenny/Desktop/Adam’s Apple - Fixed/L25.lua:153: 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’
…s/dannykilkenny/Desktop/Adam’s Apple - Fixed/L25.lua:153: in function ‘_listener’
?: in function <?:514>
?: in function <?:215>
I’m a little stumped.
[import]uid: 59140 topic_id: 21994 reply_id: 87600[/import]

what i understand from your code, is that whenever the black apple drops a red apple drops as well? is this what you wanted to do?

to increase the chances of a black apple spawning over time, you can increase the chances of it being randomly selected by reducing the random size, something like this

[lua]local chance = 10000
local case = math.random( chance )
timer.performWithDelay( 1000, function( ) chance = chance - 10 end )[/lua]

for your second question, what i would do is, add another if statement similar to the blackapple statement, where it would call the spawnApple function, like so

[lua]if case == 1 then
–blackapple
elseif case == 2 then
spawnApple( )
–timer.performWithDelay( 500, function( ) spawnApple( ) end )
else
–redapple
end[/lua]

for the third question, have you assigned a name for your black apple? ( i am not entirely sure what exactly is happening without knowing your entire code )

[lua]Blackapple.name = “apple”[/lua] [import]uid: 114118 topic_id: 21994 reply_id: 87607[/import]

Your answer to my first question looks like it works but I’ve isolated what I think my problem is. The code is throwing up an error when it’s trying to remove something.

timer.performWithDelay(0, function() self:removeSelf() end)  

That is throwing a lot off because I know the code you gave me to get the red apples to start falling works but I keep getting an error because it can’t remove the object.

Runtime error
…s/dannykilkenny/Desktop/Adam’s Apple - Fixed/L25.lua:144: 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’
…s/dannykilkenny/Desktop/Adam’s Apple - Fixed/L25.lua:144: in function ‘_listener’
?: in function <?:514>
?: in function <?:215>

All of the code pertaining to collisions and removal are already posted. I tried naming the black apple but that didn’t make a difference.

If a player is really good and somehow reaches 10,000 how can I make sure that a black apple isn’t falling every second or make sure that the code doesn’t get screwy when it tries to subtract 10 from 0? [import]uid: 59140 topic_id: 21994 reply_id: 87688[/import]

does the error come out only during a black apple collision?
because this part of your code

[lua]self:removeEventListener( “collision”, apple )[/lua]

seems like it would only remove the event listener from red apple, not black

as for your other question, how not to subtract 10 from 0,
you can check the value before the subtraction, like so

[lua]if chance > 20 then – or any other value
chance = chance - 10
end[/lua] [import]uid: 114118 topic_id: 21994 reply_id: 87690[/import]

The limiting code looks like it’s working great!

I’ve been testing the code and I’ve found out that the error happens does happen when the black apple makes a collision with anything. I tried putting in the removeListener code and changed “apple” to Blackapple and that just stopped any collisions from happening after the first red apple fell. [import]uid: 59140 topic_id: 21994 reply_id: 87721[/import]

how about trying

[lua]self:removeEventListener( “collision”, self )[/lua] [import]uid: 114118 topic_id: 21994 reply_id: 87766[/import]

WIN!

I tried out your code and it didn’t work out right away but after some tinkering I figured out this…
Before

self:removeEventListener( "collision", apple )  
Blackapple:removeEventListener( "collision", Blackapple )  

After

self:removeEventListener( "collision", self )  
Blackapple:removeEventListener( "collision", Blackapple )  

I don’t know why this works now but it does and that’s the main thing. Thanks for your help!

I do have one more question though. How do I incorporate it in the collisions so the player loses a life if they catch it? I tried to change the name of “event.other.name” but that just stopped the apples from falling after one apple. [import]uid: 59140 topic_id: 21994 reply_id: 88228[/import]

Bump [import]uid: 59140 topic_id: 21994 reply_id: 88549[/import]