display.setStatusBar( display.HiddenStatusBar ) local physics = require "physics" physics.start() physics.setGravity( 0, 0 ) -- Variables local centerX = display.contentCenterX local centerY = display.contentCenterY local xmin = display.screenOriginX - 50 local xmax = display.contentWidth + 50 local ymin = display.screenOriginY - 50 local ymax = display.contentHeight + 50 local spawnIncreaseTimer local spawnNumber=0 local speedBump = 0 local earthRotation local gametimer local nSeconds = 0 local nMins = 0 local nHours = 0 local numSec = 0 local nSecs = 0 local timervalue = 0 local spawnTimer local firebullet local bg = display.newImage("images/starbg.png") bg.x = centerX bg.y = centerY local logo = display.newImage("images/Logo.png") logo.x = centerX logo.y = centerY local begin = display.newImage("images/start.png") begin.x = centerX + 50 begin.y = centerY + 45 -- Functions -- Esta função faz o texto piscar function blink() if(begin.alpha \< 1) then transition.to( begin, {time=490, alpha=1}) else transition.to( begin, {time=490, alpha=0.1}) end end txt\_blink = timer.performWithDelay(500, blink, 0) -- Esta função aumenta, de 10 em 10 segundos, o numero de asteroides que aparecem por cada 2 segundos local function spawnIncrease() spawnNumber=spawnNumber+1 spawnIncreaseTimer = timer.performWithDelay( 15000, spawnIncrease) end -- Esta função faz com que os asteroides desapareçam se colidirem com a terra local function startcolision() function colision(event) timer.performWithDelay(1, function() display.remove(asteroids) end, 1) return true end asteroids:addEventListener("collision",colision) return asteroids end function colision(obj) display.remove( obj ) end -- Esta função faz com que os asteroides apareçam local function spawn() for i=1,spawnNumber do local allAsteroids = {"images/enemy1.1.png" , "images/enemy2.1.png"} asteroids = display.newImage(allAsteroids[math.random(#allAsteroids)]) asteroids.x = math.random(xmin, xmax) asteroids.y = math.random(ymin, ymax) if (asteroids.x \> display.screenOriginX and asteroids.x \< display.contentWidth + 1 and asteroids.y \> display.screenOriginY and asteroids.y \< display.contentHeight + 1) then display.remove(asteroids) else transition.to( asteroids, { time=math.random(3000-speedBump , 5000-speedBump), y = centerY, x = centerX , rotation = math.random(50 , 180), onComplete = colision }) speedBump = speedBump + 15 end end spawnTimer = timer.performWithDelay( 2000, spawn) end -- Esta função adiciona o score ao display local function startGame() scoreText = display.newText( "Score", 0, 0, "Helvetica", 22 ) scoreText2 = display.newText("0", 0, 0, "Helvetica", 22) scoreText.x = display.screenOriginX + 50 scoreText.y = display.screenOriginY + 20 scoreText2.x = display.screenOriginX + 50 scoreText2.y = display.screenOriginY + 45 spawnIncrease() spawn() end -- Esta função adiciona o timer ao display local function timerGame() timerTxt = display.newText("Time", 0, 0, "Helvetica", 22) timerTxt2 = display.newText("00:00:00", 0, 0, "Helvetica", 22) timerTxt.x = display.contentWidth - 15 timerTxt.y = display.screenOriginY + 20 timerTxt2.x = display.contentWidth - 15 timerTxt2.y = display.screenOriginY + 45 end -- Esta função dá inicio ao timer local function timeCount() nSeconds = timervalue if nSeconds == 0 then timerTxt2.text = "00:00:00"; else nHours = string.format("%02.f", math.floor(nSeconds/3600)); nMins = string.format("%02.f", math.floor(nSeconds/60 - (nHours\*60))); nSecs = string.format("%02.f", math.floor(nSeconds - nHours\*3600 - nMins \*60)); timerTxt2.text = nHours..":"..nMins..":"..nSecs end timervalue = timervalue + 1 gametimer = timer.performWithDelay( 1000, timeCount) end -- Esta função faz parar o aumento de asteroides que aparecem local function stopSpawnIncrease() timer.cancel( spawnIncreaseTimer ) end -- Esta função faz parar o aumento de rotação da terra local function stopRotationIncrease() timer.cancel( earthRotation ) end -- Esta função dá inicio ao jogo local function go(event) display.remove(logo) display.remove(begin) local planet = display.newImage("images/earth.png") planet.x = centerX planet.y = centerY -- Esta função faz a terra rodar e aumento a sua rotação por cada 10 segundos local function animate() planet.rotation = planet.rotation + 2 earthRotation = timer.performWithDelay( 10000, animate) end Runtime:addEventListener("enterFrame",animate); planet:scale(4, 4) transition.to( planet, { time = 200, xScale = 1, yScale = 1, onComplete = animate} ) startGame() timerGame() timeCount() local function firebullet(event) local bullet = display.newCircle( planet.x, planet.y, 2 ) physics.addBody( bullet, { density=3.0, friction=0.5, bounce=0.05 } ) end bg:addEventListener ( "tap", firebullet) end begin:addEventListener ( "tap", go)
So, this is my full code, everytime that I tap the screen a bullet spawns. Now, my problem is actually to shoot the bullet in the direction to where the cannon (in the earth) is pointing when I tap the screen. Any idea on how I can do that?