I spawn one enemy the first time my the player taps the start button.
The problem is that when the player gets a score of 3 i add one more enemy with the same function to spawn the first one it does work. But as soon as its game over and they want to restart it starts with the amount of enemies(2) they were at the previous game. This problem also occurs when i leave the play scene at 1 enemy and then go into the play scene again. Then there are 2 enemies also and 3 if i do it again etc.
What i think is the cause: I am thinking it is my start game button which remains in the scene even after going to another scene which then creates a double/triple on the next play causing one tap to trigger three spawns. Is this possible?
What i tried: I have tried to put it into a group which didnt seem to help. I also tried to place the whole function into the create scene or show scene parts which didnt help either.
Maybe some more experienced pro will know what i have done wrong??
Here below you can see my functions i use for the spawns and start game.
function spawnEnemy() local enemypics = {"images/beetleship.png","images/octopus.png", "images/rocketship.png"} enemy = display.newImage(scene.view, enemypics[math.random (#enemypics)]) enemy:addEventListener ( "tap", shipSmash ) if math.random(2) == 1 then enemy.x = math.random ( -100, -10 ) else enemy.x = math.random ( display.contentWidth + 10, display.contentWidth + 100 ) enemy.xScale = -1 end enemy.y = math.random (display.contentHeight) enemy.trans = transition.to ( enemy, { x=centerX, y=centerY, time=math.random(2500-speedBump, 4500-speedBump), onComplete=hitPlanet } ) speedBump = speedBump + 50 end function startGame() local textstart = display.newText( scene.view,"Tap here to start.!", 0, 0, "Helvetica", 24 ) textstart.x = centerX textstart.y = display.contentHeight - 30 textstart:setTextColor(255, 254, 185) local function goAway(event) display.remove(event.target) textstart = nil spawnEnemy() scoreTxt.alpha = 1 scoreTxt.text = "Score: 0" score = 0 planet.numHits = 2 planet.alpha = 1 speedBump = 0 playMusic() end textstart:addEventListener ( "tap", goAway ) end function shipSmash(event) local obj = event.target display.remove( obj ) playSFX ( sndKill ) transition.cancel ( event.target.trans ) score = score +1 scoreTxt.text = "Score: " .. score if planet.numHits \> 1 then spawnEnemy() addEnemy = addEnemy + 1 end if addEnemy == 3 then addEnemy = 0 spawnEnemy() end return true end
Please help me!