2d element access

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

First of all, I would draw all your cells to an image in memory (using setContext) as part of setup, then in draw, you just sprite that image, which is much faster.

Second, is it possible to touch more than one cell at once? If not, then you can calculate which cell has been touched, using CurrentTouch. Or is the problem harder than that?

As Ignatz says… but also in your current code you’d assuming the touch can touch multiple (which it can’t…) move the local sum and sum = 0 outside the loop.

In the if do sum = sum + b (b being the inner value) or alternately sum = sum + cells[k][a]

@ignatz player touch 2 cells in series . For example : 35 ( number on top) = 15+10 . So player first touch cell with 15 , then with 10 . Or 10+15

@spacemonkey thank you, i have missed this thing

@lupino8211 I have some projects that access table elements from a touch point. Here’s a stripped down version of one of my projects. Maybe you could use this, or see what I’m doing and modify your code. Tap each square and its value will print at the top of the screen. You can modify the size of the squares, the number of squares per side, and the x,y offset of the whole square to center it on the screen.

EDIT: Added code to show Touched value and a Touched total. Touch outside of square to reset the Touched total.


displayMode(FULLSCREEN)

function setup()
    fontSize(30)
    squareSize=100  -- size of each square
    rectSize=5  -- squares per side
    offX=100    -- move squares right or left
    offY=30     -- move squares up or down
    tab={}
    for z=1,rectSize^2 do
        table.insert(tab,math.random(-20,20))
    end
    tot=0
end

function draw()
    background(40, 40, 50)
    count=0
    for x=1,rectSize do
        for y=1,rectSize do
            count=count+1
            fill(255)
            rect(offX+x*squareSize,offY+y*squareSize,
                    squareSize,squareSize)
            fill(255,0,0)
            text(tab[count],offX+x*squareSize+squareSize/2,
                    offY+y*squareSize+squareSize/2)
        end
    end
    if val~=nil then
        text("Touched value  "..tab[val],WIDTH/2,HEIGHT-50)
    end
    text("Touched total  "..tot,WIDTH/2,HEIGHT-100)
end

function touched(t)
    if t.state==BEGAN then
        x=math.floor((t.x-offX)/squareSize)
        y=math.floor((t.y-offY)/squareSize)
        val=(x-1)*rectSize+y
        if x<1 or x>rectSize or y<1 or y>rectSize then
            val=nil
            tot=0
        else
            tot=tot+tab[(x-1)*rectSize+y]
        end
    end    
end

@dave1707 Thank you alot!

@dave1707 after analyzing your code this thing i cant understand , what do these piece of code

 x=math.floor((t.x-offX)/squareSize)
        y=math.floor((t.y-offY)/squareSize)
        val=(x-1)*rectSize+y

@lupino8211 If I have a 5x5 set of squares, the “x=” and “y=” code gives me a value from 1 to 5 for x and 1 to 5 for y of which square I touched. The “val =” code converts the x and y values to a value from 1 to 25 so I can get the value from the table that shows on each square.

@dave1707 thank you for explaining , now its clear for me

@dave1707 to don’t create a separate thread Il ask here: how can i store two values of one variable in different time moments? I mean i have var “sum” there

sum = cells[val]+cells[val]

There “val” is different numbers. For example : first user tapped cell and now

val = 3 

then user tapped another cell and now

val = 15 

so i need

sum = cells[3]+cells[15] 

And is there any solution without table?

@lupino8211 I changed my code above to show the Touched value and the Touched total. To reset the Touched total, touch anywhere outside of the squares.

thank you,@dave1707 , i knew what the solution was on the surface