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
------------ 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
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.
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 )
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.
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 )