–
– level1.lua
–
local composer = require( “composer” )
local scene = composer.newScene()
– include Corona’s “physics” library
local physics = require “physics”
physics.start(); physics.pause()
physics.setGravity( 0, 0 )
– forward declarations and other locals
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
function scene:create( event )
– 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.
local sceneGroup = self.view
– create a grey rectangle as the backdrop
local background = display.newRect( 0, 0, screenW, screenH )
background.anchorX = 0
background.anchorY = 0
background:setFillColor( .5 )
– make a crate (off-screen), position it, and rotate slightly
crate = display.newImageRect( “crate.png”, 30, 30 )
crate.x, crate.y = 100, 150
crate.rotation = 15
– add physics to the crate
physics.addBody( crate, { density=1.0, friction=0.3, bounce=0.3 } )
– create a grass object and add physics (with custom shape)
local grass = display.newImageRect( “grass.png”, screenW, 82 )
grass.anchorX = 0
grass.anchorY = 1
grass.x, grass.y = 0, display.contentHeight
– define a shape that’s slightly shorter than image bounds (set draw mode to “hybrid” or “debug” to see)
local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 }
physics.addBody( grass, “static”, { friction=0.3, shape=grassShape } )
– all display objects must be inserted into group
sceneGroup:insert( background )
sceneGroup:insert( grass)
sceneGroup:insert( crate )
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.
physics.start()
end
end
function createCrate( )
– body
if(crate.x>display.contentWidth)then
print( “crate gone” )
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.)
physics.stop()
elseif phase == “did” then
– Called when the scene is now off screen
composer.removeScene( “level1” )
end
end
function scene:destroy( event )
– 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.
local sceneGroup = self.view
package.loaded[physics] = nil
physics = nil
end
local tempBody
function touchListener(event)
local phase=event.phase
if phase == “began” then
print( phase )
tempBody = display.newCircle(event.x, event.y, 30)
tempBody.isVisible=false
physics.addBody(tempBody)
tempBody.isSensor = true
tempBody.joint = physics.newJoint(“touch”, tempBody, event.x, event.y)
function tempBody.enterFrame(event)
vx, vy = tempBody:getLinearVelocity()
crate:setLinearVelocity(vx, vy)
crate.angularVelocity = tempBody.angularVelocity
end
Runtime:addEventListener(“enterFrame”, tempBody)
elseif phase == “moved” then
tempBody.joint:setTarget(event.x, event.y)
elseif phase == “cancelled” or phase == “ended” then
Runtime:removeEventListener(“enterFrame”, tempBody)
tempBody.joint:removeSelf()
display.remove(tempBody)
end
end
local function finishGame( )
– body
Runtime:removeEventListener(“touch”,touchListener)
composer.removeScene( “level1” )
composer.gotoScene( “menu” )
end
Runtime:addEventListener(“touch”, touchListener)
Runtime:addEventListener(“enterFrame”, createCrate)
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
timer.performWithDelay( 5000, finishGame ,1 )
return scene