Created
June 27, 2013 19:13
-
-
Save omidahourai/5879470 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--WITHOUT CLOSURE | |
function character:enterFrame(event) | |
local speed = self.speed or 0 | |
local angle = self.angle or 0 | |
local xval = math.cos(angle) * speed | |
local yval = -math.sin(angle) * speed | |
self.group:translate(xval, yval) | |
end | |
Runtime:addEventListener('enterFrame', character) | |
--WITH CLOSURE | |
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 | |
character:move() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment