Tutorial
[lua]-----------------------------------------------------------------------------------------
– tutorial.lua
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)
local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()
– Boundaries
ceiling = display.newRect (0, screenH - 350, screenW, 0)
floor = display.newRect (0, screenH + 30, screenW, 0)
– Make Walls Physical
physics.addBody (ceiling, “static”, {friction = .2, bounce = .1, filter = ceilingCollisionFilter})
physics.addBody (floor, “static”, {friction = .2, bounce = .1, floorCollisionFilter})
function scene:createScene(event)
local rocketCollisionFilter = {categoryBits = 1, maskBits = 2}
local missile1CollisionFilter = {categoryBits = 2, maskBits = 1}
local missile2CollisionFilter = {categoryBits = 2, maskBits = 1}
local ceilingCollisionFilter = {categoryBits = 3, maskBits = 0}
local floorCollisionFilter = {categoryBits = 3, maskBits = 0}
local screenGroup = self.view
– Background Images
background1 = display.newImage(“background.png”)
background1:setReferencePoint(display.BottomLeftReferencePoint)
background1.x = 0
background1.y = 320
background1.speed = 10
screenGroup:insert(background1)
background2 = display.newImage(“background.png”)
background2:setReferencePoint(display.BottomLeftReferencePoint)
background2.x = 480
background2.y = 320
background2.speed = 10
screenGroup:insert(background2)
– Rocket
rocket = display.newImage(“rocket.png”)
rocket.x = 100
rocket.y = 160
physics.addBody(rocket, “dynamic”, {density = .1, friction = .5, radius = 25, filter = rocketCollisionFilter})
screenGroup:insert(rocket)
– Missiles
missile1 = display.newImage(“missile.png”)
missile1.x = 500
missile1.y = 100
missile1.speed = math.random(4,12)
missile1.initY = missile1.y
missile1.amp = math.random(1,100)
missile1.angle = math.random(1,360)
physics.addBody(missile1, “static”, {density = .1, bounce = .1, friction = .2, filter = missile1CollisionFilter})
screenGroup:insert(missile1)
missile2 = display.newImage(“missile.png”)
missile2.x = 500
missile2.y = 250
missile2.speed = math.random(7,15)
missile2.initY = missile2.y
missile2.amp = math.random(1,100)
missile2.angle = math.random(1,360)
physics.addBody(missile2, “static”, {density = .1, bounce = .1, friction = .2, filter = missile2CollisionFilter})
screenGroup:insert(missile2)
end
function scrollField(self,event)
if self.x < -475 then
self.x = 475
else
self.x = self.x - self.speed
end
end
function moveMissile(self,event)
if self.x < -50 then
self.x = 500
self.y = math.random(20,300)
self.speed = math.random(2,5)
self.amp = math.random(20,50)
self.angle = math.random(1,200)
else
self.x = self.x - self.speed
self.angle = self.angle + .1
self.y = self.amp * math.sin(self.angle)+self.initY
end
end
function activateRocket(self,event)
self:applyForce(0, -18, self.x, self.y)
end
function onTouch(event)
if event.phase == “began” then
rocket.enterFrame = activateRocket
Runtime:addEventListener(“enterFrame”, rocket)
end
if event.phase == “ended” then
Runtime:removeEventListener(“enterFrame”, rocket)
end
end
function onCollision(event)
if event.phase == “began” then
storyboard.gotoScene(“level1”, “fade”, 400)
end
end
function scene:enterScene(event)
background1.enterFrame = scrollField
Runtime:addEventListener(“enterFrame”, background1)
background2.enterFrame = scrollField
Runtime:addEventListener(“enterFrame”, background2)
missile1.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile1)
missile2.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile2)
Runtime:addEventListener(“touch”, onTouch)
Runtime:addEventListener(“collision”, onCollision)
end
function scene:exitScene(event)
Runtime:removeEventListener(“enterFrame”, background1)
Runtime:removeEventListener(“enterFrame”, background2)
Runtime:removeEventListener(“enterFrame”, missile1)
Runtime:removeEventListener(“enterFrame”, missile2)
Runtime:removeEventListener(“touch”, onTouch)
Runtime:removeEventListener(“collision”, onCollision)
end
function scene:destroyScene(event)
end
scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)
return scene [import]uid: 20272 topic_id: 34859 reply_id: 138722[/import]