add character movement acceleration/deceleration
This commit is contained in:
parent
3ecd48d11d
commit
6850f2af25
|
@ -159,18 +159,44 @@ do -- Player
|
|||
isFlipped=false
|
||||
}
|
||||
|
||||
local maxSp=2
|
||||
local accel=0.3
|
||||
local decel=0.5
|
||||
|
||||
function Player:direct()
|
||||
if btn(2) then
|
||||
self.vx = -2
|
||||
self.isFlipped = true
|
||||
self:goLeft()
|
||||
elseif btn(3) then
|
||||
self.vx = 2
|
||||
self.isFlipped = false
|
||||
else self.vx = 0
|
||||
self:goRight()
|
||||
else self:stop()
|
||||
end
|
||||
if btnp(4) and self.vy == 0 then self.vy = j end
|
||||
end
|
||||
|
||||
function Player:goLeft()
|
||||
if self.vx > 0 then self.vx=0 end
|
||||
self.vx=self.vx - accel
|
||||
if self.vx < -maxSp then self.vx=-maxSp end
|
||||
self.isFlipped = true
|
||||
end
|
||||
|
||||
function Player:goRight()
|
||||
if self.vx < 0 then self.vx=0 end
|
||||
self.vx=self.vx + accel
|
||||
if self.vx > maxSp then self.vx=maxSp end
|
||||
self.isFlipped = false
|
||||
end
|
||||
|
||||
function Player:stop()
|
||||
if self.vx < 0 then
|
||||
self.vx= self.vx + decel
|
||||
if self.vx > 0 then self.vx=0 end
|
||||
elseif self.vx > 0 then
|
||||
self.vx= self.vx - decel
|
||||
if self.vx < 0 then self.vx=0 end
|
||||
end
|
||||
end
|
||||
|
||||
function Player:update()
|
||||
self:resolve()
|
||||
self.x = self.x + self.vx
|
||||
|
|
Loading…
Reference in a new issue