Game.lua file
local marquee=display.newGroup() marquee.alpha=0 marquee.isVisible=false local marquee\_bg=display.newRoundedRect(0,0, .5, .5, 2) marquee\_bg:setFillColor(140,140,140) local marquee\_you\_win=display.newText(marquee,"You Win!", 0, 0, "Komika Axis", 36) marquee\_you\_win.x=marquee\_bg.width\*.5+150 marquee\_you\_win.y=(marquee\_bg.height\*.5)+200 local marquee\_play\_again=display.newText(marquee," Play Again ",0,0, "Komika Axis", 20) marquee\_play\_again.x=marquee\_bg.width\*.5+150 marquee\_play\_again.y=(marquee\_bg.height\*.5)+230 function marquee:show( value ) if(value=="win") then marquee\_you\_win:write(" ".. "You Win!") elseif(value=="loss") then marquee\_you\_win:write(" ".."You Lose!") end self.isVisible=true transition.to(self,{time=150, alpha=1}) end function marquee:hide( ) local s=self local function hideit() s.isVisible=false state:dispatchEvent({name="change", state="new"}) end transition.to(self,{time=150, alpha=1, onComplete=hideit}) end function marquee\_you\_win:write( text ) self.text=text end function marquee:touch(event) if(event.phase=="ended" or event.phase=="cancelled") then for i=0, #balloons do display.remove(balloons[i]) --balloons[i]=nil table.remove(balloons, 5) end for i=1, #bullets do display.remove(bullets[i]) --bullets[i]=nil table.remove(bullets,-1) end event.target:hide() end end marquee:addEventListener("touch", marquee)
Game.lua file continued:
local function spawnBalloons( number ) local function spawn(event) local b=balloon.newBalloon(m.random(50,100)) balloons[b]=b balloons[b].x=m.random(\_W-260,\_W-60) balloons[b].remove=true if(event.count==number) then timer.cancel(tmr) tmr=nil end end tmr=timer.performWithDelay(2000, spawn, number) end local function updateScore( obj,number) obj.text=" ".. number obj.x=\_W-223 end --STATE MACHINE bullet.state=state balloon.state=state --State Machine to handle logic function state:change(event) if(event.state=="score") then if(event.multiplier~=0) then \_score=\_score+(event.points\*event.multiplier) updateScore(score, \_score) end --if no more bullets or balloons check score if(\_total\_bullets==0 or \_total\_balloons==0) then if(\_score\>=win\_th) then state:dispatchEvent({name="change",state="winloss", value="win"}) else state:dispatchEvent({name="change", state="winloss", value="loss"}) end --otherwise, they have balloons and bullets, let them keep playing else if(\_score\>=win\_th) then state:dispatchEvent({name="change", state="winloss", value="win"}) end end elseif(event.state=="new") then --[[ for i=sceneGroup.numChildren, 1, -1 do if(sceneGroup[i].remove) then if(sceneGroup[i].timer) then timer.cancel(sceneGroup[i].timer) end sceneGroup[i].remove=nil sceneGroup[i]:removeSelf() sceneGroup[i]=nil collectgarbage("collect") end end --]] composer.gotoScene("restart") initVars({bullets=7,balloons=5,score=0}) elseif(event.state=="winloss") then if(tmr) then timer.cancel(tmr) tmr=nil end bullet.ready=false marquee:show(event.value) elseif(event.state=="pop") then \_total\_balloons=\_total\_balloons-1 if(\_total\_balloons==0) then state:dispatchEvent({name="change", state="score", points=0, multiplier=0}) end elseif(event.state=="fire") then \_total\_bullets=\_total\_bullets-1 if(\_total\_bullets==0) then state:dispatchEvent({name="change",state="score", points=0, multiplier=0}) end end end --Create listener for state changes state:addEventListener("change",state) function initVars( params) \_total\_bullets=params.bullets \_total\_balloons=params.balloons \_score=params.score end function startGame(params) updateScore(score,\_score) bullet.ready=true spawnBullets(params.bullets) spawnBalloons(params.balloons) end startGame({bullets=\_total\_bullets,balloons=\_total\_balloons, \_score=0}) end function scene:show( event ) local sceneGroup = display.newGroup() end function scene:hide( event ) local sceneGroup = display.newGroup() if ( phase == "will" ) then elseif ( phase == "did" ) then end end function scene:destroy( event ) display.remove(sceneGroup) display.remove(marquee) display.remove(balloon) display.remove(bullet) display.remove(txt3) display.remove(txt2) display.remove(txt1) display.remove(bullets) display.remove(balloons) display.remove(myLine) display.remove(state) table.remove(m)
Balloon.lua file
module(...,package.seeall) thresholds={} state={} pop={} local function checkThreshold(obj1,obj2 ) if (obj1 and obj2) then if(obj1.y\<obj2[1].boundary and obj1.y \> obj2[2].boundary) then if (obj1.multiplier==nil) then end obj1.multiplier=obj2[1].multiplier elseif(obj1.y\<obj2[2].boundary and obj1.y\>obj2[3].boundary) then if(obj1.multiplier==obj2[1].multiplier) then obj1:flash() end obj1.multiplier=obj2[2].multiplier elseif(obj1.y\<obj2[3].boundary and obj1.y\>-10) then if(obj1.multiplier==obj2[2].multiplier) then obj1:flash() end obj1.multiplier=obj2[3].multiplier elseif(obj1.y\<-10) then if(obj1.timer) then obj1:pop() end end end end function newBalloon(velocity) local sceneGroup=display.newGroup() local balloon=display.newCircle(sceneGroup,0,0,15) balloon:setFillColor(math.random(0, 255)/255, math.random(0, 255)/255, math.random(0, 255)/255) balloon.x=\_W/2 balloon.y=\_H+20 balloon.xScale=1 balloon.yScale=1 balloon.type="balloon" balloon.hit=false balloon.multiplier=nil balloon.points=1 balloon.tween={start={},finish={}} function balloon:flash() local xS=self.xScale local ys=self.yScale local factor=0.3 local function tweenBack() transition.to(self, {time=150, xScale=1, yScale=1}) end self.tween.finish=tweenBack self.tween.start=transition.to(self, {time=100, xScale=xS+factor, yScale=yS+factor,onComplete=self.tween.finish}) end function balloon:pop() state:dispatchEvent({name="change", state="pop"}) if(balloon.timer) then timer.cancel(balloon.timer) balloon.timer=nil end --immediatly hide object balloon.isVisible=false --if these two properties exist they are set to nil if(balloon.tween.start) then balloon.tween.start=nil end if(balloon.tween.finish) then balloon.tween.finish=nil end balloon.tween=nil --Remove object balloon:removeSelf() balloon=nil collectgarbage("collect") end balloon.timer=timer.performWithDelay(20, function(event) checkThreshold(balloon, thresholds) end,-1) physics.addBody(balloon, "kinematic", {density=0, bounce=0, friction=0, radius=15}) balloon:setLinearVelocity(0, velocity\*math.random(-8,-2)) --in line above the 0 is velocity on x axis the "velocity\*-1" is velocity on y return balloon end
My game works like this. Balloons float up and i have five bullets and there are 5 balloons that come up and i have to shoot my bullet at them. If i run out of bullets the game ends and i lose even if not all the balloons have floated up. The game also ends if all 5 balloons have floated to the top of the screen. All works fine but then i restart my game when i click the marquee and i have five bullets again the balloons from the previous game seem to still be thee but just invisible. This makes the game end prematurely because say that i ran out of bullets and two balloons were floating up, and then i restart the game, those two balloons will count for the number of balloons that hit the top. Since only five balloons can hit the top in a new game those two old balloons already count for 2/5 balloons when they hit the top. So only three other balloons can come from the new game and float up until the game ends. Please help, how can i remove all the balloons upon restarting the game?!