Skip to content

Instantly share code, notes, and snippets.

@omidahourai
Created June 27, 2013 19:15
Show Gist options
  • Save omidahourai/5879477 to your computer and use it in GitHub Desktop.
Save omidahourai/5879477 to your computer and use it in GitHub Desktop.
function character:move()
--Localized variables and function calls
local mCos, mSin = math.cos, math.sin
local group = self.group
--Closure enterFrame
local function onEachFrame(event)
local speed = self.speed or 0
local angle = self.angle or 0
local xval = mCos(angle) * speed
local yval = -mSin(angle) * speed
group:translate(xval, yval)
end
Runtime:addEventListener('enterFrame', onEachFrame)
end
function character:jump()
local power = self.jumpPower --Let's say it's 3
local airTime = self.airTime --Let's say 1000 ms
local fpms = 60/1000 --Frames per ms = 0.06
local totFrames = airTime * fpms --Total frames (= 60)
local curFrame = 1 --Current frame (starting at 1)
local function onEachFrame(event)
if (curFrame <= totFrames) then
curFrame = curFrame +1
--use parabolic equation: y = -4 * (x-0.5)^2 +1
local progress = curFrame / totFrames
local x = progress-0.5
local dy = (4 * (x*x) +1) * power
group:translate(0,dy)
else
--Jump finished, stop running onEachFrame
Runtime:removeEventListener('enterFrame', onEachFrame)
end
end
Runtime:addEventListener('enterFrame', onEachFrame)
end
function character:touch(event)
if (event.phase == 'began') then
self:jump()
end
end
--start moving and listening for jumps!
character:move()
Runtime:addEventListener('touch', character)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment