Hello kind folks, have problem with accessing direct element from table by touching it, can you help me with that?
Here’s my code
--# Main
-- Like 2048
displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
cells = {}
--some var's
touchi = false
touching = false
--instances of classes
field = Field()
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
field:draw()
-- This sets the line thickness
-- Do your drawing here
end
function touched(t)
touchi = t
if t.state == BEGAN or t.state == MOVING then
touching = true
end
end
--# Field
Field = class()
function Field:init(x)
--sum, calculated from two random numbers in table
self.sum = 0
--2d table to make my field
self.time = 0
self.width = WIDTH
for x = 1,4 do
cells[x] = {}
for y = 1,4 do
--fill table with random number
cells[x][y] = math.random(-20,20)
--calculating
self.sum = cells[math.random(x)][math.random(y)] + cells[math.random(x)][math.random(y)]
end
end
end
function Field:draw()
-- Codea does not automatically call this method
pushStyle()
background(255,225,196)
fill(255,207,161)
rectMode(CENTER)
rect(WIDTH/2+5,HEIGHT/2-60,530,530)
popStyle()
--there you have 10 seconds to think
self.time = self.time + DeltaTime
self.width = WIDTH - self.time*100
if self.width<0 then
self.time = 0
self.width = WIDTH
end
pushStyle()
fill(255, 0, 0, 255)
rectMode(CENTER)
rect(WIDTH/2,20,self.width,10)
popStyle()
for k,v in pairs(cells) do
for a,b in pairs(cells[k]) do
pushStyle()
fill(255,142,30)
rect(130+k*130,-60+a*130,1024/8.5,1024/8.5)
popStyle()
pushStyle()
fill(255, 255, 255, 255)
font("AmericanTypewriter-Bold")
fontSize(50)
text(b,190+k*130,a*130)
popStyle()
pushStyle()
popStyle()
fill(0, 0, 0, 255)
font("ArialRoundedMTBold")
fontSize(130)
text(self.sum,WIDTH/2,HEIGHT-100)
popStyle()
--another sum, which store values of touched cells
local sum
sum = 0
if CurrentTouch.x < 220+k*130 and CurrentTouch.x > 170+k*130 and CurrentTouch.y < a*130+20 and CurrentTouch.y > a*130-20 then
--there i need sum up touched cells, but i have no idea, how can i solve this
end
end
end
end
function Field:touched(touch)
-- Codea does not automatically call this method
end