Skip to content

Instantly share code, notes, and snippets.

@omidahourai
Created June 27, 2013 19:13
Show Gist options
  • Save omidahourai/5879470 to your computer and use it in GitHub Desktop.
Save omidahourai/5879470 to your computer and use it in GitHub Desktop.
--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