Hi guys,
I’m attempting to create a game where the user has to destroy falling asteroids before they collide with the Earth. So far I have the asteroids spawning, and a rudimentary collision detection in place, along with the ability to update the score when the asteroids are touched.
But… I’m stuck on the endgame conditions. I can’t figure out how to restart or reset the game conditions. Any help will be appreciated.
I’d also like to be able to introduce extra levels where the asteroids fall at greater velocity/number. Any ideas? Please excuse my poorly written code.
[code]
module(…, package.seeall);
function new()
local Scene = display.newGroup();
local physics = require(“physics”);
physics.start();
physics.setGravity(0,1)
–physics.setDrawMode(“hybrid”)
local blast = audio.loadSound(“bomb1.mp3”)
–Set up some vars
local _score = 0;
local scoreLeftX=1000;
local collisionState=0
local AsteroidTable = {};
–Create groups
local background = display.newGroup();
local foreground = display.newGroup();
local GUI = display.newGroup();
local endLevel = display.newGroup();
–Insert groups into the Scene for the Director class
Scene:insert(background);
Scene:insert(foreground);
Scene:insert(GUI);
Scene:insert(endLevel);
–Background images
local bg_frame = display.newImageRect(“images/starfield.jpg”, 384,512);
bg_frame.x=_W * 0.5
bg_frame.y=_H * 0.5
bg_frame.alpha=0.9
background:insert(bg_frame);
local frame = display.newImageRect(“images/frame.png”, 384,512);
frame:setReferencePoint(displayCenterReferencePoint)
frame.x=_W * 0.5
frame.y=_H * 0.5
GUI:insert(frame);
local score_txt = display.newText(" ", 0, 0, native.systemFont, 16);
score_txt.x = 110; score_txt.y = (score_txt.height * 0.5) + 3;
local score = display.newText(" " … _score, 0, 0, native.systemFont, 16);
score:setReferencePoint(display.CenterLeftReferencePoint);
score.x = scoreLeftX; score.y = score_txt.y;
score_txt:setTextColor(255,255,255);
score:setTextColor(255,255,255)
GUI:insert(score_txt);
local wallLeft = display.newRect(0,0,120,_H+100)
wallLeft.x=-40
wallLeft.y=_H/2-100
wallLeft:setFillColor(0,0,0)
foreground:insert(wallLeft)
physics.addBody(wallLeft, “static”)
wallLeft.alpha=0.05
local wallRight = display.newRect(0,0,120,_H+100)
wallRight.x=_W+40
wallRight.y=_H/2-100
wallRight:setFillColor(0,0,255)
foreground:insert(wallRight)
physics.addBody(wallRight, “static”)
wallRight.alpha=0.05
------- SCORING -------------
local function addToScore()
if (collisionState==0) then
_score = _score + 1
score_txt.text=_score
end
end
local function onCircleCollision()
collisionState=collisionState+1
end
--------------- ASTEROIDS -------------------------
local imageTable = {“images/asteroid1.png”,“images/asteroid2.png”, “images/asteroid3.png”};
local function createAsteroid()
local a = m.random(1,3);
local indexCount = 0;
local asteroid = display.newImage(imageTable[a]);
asteroid.x = m.random(20, 320);
asteroid.y = -100
physics.addBody(asteroid, “dynamic”, {density = 1, friction = 1, bounce = 0, radius=25});
asteroid.index = indexCount + 1;
AsteroidTable[#AsteroidTable + 1] = asteroid;
foreground:insert(asteroid);
asteroid:setLinearVelocity(10, 100);
indexCount = indexCount + 1;
local function touched (e)
if e.phase == “began” then
table.remove(AsteroidTable, asteroid.index)
asteroid:removeSelf();
asteroid = nil
local explosion = display.newImageRect(“images/explode.png”, 50,50)
explosion.x=e.target.x
explosion.y=e.target.y
audio.play(blast)
local function destroyExplode()
–print(“bang”)
explosion:removeSelf()
end
timer.performWithDelay(100, destroyExplode)
return
end
if (collisionState==1) then
asteroid:removeEventListener(“touch”, touched);
asteroid:removeEventListener(“touch”, addToScore)
end
end
asteroid:addEventListener(“touch”, touched);
asteroid:addEventListener(“touch”, addToScore)
return asteroid;
end
asterTmr = timer.performWithDelay(500, createAsteroid, 20);
local earth = display.newImageRect(“images/earth.png”, 320,320)
earth:setReferencePoint(displayCenterReferencePoint)
physics.addBody(earth,“static”, {radius=160, friction=1})
earth.x = _W/2
earth.y = _H/2+300
foreground:insert(earth)
local function animateEarth( event )
earth.rotation = earth.rotation + .02
–print(“lovely”)
end
local displayLose = display.newImageRect(“images/losescreen.jpg”, 384, 512)
displayLose.x=_W/2
displayLose.y=_H/2
GUI:insert(displayLose)
displayLose.isVisible=false
local playAgain = display.newImageRect(“images/playagain.png”, 240,75)
playAgain.x=_W/2
playAgain.y=_H/2
GUI:insert(playAgain)
playAgain.isVisible=false
local function removeAll()
local blankScreen = display.newRect(0,0,384,512)
blankScreen.x=_W/2
blankScreen.y=_H/2
blankScreen.alpha=0.05
blankScreen:setFillColor(0,0,0)
endLevel:insert(blankScreen)
transition.to(blankScreen, {time=4000, alpha=1})
end
function checkState()
if (collisionState==1) then
physics.pause();
earth.isFixedRotation = true
displayLose.isVisible=true
playAgain.isVisible=true
local yourScore = display.newText("Your Score is "… _score, 0, 0, native.systemFont, 35);
yourScore.x=_W/2
yourScore.y=_H/2+70
yourScore:setTextColor(255,255,255)
GUI:insert(yourScore)
timer.performWithDelay(1000, removeAll)
–timer.performWithDelay(5000, function() director:changeScene (“menu”) end)
end
end
earth:addEventListener(“collision”,onCircleCollision)
Runtime:addEventListener( “enterFrame”, animateEarth )
Runtime:addEventListener( “enterFrame”, checkState )
return Scene
end
[/code] [import]uid: 92074 topic_id: 25728 reply_id: 325728[/import]