I want to do a simple platform game. I have a few questions:
- Please tip me a better way to jump - I know my code is bad ;(
- Why my player sometimes slides around the board? Should not do that …
Test this code and help me please…
thank you
e.
[lua]-- Setup
display.setStatusBar(display.HiddenStatusBar)
system.activate(“multitouch”)
local physics = require( “physics” )
physics.start()
physics.setGravity(0, 20)
local sprite = require(“sprite”)
– Background
gameFriction = 2;
– Test Level
local level = display.newRect(0, 300, 480, 20)
physics.addBody( level, “static”, { friction = gameFriction, bounce = 0 } )
level:setFillColor( 200, 0, 0)
local platform = display.newRect(100, 230, 200, 20)
physics.addBody( platform, “static”, { friction = gameFriction, bounce = 0 } )
platform:setFillColor( 200, 0, 0)
local platform2 = display.newRect(230, 170, 120, 20 )
physics.addBody( platform2, “static”, { friction = gameFriction, bounce = 0 } )
platform2:setFillColor( 200, 0, 0)
local platform3 = display.newRect(340, 110, 120, 20 )
physics.addBody( platform3, “static”, { friction = gameFriction, bounce = 0 } )
platform3:setFillColor( 200, 0, 0)
local platform4 = display.newRect(0, 0, 20, 400 )
physics.addBody( platform4, “static”, { friction = gameFriction, bounce = 0 } )
platform4:setFillColor( 200, 0, 0)
local platform5 = display.newRect(460, 0, 20, 400 )
physics.addBody( platform5, “static”, { friction = gameFriction, bounce = 0 } )
platform5:setFillColor( 200, 0, 0)
– Player
local playerBody = { density = 0, friction = 0, bounce = 0, radius = 15 }
local alien = display.newRect(100, 100, 30, 30)
alien:setFillColor( 0, 200, 0)
alien.jump = 0; alien.move = 0
physics.addBody( alien, playerBody )
alien.isFixedRotation = true
function alienMain( event )
local this = event.target
alien.x = alien.x + (alien.move * 2)
end
local function alienLeft( event )
local this = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( this , event.id )
this.isFocus = true
alien.move = -1
Runtime:addEventListener(“enterFrame”, alienMain )
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( this , nil )
this.isFocus = false
alien.move = 0
Runtime:removeEventListener( “enterFrame”, alienMain )
end
end
local function alienRight( event )
local this = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( this , event.id )
this.isFocus = true
alien.move = 1
Runtime:addEventListener(“enterFrame”, alienMain )
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( this , nil )
this.isFocus = false
alien.move = 0
Runtime:removeEventListener( “enterFrame”, alienMain )
end
end
local t = {}
function t:timer( event )
alien.jump = 0
timer.cancel( event.source )
end
local function alienJump( event )
local this = event.target
local phase = event.phase
if “began” == phase and alien.jump == 0 then
display.getCurrentStage():setFocus( this , event.id )
this.isFocus = true
alien:applyLinearImpulse( 0, -0.08, alien.x, alien.y )
alien.jump = 1
timer.performWithDelay( 1000, t, 0 )
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( this , nil )
this.isFocus = false
end
end
– LEFT BUTTON
local leftBtn = display.newCircle( 60, 250, 30 )
leftBtn:setFillColor(128,128,128)
leftBtn.alpha = 0.3
leftBtn:addEventListener( “touch”, alienLeft )
– RIGHT BUTTON
local rightBtn = display.newCircle( 140, 250, 30 )
rightBtn:setFillColor(128,128,128)
rightBtn.alpha = 0.3
rightBtn:addEventListener( “touch”, alienRight )
– JUMP BUTTON
local jumpBtn = display.newCircle( 410, 250, 30 )
jumpBtn:setFillColor(128,128,128)
jumpBtn.alpha = 0.3
jumpBtn:addEventListener( “touch”, alienJump )[/lua]
[import]uid: 9171 topic_id: 4327 reply_id: 304327[/import]