I am creating a balloon burst game check this code the score doesn’t increases according to rectangles which i have created and balloons are not rising properly from bottom right corner and i want to add time to that game if time rans out then game over file shows…please some one help me this is my code.
---------game.lua
local composer = require( “composer” )
local scene = composer.newScene()
local widget = require “widget”
local BackBtn
local function onBackBtnRelease()
– go to game.lua scene
composer.gotoScene( “menu”, “fade”, 800 )
–audio.pause(music)
BackBtn:removeSelf()
– balloon:removeSelf()
–arrow:removeSelf()
–score_txt:removeSelf()
return true – indicates successful touch
end
– forward declarations and other locals
function scene:create( event )
local sceneGroup = self.view
local physics=require(“physics”);
physics.start();
–Classes
– local balloon=require(“balloon”);
– Called when the scene’s view does not exist.
–
– INSERT code here to initialize the scene
– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc.
–Set up some vars
– We need physics started to add bodies, but we don’t want the simulaton
– running until the scene is on the screen.
function newBalloon()
local balloon= display.newImage(“images/balloon_red.png”);
balloon.xScale=0.2; balloon.yScale=0.2;
–balloon:setFillColor(255,0,0);
local v1,v2
v1=math.random(W/2)
v2=math.random(H+20)
balloon.x=v1; balloon.y=v2;
–Set up custom properties
–balloon.type=“balloon”;
–balloon.hit= false;
–balloon.multiplier=nil;
–balloon.points=1;
physics.addBody(balloon, “kinematic”,{density=0, bounce=0, friction=0,radius=40});
balloon:setLinearVelocity(0,-200);
–return balloon
end
timer.performWithDelay(“800”,newBalloon,0)
– create a grey rectangle as the backdrop
– the physical screen will likely be a different shape than our defined content area
– since we are going to position the background from it’s top, left corner, draw the
– background at the real top, left corner.
local music= audio.loadStream(“media/gamesound.mp3”);
audio.play(music);
local background = display.newImage( “images/background.jpg” );
local score_txt= display.newText(“Score:”,40,30,“Royalacid”,20)
score_txt:setFillColor(0,0,0);
local RR = display.newRoundedRect(160,90,300,80,3);
RR:setFillColor(255,0,0);
local RR2 = display.newRoundedRect( 160,190,300,120,2 )
RR2:setFillColor(0,128,0);
local RR3 = display.newRoundedRect( 160,340,300,180,2 )
RR3:setFillColor(0,255,255);
local txt= display.newText(“10x”,260,80,“Shaka Pow”,20);
txt:setFillColor(255,255,255);
txt.xScale= 1.3; txt.yScale=1.3;
local txt2= display.newText(“5x”,260,170,“Shaka Pow”,20);
txt2:setFillColor(255,255,255);
txt2.xScale= 1.3; txt2.yScale=1.3;
local txt3= display.newText(“1x”,260,290,“Shaka Pow”,20);
txt3:setFillColor(255,255,255);
txt3.xScale= 1.3; txt3.yScale=1.3;
– create a widget button (which will loads level1.lua on release)
BackBtn = widget.newButton{
label=“Back”,
labelColor = { default={1,0,0}, over={0} },
default=“button.png”,
over=“button-over.png”,
width=154, height=40,
onRelease = onBackBtnRelease
– event listener function
}
BackBtn.x = display.contentCenterX
BackBtn.y = display.contentHeight - 20
BackBtn.xScale=1.3; BackBtn.yScale=1.3;
– all display objects must be inserted into group
sceneGroup:insert( background )
sceneGroup:insert(RR)
sceneGroup:insert(RR2)
sceneGroup:insert(RR3)
sceneGroup:insert(txt)
sceneGroup:insert(txt2)
end
– function new()
– local Scene= display.newGroup();
– local physics=require(“physics”);
– physics.start();
– -- --Set up some vars
– -- local thresholds={};
– -- local balloons={};
– local _score=0;
– -- local scoreLeftX=160;
– -- --Classes
– local balloon=require(“balloon”);
– --Create groups
– local background2=display.newGroup();
– local foreground=display.newGroup();
– local GUI=display.newGroup();
– --Insert groups into the scene for composer
– Scene:insert(background2);
– Scene:insert(foreground);
– Scene:insert(GUI);
– --Background images
– local bg_frame=display.newImage();
– GUI:insert(bg_frame);
– thresholds[1]= display.newRect(0,0,768,525);
– thresholds[2]= display.newRect(0,0,768,298);
– thresholds[3]= display.newRect(0,0,768,201);
– thresholds[1]=setFillColor(66,159,255);
– thresholds[2]=setFillColor(0,183,0);
– thresholds[3]=setFillColor(183,0,0);
– thresholds[1].x= W/2; thresholds[1].y= H-(thresholds[1].height *0.5);
– thresholds[2].x= W/2; thresholds[2].y=( H-thresholds[1].height)- (thresholds[2].height *0.5);
– thresholds[3].x= W/2; thresholds[3].y= 0+(thresholds[3].height *0.5);
– local t1x=display.newImage();
– t1x:setReferencePoint(display.CenterRightReferencePoint)
– local t5x=display.newImage();
– t1x:setReferencePoint(display.CenterRightReferencePoint)
– local t10x=display.newImage();
– t1x:setReferencePoint(display.CenterRightReferencePoint)
– t1x.x = (W*0.5) + 300;
– t5x.x = t1x.x;
– t10x.x = t1x.x;
– t1x.y = 730;
– t5x.y = 350;
– t10x.y = 135;
– background:insert(thresholds[1]);
– background:insert(thresholds[2]);
– background:insert(thresholds[3]);
– foreground:insert(t1x);
– foreground:insert(t5x);
– foreground:insert(t10x);
– local score_txt=display.newText(“SCORE”,0,0,“Shaka Pow”,36);
– score_txt.x=(score_txt.width*0.5) +20; score_txt.y=(score_txt.height*0.5) +4;
– local score=display.newText("" … _score,0,0,“Shaka Pow”,36);
– score:setReferencePoint(display.CenterLeftReferencePoint);
– score.x=scoreLeftX; score.y=score_txt.y;
– score_txt:setTextColor(0,0,0);
– score:setTextColor(0,0,0);
– GUI:insert(score_txt);
– --Set up thresholds and multipliers
– for i=1,#thresholds do
– thresholds[i].boundary= math.ceil(thresholds[i].y +(thresholds[i].height* 0.5));
– --Multipliers
– if( i==1 ) then
– thresholds[i].multiplier= 1;
– else
– thresholds[i].multiplier= (i-1)* 5;
– end
– end
– --Feed the thresholds into the balloon class
– balloon.thresholds= thresholds;
– --Create some boundries
– --A ground for the canon balls to collide against
– local ceiling =display.newRect(0,0,W,5);
– ceiling.y=0 - ceiling.height*0.5;
– ceiling.type=“wall”;
– local ground =display.newRect(0,0,W,30);
– ground.y=H - ground.height* 0.5;
– ground.type= “wall”;
– --Two walls
– local leftwall= display.newRect(0,0,6,H);
– leftwall.x=0 - leftwall.width* 0.5;
– leftwall.type= “wall”;
– local rightwall= display.newRect(0,0,6,H);
– rightwall.x= W+ rightwall.width* 0.5;
– rightwall.type= “wall”;
– --Add physics bodies
– physics.addBody(ceiling,“static”);
– physics.addBody(ground,“static”);
– physics.addBody(leftwall,“static”);
– physics.addBody(rightwall,“static”);
– --insert into groups
– foreground:insert(ceiling);
– foreground:insert(ground);
– foreground:insert(leftwall);
– foreground:insert(rightwall);
– local function spawnBalloons(number)
– local function spawn(e)
– --Create an instance of a balloon
– --and assign it a random linear velocity
– local b= balloon.newBalloon(m.random(50,100));
– --Store the balloon instance in the balloons table
– --using the table number as the index
– balloons[b]=b;
– balloons[b].x=m.random(W*0.5,(W*0.5) * 1.75);
– --Flag the balloon for removal later
– balloons[b].remove = true;
– --insert the balloon into the foreground group
– foreground:insert(balloons[b]);
– --cancel and nill timer when done
– if(e.count==number) then
– timer.cancel(tmr);
– tmr=nil;
– end
– end
– tmr=timer.performWithDelay(2000,spawn,number);
– end
– spawnBalloons(5);
– return Scene
– end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then
– Called when the scene is now on screen
–
– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc.
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == “will” then
– Called when the scene is on screen and is about to move off screen
–
– INSERT code here to pause the scene
– e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == “did” then
– Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
– Called prior to the removal of scene’s “view” (sceneGroup)
–
– INSERT code here to cleanup the scene
– e.g. remove display objects, remove touch listeners, save state, etc.
if BackBtn then
BackBtn:removeSelf() – widgets must be manually removed
BackBtn = nil
end
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
–game.lua
– H= display.viewableContentHeight;
– W= display.viewableContentWidth;
– local composer= require(“composer”)
– local scene= composer.newScene()
–local function gotoMenu()
–composer.gotoScene(“menu”,{effect=“fade”,time=500})
–end
– function scene:create(event)
– local sceneGroup= self.view
– sceneGroup:insert(“bg”)
– end
– function scene:show(event)
– local sceneGroup= self.view
– local phase= event.phase
– if(phase== “will”) then
–local background= display.newImage(“images/background.jpg”,W,H);
– bg:setFillColor(255,5,0);
– bg.x= W*0.5; bg.y= H*0.5;
–background.alpha= 0.9;
– elseif(phase== “did”) then
– timer.performwithDelay(3000,showScene1)
– end
–background:addEventListener(“Touch”, gotoMenu);
–end
–return scene
and this is arrow file code i want to add this in game.lua file…
local physics=require (“physics”)
physics.start()
local force_multiplier=10;
local myLine = nil;
local function newarrow()
local arrow= display.newRect(0,0,60,60);
arrow.x=100; arrow.y=1600;
arrow.xScale=0.5; arrow.yScale=0.5;
arrow.type=“arrow”;
–Set up properties
physics.addBody(arrow, “kinematic”, {density=0.2, friction=0.2, bounce=0.5, radius=20})
arrow.linearDamping=0.3;
arrow.angularDamping=0.8;
arrow.isarrow=true
arrow.isSensor=true;
function arrow:touch(e)
local t=e.target
if(e.phase==“began”) then
display.getCurrentStage():setFocus(t)
self.isFocus=true
self.bodyType=“kinematic”;
–Stop current motion
self:setLinearVelocity(0,0)
self.angularVelocity=0
myLine=nil
elseif(self.isFocus) then
if(e.phase==“moved”) then
if(myLine) then
myLine.parent:remove(myLine)
end
myLine = display.newLine(self.x, self.y, e.x, e.y)
myLine:setStrokeColor(255,255,255,50)
myLine.strokeWidth=8
elseif(e.phase==“ended” or e.phase==“cancelled”) then
display.getCurrentStage():setFocus(nil)
self.isFocus=false
audio.play(shot);
if(myLine) then
myLine.parent:remove(myLine)
end
–Launch the arrow
self.bodyType=“dynamic”;
self:applyForce((self.x - e.x)*force_multiplier, (self.y - e.y)*force_multiplier, self.x,self.y);
– --wait a second to give the arrow time to hit something
– self.timer=timer.performWithDelay(1000, function(e)
– state:dispatchEvent({name=“change”, state=“fire”});
– if(e.count==1) then
– timer.cancel(self.timer);
– self.timer=nil;
– end
– end, 1)
end
end
end
arrow:addEventListener(“touch”, arrow);
end
newarrow()