Skip to content

Instantly share code, notes, and snippets.

@runekaagaard
Created March 27, 2011 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runekaagaard/889385 to your computer and use it in GitHub Desktop.
Save runekaagaard/889385 to your computer and use it in GitHub Desktop.
local background = display.newImage( "grass.png" )
local screen = {
top = display.screenOriginY,
bottom = display.viewableContentHeight + display.screenOriginY,
left = display.screenOriginX,
right = display.viewableContentWidth + display.screenOriginX,
}
screen.width = screen.right - screen.left
local plane = {
dx = 0,
dy = 0,
gravity = 0.01,
x = display.contentWidth * 0.5,
y = display.contentHeight * 0.5,
obj = display.newImage( "plane.png", display.contentWidth * 0.5, display.contentHeight * 0.5 - 40),
}
local buttons = {
buttons = {},
num_of_buttons = 3
}
buttons.create = function()
width = screen.width - 10
for i=1,buttons.num_of_buttons do
b = display.newRoundedRect(
screen.left + screen.width * (i -1) / 3 + 5,
screen.bottom - width / 3,
width / 3 - 5,
width / 3 - 5,
10
)
buttons.paint(b)
b.l = b.x - b.width / 2
b.r = b.x + b.width / 2
b.t = b.y - b.height / 2
b.b = b.y + b.height / 2
b.set_Color = function(b, r, g, b)
b.setFillColor(r, g, b)
end
buttons.buttons[i] = b
end
end
buttons.paint = function(button)
button:setFillColor(255, 0, 0)
button.strokeWidth = 6
button:setStrokeColor(200, 200, 200, 255)
end
--< >
buttons.checkForTouch = function(e)
for k,b in pairs(buttons.buttons) do
b.isPressed = e.x < b.r and
e.x > b.l and
e.y > b.t and
e.y < b.b
if b.isPressed then
b:setFillColor(0, 255, 0)
else
b:setFillColor(255, 0, 0)
end
end
end
buttons.cancelPresses = function(e)
for k,b in pairs(buttons.buttons) do
b.isPressed = false
b:setFillColor(255, 0, 0)
end
end
local function onTouch(event)
if event.phase == 'began' or event.phase == 'moved' then
buttons.checkForTouch(event)
end
if event.phase == 'ended' then
buttons.cancelPresses()
end
end
local function onEnterFrame()
noneButtonsPressed = true
for k,b in pairs(buttons.buttons) do
if b.isPressed then
noneButtonsPressed = false
if k == 1 then plane.obj.rotation = plane.obj.rotation - 10 end
if k == 2 then plane.obj.rotation = plane.obj.rotation + 10 end
if k == 3 then
rot = math.rad(plane.obj.rotation + 270)
plane.dx = plane.dx + math.cos(rot) / 2
plane.dy = plane.dy + math.sin(rot) / 2
end
end
end
if noneButtonsPressed then
plane.dx = plane.dx * 0.96
plane.dy = plane.dy * 0.96
end
plane.obj:translate(plane.dx, plane.dy)
end
-- Initialize
buttons.create()
Runtime:addEventListener("touch", onTouch)
Runtime:addEventListener("enterFrame", onEnterFrame);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment