Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local director = require("director")
- local physics = require("physics")
- physics.start()
- local playGame = true;
- local localGroup
- localGroup = display.newGroup()
- local background = display.newImage("clouds.png")
- background.x = display.contentWidth/2; background.y = display.contentHeight/2;
- localGroup:insert(background)
- local score = display.newText( "Score: 0", 10, 25, nil, 24 )
- score:setTextColor( 255,255,255 )
- localGroup:insert(score)
- scoreSet = 0
- local floor = display.newRect(10,25,600,50)
- floor.x = display.contentWidth/2; floor.y = 368
- floor.name = "floor" -- This sets the name of the floor to later be used in collision functions
- localGroup:insert(floor)
- physics.addBody(floor, "static")
- local paddle = display.newRect(0,0, 150, 75)
- paddle.x = display.contentWidth/2; paddle.y = 325
- paddle.name = "paddle" -- This sets the name of the paddle to later be used in collision functions
- localGroup:insert(paddle)
- physics.addBody(paddle, "kinematic")
- local function onCrateCollision( self, event ) -- This is called when the crate collides with anything
- if ( event.phase == "began" ) then -- This is here to make sure its only called once at the start of the collision to avoid errors
- if ( event.other.name == "floor" or event.other.name == "paddle" ) then -- This checks to make sure its either the floor, or a paddle the crate is colliding with
- if (playGame == true) then
- hitSound = audio.loadSound("BEEPMULT.wav")
- audio.play(hitSound)
- self:removeSelf() -- Delete the crate
- self = nil
- end
- end
- end
- end
- local function onPickupCollision( self, event )
- if ( event.phase == "began" ) then
- if ( event.other.name == "paddle" or event.other.name == "floor" ) then
- if playGame == true then
- pickUpSound = audio.loadSound("BEEPFM.wav")
- audio.play(pickUpSound)
- self:removeSelf()
- end
- end
- end
- end
- function newCrate()
- j = display.newImage("crate.png");
- j.x = 30 + math.random(400)
- j.y = -100
- print("spawn")
- localGroup:insert(j)
- physics.addBody( j, { density=0.9, friction=0.3, bounce=0.3} )
- j.collision = onCrateCollision -- This sets it so the onCrateCollision function above is attachesd to the crate object ( j )
- j.name = "crate" -- This line names the crate so we can identify it later in collisions
- j:addEventListener( "collision", j ) -- j the object ( crate )now has a event listener for when it collised. We set j.collision to onCrateCollision above so we can just call j as the last object here
- end
- function newPickup()
- rand = math.random(1000)
- if (rand <100 ) then
- b = display.newImage("star.png");
- b.x = 30 + math.random(400)
- b.y = -100
- b:rotate(math.random(360))
- localGroup:insert(b)
- physics.addBody(b, { density = 0.9, friction = 0.3, bounce = 0.3, radius = 20} )
- b.collision = onPickupCollision
- b.name = "star"
- b:addEventListener( "collision", b )
- end
- end
- local function drag(event) -- The following function is used for dragging the paddle around and updating its position
- local t = event.target
- local phase = event.phase
- if phase == "began" then
- local parent = t.parent
- parent:insert ( t )
- display.getCurrentStage():setFocus( t )
- t.isFocus = true;
- t.x0 = event.x - t.x
- elseif t.isFocus then
- if phase == "moved" then
- t.x = event.x - t.x0
- elseif phase == "ended" or phase == "cancelled" then
- display.getCurrentStage():setFocus( nil )
- t.isFocus = false
- end
- end
- end
- -- Event Listeners --
- paddle:addEventListener( "touch", drag ) -- This will call the drag function when the paddle is touched
- floor:addEventListener( "collision", floor ) -- this will call the floor:collision function below if anything colides with the floor
- paddle:addEventListener( "collision", paddle ) -- this will call the paddle:collision function below if anything colides with the paddle
- local dropCrates = timer.performWithDelay( 200, newCrate, 0 ) -- Sets the timer to drop crates
- local dropPickup = timer.performWithDelay ( 500, newPickup, 0 )
- function onAlert(event)
- if ( event.action == "clicked" ) then
- local i = event.index
- if i == 2 then
- print("yes!")
- director:changeScene("main", "crossfade")
- elseif i == 1 then
- print("no!")
- director:changeScene("game", "crossfade")
- end
- end
- end
- -- Check for Collisions --
- function paddle:collision (event) -- This function is called whenever any object collides with the paddle
- if event.other.name == "crate" then -- This checks to make sure the object collided is a crate (the below is stuff u added)
- paddle:removeEventListener("touch", drag)
- paddle:removeEventListener("collision", paddle )
- floor:removeEventListener("collision", floor)
- local alert = native.showAlert( "OH NO!", "YOU LOST, SORRY!",
- { "Retry", "Menu" }, onAlert)
- end
- if event.other.name == "star" then
- scoreSet = scoreSet + 5
- score.text = "Score: " .. scoreSet
- end
- end
- function floor:collision (event) -- This function is called whenever any object collides with the floor
- if ( event.other.name == "crate" ) then -- This checks to make sure the object collided is a crate . If so we then add to the score and update the score text
- print("yo")
- scoreSet = scoreSet +1 -- Add one to the current score
- score.text = "Score: " .. scoreSet -- Display the updated score
- end
- end
- function new()
- return localGroup
- end
Advertisement
Add Comment
Please, Sign In to add comment