How can I make this object spawn again?

Hi guys,

I’m new to corona sdks and lua. So today I tried making this funny app for my friend his birthday, but I can’t seem to figure 1 thing out. It’s basically a whack the mole game and every time the mole is tapped it disappears (no problems so far), but when I tap the mole, I’d also like to make another mole appear and tappable and this proces over and over again.

This is the code, I tried to label everything :slight_smile:

------------ Start Game App --------------- -- Coordinations -- local centerX = display.contentCenterX local centerY = display.contentCenterY -- Forward References -- local startGame local spawnEnemy1 local enemy local tapStart local safkes = 0 local scoreTxt local safke = audio.loadSound("Snd/safke.wav") -- Global style display.setDefault( "background", 40, 300, 40 ) display.setStatusBar( display.HiddenStatusBar ) -- Menu Screen -- local function startMenu() -- Start Button startBtn = display.newImage("Img/Start.png") startBtn.x = centerX startBtn.y = centerY startBtn.alpha = 0 -- Transition transition.fadeIn( startBtn, onStart ) -- Transition Away From Menu startBtn:addEventListener( "tap", tapStart ) end -- Game Start -- function startGame() spawnEnemy1() enemy:addEventListener( "tap", destroyEnemy ) scoreTxt = display.newText("safkes: 0", 0, 0, "Helvetica", 25 ) scoreTxt.anchorX = 1 scoreTxt.anchorY = 0.5 scoreTxt.x = centerX / 0.85 scoreTxt.y = display.screenOriginY + 15 scoreTxt:setTextColor( 0, 0, 0 ) end -- Game Funtions -- function spawnEnemy1() enemy = display.newImage("Img/Enemy.png") enemy.y = math.random( display.contentHeight ) if math.random(2) == 1 then enemy.x = math.random ( -100, -10) else enemy.x = math.random ( display.contentWidth + 10, display.contentWidth + 100 ) end enemy: scale( .5, .5) transition.to( enemy, { time = 1000, x = math.random( display.contentWidth ), y = math.random( display.contentHeight ) } ) end -- Event Funtions -- function tapStart (event) display.remove(startBtn) -- Transition To the Game startGame() end function destroyEnemy(event) display.remove(enemy) safkes = safkes + 1 scoreTxt.text = "safkes: " .. safkes audio.play(safke) end startMenu()

So yeah, the code as it is at the moment looks fine in my opinion, just that last part of the game isn’t working out :confused:

Just looking at your code very very quickly …  my first thought would be to just put a call to  spawnEnemy1() at the end of your ‘destroyEnemy’ function.  And you should move the   'enemy:addEventListener(“tap”, destroyEnemy) code down into the spawnEnemy1() function,right after you create the image.

this is just the quick fix, if I had more time,  I could look at it closer, but I think that will get it doing what you want.

good luck

I think i have the perfect template for you here…! 

http://coronatemplates.tk/templates

Download “Disappearing Dot” 

Good Luck!

 

Thank you,

this was exactly what I needed!

To me it seems that the code in the template i mentioned is much more efficient…

Good Luck!

I’ve attempted to fix it up. I recommend using storyboard scenes, but if you aren’t, I think this works.

------------ Start Game App --------------- -- Coordinations -- local \_centerX = display.contentCenterX local \_centerY = display.contentCenterY local \_maxW = display.contentWidth local \_maxH = display.contentHeight -- Forward References -- local startGame local tapStart local safkes = 0 local scoreTxt local safke = audio.loadSound("Snd/safke.wav") local startBtn -- Event Handler Functions -- local onStart = function (event) -- Transition Away From Menu startBtn:addEventListener( "tap", tapStart ) end local tapStart = function (event) —- this prevents a tap handler from firing startBtn.alpha = 0 startGame() end local destroyEnemy = function(event) enemyGroup:remove(enemy) safkes = safkes + 1 scoreTxt.text = "safkes: " .. safkes audio.play(safke) end -- Global style display.setDefault( "background", 40, 300, 40 ) display.setStatusBar( display.HiddenStatusBar ) local group = display.newGroup() local enemyGroup = display.newGroup() group:insert(enemyGroup) -- Game Functions -- function spawnEnemy() local enemy = display.newImage("Img/Enemy.png") enemyGroup:insert(enemy) enemy.y = math.random( \_maxH ) if math.random(2) == 1 then enemy.x = math.random ( -100, -10) else enemy.x = math.random ( \_maxW + 10, \_maxW + 100 ) end enemy: scale( .5, .5) enemy:addEventListener( "tap", destroyEnemy ) transition.to( enemy, { time = 1000, x = math.random( \_maxW ), y = math.random( \_maxH ) } ) end -- Game Start -- function startGame() spawnEnemy() scoreTxt = display.newText(group, "safkes: 0", 0, 0, "Helvetica", 25 ) scoreTxt.anchorX = 1 scoreTxt.anchorY = 0.5 scoreTxt.x = centerX / 0.85 scoreTxt.y = display.screenOriginY + 15 scoreTxt:setTextColor( 0, 0, 0 ) end ————— —- Menu Screen ————— -- Start Button startBtn = display.newImage("Img/Start.png") group:add(startBtn) startBtn.x = centerX startBtn.y = centerY startBtn.alpha = 0 -- Transition transition.fadeIn( startBtn, onStart )

Ok so here’s some code that will work perfect and is very simple and then im done with this topic… 

local BG = display.newImageRect( "BG.png", 1080, 1920 ) local startTimer local function spawnBall() local spawn = math.random(display.contentWidth \* 0.1, display.contentWidth \* 0.9) local spawn2 = math.random(display.contentHeight \* 0.01, display.contentHeight \* 0.99) local ball = display.newImageRect("dot.png", 50, 50 ) ball.x = spawn ball.y = spawn2 local function ballTouch(event) if event.phase == "began" then spawnBall() display.remove(ball) end end ball:addEventListener( "touch", ballTouch ) end startTimer = timer.performWithDelay( 100, spawnBall, 1 )

Hi

thank you for all the help!

I figured out the problem already but your code looks a bit cleaner so I’m definitely going to review it! :slight_smile:

Just looking at your code very very quickly …  my first thought would be to just put a call to  spawnEnemy1() at the end of your ‘destroyEnemy’ function.  And you should move the   'enemy:addEventListener(“tap”, destroyEnemy) code down into the spawnEnemy1() function,right after you create the image.

this is just the quick fix, if I had more time,  I could look at it closer, but I think that will get it doing what you want.

good luck

I think i have the perfect template for you here…! 

http://coronatemplates.tk/templates

Download “Disappearing Dot” 

Good Luck!

 

Thank you,

this was exactly what I needed!

To me it seems that the code in the template i mentioned is much more efficient…

Good Luck!

I’ve attempted to fix it up. I recommend using storyboard scenes, but if you aren’t, I think this works.

------------ Start Game App --------------- -- Coordinations -- local \_centerX = display.contentCenterX local \_centerY = display.contentCenterY local \_maxW = display.contentWidth local \_maxH = display.contentHeight -- Forward References -- local startGame local tapStart local safkes = 0 local scoreTxt local safke = audio.loadSound("Snd/safke.wav") local startBtn -- Event Handler Functions -- local onStart = function (event) -- Transition Away From Menu startBtn:addEventListener( "tap", tapStart ) end local tapStart = function (event) —- this prevents a tap handler from firing startBtn.alpha = 0 startGame() end local destroyEnemy = function(event) enemyGroup:remove(enemy) safkes = safkes + 1 scoreTxt.text = "safkes: " .. safkes audio.play(safke) end -- Global style display.setDefault( "background", 40, 300, 40 ) display.setStatusBar( display.HiddenStatusBar ) local group = display.newGroup() local enemyGroup = display.newGroup() group:insert(enemyGroup) -- Game Functions -- function spawnEnemy() local enemy = display.newImage("Img/Enemy.png") enemyGroup:insert(enemy) enemy.y = math.random( \_maxH ) if math.random(2) == 1 then enemy.x = math.random ( -100, -10) else enemy.x = math.random ( \_maxW + 10, \_maxW + 100 ) end enemy: scale( .5, .5) enemy:addEventListener( "tap", destroyEnemy ) transition.to( enemy, { time = 1000, x = math.random( \_maxW ), y = math.random( \_maxH ) } ) end -- Game Start -- function startGame() spawnEnemy() scoreTxt = display.newText(group, "safkes: 0", 0, 0, "Helvetica", 25 ) scoreTxt.anchorX = 1 scoreTxt.anchorY = 0.5 scoreTxt.x = centerX / 0.85 scoreTxt.y = display.screenOriginY + 15 scoreTxt:setTextColor( 0, 0, 0 ) end ————— —- Menu Screen ————— -- Start Button startBtn = display.newImage("Img/Start.png") group:add(startBtn) startBtn.x = centerX startBtn.y = centerY startBtn.alpha = 0 -- Transition transition.fadeIn( startBtn, onStart )

Ok so here’s some code that will work perfect and is very simple and then im done with this topic… 

local BG = display.newImageRect( "BG.png", 1080, 1920 ) local startTimer local function spawnBall() local spawn = math.random(display.contentWidth \* 0.1, display.contentWidth \* 0.9) local spawn2 = math.random(display.contentHeight \* 0.01, display.contentHeight \* 0.99) local ball = display.newImageRect("dot.png", 50, 50 ) ball.x = spawn ball.y = spawn2 local function ballTouch(event) if event.phase == "began" then spawnBall() display.remove(ball) end end ball:addEventListener( "touch", ballTouch ) end startTimer = timer.performWithDelay( 100, spawnBall, 1 )

Hi

thank you for all the help!

I figured out the problem already but your code looks a bit cleaner so I’m definitely going to review it! :slight_smile: