My messy game.lua code as it stands.
[lua]module(…, package.seeall)
function new()
–local group for Director
local localGroup = display.newGroup()
local physics = require (“physics”)
– Start physics engine and set options
physics.setGravity( 0 , 0 )
–physics.setDrawMode( “hybrid” )
physics.start()
– Setup Display Groups
local bkgdGroup = display.newGroup()
local frgdGroup = display.newGroup()
local scoreGroup = display.newGroup()
– Setup background image
local bkgd = display.newImage (“background.png”)
– Setup foreground images
local station = display.newImage(“station.png”)
local pausebtn = display.newImage(“pause.png”)
– Display Group For Background Images
bkgdGroup:insert(bkgd)
bkgdGroup:setReferencePoint(display.CenterReferencePoint)
bkgdGroup.x = _W / 2; bkgdGroup.y = _H / 2;
– Displayground for foreground images
frgdGroup:insert(station)
station.x = _W / 2; station.y = _H - 100;
frgdGroup:insert(pausebtn)
pausebtn:setReferencePoint(display.BottomRightReferencePoint)
pausebtn.x = _W - 30; pausebtn.y = _H - 25; pausebtn.alpha = 0.5;
– Setup Scoreboard
local scoreBoard = display.newRect(0, 0, 200, 40)
– Preset Settings for disaply objects
scoreBoard:setFillColor(255, 255, 200, 70)
local myScoreText = display.newText(“0”, 0, 0, native.systemFont, 30)
myScoreText:setTextColor(255, 255, 255)
myScoreText:setReferencePoint(display.TopLeftReferencePoint)
myScoreText.x = 165; myScoreText.y = 4;
– Display Group for ScoreBoard
scoreGroup:insert(scoreBoard)
scoreGroup:insert(myScoreText)
scoreGroup:setReferencePoint(display.BottomLeftReferencePoint)
scoreGroup.x = 30; scoreGroup.y = _H - 25;
–Setup shield around station
shield = display.newImage(“shield_1.png”)
shield.myName = “shield”
frgdGroup:insert(shield)
shield.x = _W / 2 - 6; shield.y = _H - 100;
physics.addBody(shield, “static”, {density=1, friction=0, bounce=0, radius=78})
– Shoot listener function
function shootListener ()
function addShootListener()
bkgd:addEventListener (“tap”, shoot)
physics.start()
end
timer.performWithDelay( 300, addShootListener,1 )
end
– pause screen popup
local function pauseScreen( event )
if event.phase == “ended” then
bkgd:removeEventListener (“tap”, shoot)
physics.pause()
director:openPopUp( “pause”, shootListener)
end
end
– Update the score on the scoreboard
local function updateScore( event )
_MyScore = _myScore + 10
myScoreText.text = _MyScore
end
– Update shot data
local function updateShots ()
_ShotsFired = _ShotsFired + 1
end
– Update number of objects destroyed
local function updateDestroyed ()
_NumDestroyed = _NumDestroyed + 1
– End the game if the destroyed targets meets criteria
if _NumDestroyed == _ToDestroy then
endGame()
end
end
– When game ends, display game over graphis
– Show score, shots fired and number of object destroyed… dig i mention chicks dig it?
local function endGame ()
– Clean Up
bkgd:removeEventListener (“tap”, shoot)
cancelAllTransitions()
physics.stop()
– Display gameover dialogue
director:changeScene(“gameover”)
end
– Missile explosion at end of transition
local function msExplosion(x,y)
local explosion = display.newCircle(0,0,5)
frgdGroup:insert(explosion)
explosion.x = x; explosion.y = y; explosion.alpha=0.6;
explosion:setFillColor ( 255, 240, 0 )
local rand = math.random(15,25)
transitionStash.newTransition=transition.to(explosion, {time=1500, xScale=rand, yScale=rand, onComplete=function()explosion:removeSelf();updateDestroyed();end})
end
– Launch objects towards station from
– random locations across the top of the screen
local function deployAsteroid ()
local asteroid = display.newImage(“asteroid.png”)
asteroid.myName = “asteroid”
frgdGroup:insert(asteroid)
asteroid:setReferencePoint(display.CenterReferencePoint)
asteroid.x = math.random(-30, 1054); asteroid.y = -20;
asteroid.xScale = 0.75; asteroid.yScale = 0.75;
physics.addBody(asteroid, {density=1, friction=0, bounce=0, radius=47})
transitionStash.newTransition=transition.to(asteroid, {time=6000, x = _W / 2, y = _H-100, rotation= 180, onComplete=function()asteroid:removeSelf();end})
end
– Shoot the missile
function shoot ( event )
if event.y < (_H - 125) then
– update how many shots to have fired
updateShots()
– Initialize missile.png on screen
local smMissile = display.newImage(“smMissile.png”)
smMissile.myName = “missile”
frgdGroup:insert(smMissile)
– Maths… I hate it
radians = math.atan2(event.y-station.y,event.x-station.x)
degrees = ((radians/math.pi)*180)+90
– Maths… Rotate it
smMissile.rotation = degrees
– Missile launch location (on space station)
smMissile.x = display.contentCenterX
smMissile.y = 690
– Move missle from station to touch event… Vrooooom!
transitionStash.newTransition = transition.to(smMissile, {x=event.x, y=event.y, transition=easing.inQuad, onComplete=function() smMissile:removeSelf(); msExplosion(event.x,event.y); end})
end
end
local function depleteShield()
local function killShield()
shield = display.newImage(“shield_2.png”)
shield.myName = “shield”
frgdGroup:insert(shield)
shield.x = _W / 2 - 6; shield.y = _H - 100;
physics.addBody(shield, “static”, {density=1, friction=0, bounce=0, radius=78})
end
transitionStash.newTransition = transition.to(shield, {time=300, alpha = 0, onComplete= function() display.remove(shield); end})
timer.performWithDelay(300,killShield)
end
local function onCollision(e)
– Detect if the collision is from the asteroid hitting the station or a missile explosion
if (e.object2.myName == “asteroid”) and (e.phase == “began”) then
if _ShieldNo <= 5 then
_ShieldNo = 2
print(“Hit Detected”)
depleteShield()
–Insert code to remove asteroid here–
else
gameOver()
end
else if (e.object2.myName == “missile”) and (e.phase == “began”) then
msExplosion()
end
end
end
local function launchGame()
– check level number and increase items
– to be destroyed as game progresses
if _MyLevel == 1 then
_MyScore = 0
_ShotsFired = 0
_NumDestroyed = 0
_ToDestroy = 5
else
_ToDestroy = _toDestroy + math.random(5,8)
end
–Display what level you are onat start of game
local levelText = display.newText(("Level "… _MyLevel),0,0, “Bauhaus 93”, 140)
frgdGroup:insert(levelText)
levelText:setTextColor(255,255,255)
levelText.x = _W / 2
levelText.y = _H / 2
transitionStash.newTransition = transition.to(levelText, {time=1000, delay=1000, alpha=0, onComplete=shootListener})
local rnumb = math.random(2000,3500)
– insert code to deploy asteroids until _ToDestroy number has been met –
timer.performWithDelay(rnumb, deployAsteroid)
end
–Launch the game
launchGame()
–Pause Event Listeners
pausebtn:addEventListener(“touch”, pauseScreen)
Runtime:addEventListener(“collision”, onCollision)
localGroup:insert(bkgdGroup)
localGroup:insert(frgdGroup)
localGroup:insert(scoreGroup)
return localGroup
end[/lua] [import]uid: 83851 topic_id: 13987 reply_id: 53006[/import]