Game Development Reference
In-Depth Information
Creating an Elevator
You might come across a scenario while making a game where the character has to ride an elevator
to move to a different level. Though this is conceptually very simple, it's important to understand the
specifics.
For this example, we shall have four objects: the up and down buttons, the platform, and the
character/player. For simplicity, as shown in Figure 8-17 , we will use rectangles. When the program
starts, the character, which is a physics body, will fall, and the platform will absorb the fall and
prevent the object from bouncing. The buttons will move the platform up and down.
local physics = require("physics")
physics.start()
display.setStatusBar(display.HiddenStatusBar)
local beam = display.newRect(50,300,200,50)
physics.addBody(beam,"static",{friction=1,density=1,bounce=0})
beam:setFillColor(0,255,0)
local box = display.newRect(120,120,40,40)
physics.addBody(box,{friction=1})
box:setFillColor(255,0,0)
function moveUp()
box.bodyType="static"
transition.to( box,
{time=300, y=-100, delta = true,
onComplete=function() box.bodyType="dynamic" end})
transition.to(beam,{time=300,y=-100, delta=true})
end
function moveDown()
box.bodyType="static"
transition.to( box,
{time=300, y=100, delta=true,
onComplete=function() box.bodyType="dynamic" end})
transition.to(beam,{time=300,y=100, delta=true})
end
local btnUp = display.newRect(10,10,20,40)
local btnDn = display.newRect(10,60,20,40)
btnDn:addEventListener("tap",moveDown)
btnUp:addEventListener("tap",moveUp)
 
Search WWH ::




Custom Search