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]