Created
November 13, 2013 14:56
-
-
Save Circussmith/7450391 to your computer and use it in GitHub Desktop.
Grid button
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
--# Button | |
Button = class() | |
function Button:init() | |
-- you can accept and set parameters here | |
btnTab = {} | |
txt = false | |
chBtn = false | |
self.btnSize = vec2(102,77) | |
self:makeButton() | |
end | |
function Button:draw() | |
-- Codea does not automatically call this method | |
for i,v in ipairs (btnTab) do | |
pushStyle() | |
spriteMode(CORNER) | |
x = v.x * self.btnSize.x | |
y = v.y * self.btnSize.y | |
sprite(v.img,x,y,v.size.x,v.size.y) | |
end | |
if txt == true then | |
fill(255, 255, 255, 255) | |
fontSize(40) | |
text("It Worked!",WIDTH/2,HEIGHT/2) | |
end | |
end | |
function Button:makeButton() | |
fImg = readImage("Tyrian Remastered:Arrow Left") | |
bImg = readImage("Tyrian Remastered:Arrow Right") | |
table.insert(btnTab,{x = 2,y = 2,isButton = true,img = fImg,size = self.btnSize,btnDoes = function() txt=false end}) | |
table.insert(btnTab,{x = 6, y = 2,img = bImg,size = self.btnSize,btnDoes = function() txt = true end}) | |
end | |
function Button:checkButton(w,h) | |
print(w,h) | |
for i,v in ipairs(btnTab) do | |
if v.x == w and v.y == h then | |
v.btnDoes() | |
end | |
end | |
end | |
function Button:touched(touch) | |
-- Codea does not automatically call this method | |
if touch.state == BEGAN then | |
chBtn = true | |
end | |
if chBtn == true and touch.state == ENDED then | |
bx = touch.x/self.btnSize.x | |
by = touch.y/self.btnSize.y | |
z = math.random(bx,bx) | |
d = math.random(by,by) | |
chBtn = false | |
self:checkButton(z,d) | |
end | |
end | |
--# Main | |
-- button grid | |
displayMode(FULLSCREEN) | |
-- Use this function to perform your initial setup | |
function setup() | |
button = Button() | |
end | |
-- This function gets called once every frame | |
function draw() | |
-- This sets a dark background color | |
background(40, 40, 50) | |
button:draw() | |
-- This sets the line thickness | |
strokeWidth(5) | |
-- Do your drawing here | |
end | |
function touched(touch) | |
button:touched(touch) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment