I am just getting my feet wet with Codea and don’t fully grasp the concept of creating separate classes and passing information where the code refers to nnnnn.self etc.
I have written my simple method of zone detection on a 5 x 5 matrix of squares. It provide the correct output, however looking at other examples it looks like I need to get my head round other concepts before I try to take my idea further.
Anyone who has any suggestion as to how the code should be written, feel free to comment. All the code below exists in the main routine.
---- Touch Detection
– Use this function to perform your initial setup
function setup()
print(“Hello World!”)
end
– This function gets called once every frame
function draw()
background(40, 40, 50)
strokeWidth(5)
noFill()
x = -5
y = -5
z = 0
regiona = {}
regionb = {}
regionc = {}
regiond = {}
for j = 1,5 do
y = y + 105
for i = 1,5 do
x = x + 105
rect(x,y,100,100)
z = z + 1
regiona[z] = x + 20
regionb[z] = y + 20
regionc[z] = x + 80
regiond[z] = y + 80
end
x = -5
end
end
function touched(touch)
if touch.state == ENDED then
print("x = "..touch.x.." y = "..touch.y)
text(touch.x,touch.x,touch.y)
--check touch zones
z = 0
for z=1,25 do
if touch.x > regiona[z] and touch.x < regionc[z] and touch.y > regionb[z] and touch.y < regiond[z] then
print("zone "..z)
break
end
end
end
end