CODE HELP!

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 ) &nbsp; &nbsp;if (obj1 and obj2) then &nbsp; &nbsp; &nbsp; if(obj1.y\<obj2[1].boundary and obj1.y \> obj2[2].boundary) then &nbsp; &nbsp; &nbsp; if (obj1.multiplier==nil) then &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; obj1.multiplier=obj2[1].multiplier&nbsp; &nbsp; &nbsp; &nbsp; elseif(obj1.y\<obj2[2].boundary and obj1.y\>obj2[3].boundary) then &nbsp; &nbsp; &nbsp; if(obj1.multiplier==obj2[1].multiplier) then &nbsp; &nbsp; &nbsp; obj1:flash() &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; obj1.multiplier=obj2[2].multiplier &nbsp; &nbsp; &nbsp; elseif(obj1.y\<obj2[3].boundary and obj1.y\>-10) then &nbsp; &nbsp; &nbsp; &nbsp; if(obj1.multiplier==obj2[2].multiplier) then &nbsp; &nbsp; &nbsp; &nbsp; obj1:flash() &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; obj1.multiplier=obj2[3].multiplier &nbsp; &nbsp; &nbsp; &nbsp; elseif(obj1.y\<-10) then &nbsp; &nbsp; &nbsp; &nbsp; if(obj1.timer) then &nbsp; &nbsp; &nbsp; &nbsp; obj1:pop() &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp;end end function newBalloon(velocity) local sceneGroup=display.newGroup() local balloon=display.newCircle(sceneGroup,0,0,15) &nbsp; &nbsp; &nbsp;balloon:setFillColor(math.random(0, 255)/255, math.random(0, 255)/255, math.random(0, 255)/255) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; balloon.x=\_W/2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; balloon.y=\_H+20 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; balloon.xScale=1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; balloon.yScale=1 &nbsp; &nbsp; &nbsp; balloon.type="balloon" &nbsp; &nbsp; &nbsp; balloon.hit=false &nbsp; &nbsp; &nbsp; balloon.multiplier=nil &nbsp; &nbsp; &nbsp; balloon.points=1 &nbsp; &nbsp; &nbsp;balloon.tween={start={},finish={}} &nbsp; &nbsp; &nbsp;function balloon:flash() &nbsp; &nbsp; &nbsp; local xS=self.xScale &nbsp; &nbsp; &nbsp; local ys=self.yScale &nbsp; &nbsp; &nbsp; local factor=0.3 &nbsp; &nbsp; &nbsp; local function tweenBack() &nbsp; &nbsp; &nbsp; transition.to(self, {time=150, xScale=1, yScale=1}) &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; self.tween.finish=tweenBack &nbsp; &nbsp; &nbsp; self.tween.start=transition.to(self, {time=100, xScale=xS+factor, yScale=yS+factor,onComplete=self.tween.finish}) &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; &nbsp;function balloon:pop() &nbsp; &nbsp; &nbsp; state:dispatchEvent({name="change", state="pop"}) &nbsp; &nbsp; &nbsp; if(balloon.timer) then &nbsp; &nbsp; &nbsp; timer.cancel(balloon.timer) &nbsp; &nbsp; &nbsp; balloon.timer=nil end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --immediatly hide object &nbsp; &nbsp; &nbsp; balloon.isVisible=false &nbsp; &nbsp; &nbsp;--if these two properties exist they are set to nil &nbsp; &nbsp; &nbsp; if(balloon.tween.start) then &nbsp; &nbsp; &nbsp; balloon.tween.start=nil &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; if(balloon.tween.finish) then &nbsp; &nbsp; &nbsp; balloon.tween.finish=nil &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; balloon.tween=nil &nbsp; &nbsp; &nbsp; --Remove object &nbsp; &nbsp; &nbsp; balloon:removeSelf() &nbsp; &nbsp; &nbsp; balloon=nil &nbsp; &nbsp; &nbsp; collectgarbage("collect") &nbsp; end &nbsp; &nbsp; &nbsp; balloon.timer=timer.performWithDelay(20, function(event) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;checkThreshold(balloon, thresholds) &nbsp; &nbsp; &nbsp; end,-1) &nbsp; &nbsp; &nbsp; physics.addBody(balloon, "kinematic", {density=0, bounce=0, friction=0, radius=15}) &nbsp; &nbsp; &nbsp; balloon:setLinearVelocity(0, velocity\*math.random(-8,-2)) &nbsp; &nbsp; &nbsp; --in line above the 0 is velocity on x axis the "velocity\*-1" is velocity on y &nbsp; &nbsp; &nbsp; 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?!

It’s a bit hard to follow your code but it seems to me that you are removing the references to your balloons but not the actual objects. When you create any object in a composer scene, don’t forget to insert it into the main group that you define with _ self.view _. You can also insert those objects to other groups and insert that group into the main group.

Also, take a look at composer.removeHidden() and composer.removeScene() functions in API docs -> https://docs.coronalabs.com/api/library/composer/index.html

shadowknight1873

hi, please get back to me. I have a question I want to ask you

I  was just like you

I was just like you

Yes jirayu what is your question?

Bgmadclown I have included my balloon object into the sceneGroup.

First things first, you should remove all lines of

local sceneGroup = display.newGroup()

and insert just one at the top of that scene file without calling the newGroup function. I guess there’s 3 at the moment. Change it to this:

local sceneGroup -- preferrably at the top of file to keep the code clean local function scene:create(event) sceneGroup = self.view end

After doing so, you should also remove the lines where you manually remove sceneGroup and objects from this display group because Composer will handle those when you go to a new scene.

**** Remember that you should insert all the groups you created into this main group so Composer will take care of all of these groups.

sceneGroup:insert(exampleGroup)

When you go into a new scene remember to put this line into scene:show to remove all the scenes that are created before.

composer.removeHidden()

It’s a bit hard to follow your code but it seems to me that you are removing the references to your balloons but not the actual objects. When you create any object in a composer scene, don’t forget to insert it into the main group that you define with _ self.view _. You can also insert those objects to other groups and insert that group into the main group.

Also, take a look at composer.removeHidden() and composer.removeScene() functions in API docs -> https://docs.coronalabs.com/api/library/composer/index.html

shadowknight1873

hi, please get back to me. I have a question I want to ask you

I  was just like you

I was just like you

Yes jirayu what is your question?

Bgmadclown I have included my balloon object into the sceneGroup.

First things first, you should remove all lines of

local sceneGroup = display.newGroup()

and insert just one at the top of that scene file without calling the newGroup function. I guess there’s 3 at the moment. Change it to this:

local sceneGroup -- preferrably at the top of file to keep the code clean local function scene:create(event) sceneGroup = self.view end

After doing so, you should also remove the lines where you manually remove sceneGroup and objects from this display group because Composer will handle those when you go to a new scene.

**** Remember that you should insert all the groups you created into this main group so Composer will take care of all of these groups.

sceneGroup:insert(exampleGroup)

When you go into a new scene remember to put this line into scene:show to remove all the scenes that are created before.

composer.removeHidden()