touch for table objects

Hello, i’m learning Lua since 2 days and trying to write a game.

I’ve created the table of 7x7x6 objects and i’m trying to allow the user to touch each of them.

Firstly i succesfully writed code which allows me to touch and move two different objects:

local file1 = "green\_ball\_32.png" local file2 = "yellow\_ball\_32.png" local example = display.newImage( file1, 0, 0 ) local example2 = display.newImage( file2, 0, 40 ) local function choosenone( event ) ... -- choosing object and move it -- ... end example:addEventListener("touch", choosenone) example2:addEventListener("touch", choosenone)

 and it’s working, i can touch theese balls and move them through the screen

My problem is that i’m creating 3-dimensional table 7x7x6 with objects and i don’t know how to allow each one of them to be touched

local file1 = "green\_ball\_32.png" local file2 = "yellow\_ball\_32.png" local file3 = "red\_ball\_32.png" local file4 = "blue\_ball\_32.png" local file5 = "black\_ball\_32.png" local file6 = "pink\_ball\_32.png" showball = {} -- create the matrix for i=1,7 do showball[i] = {} -- create a new row for j=1,7 do showball[i][j] = {} -- create a new row showball[i][j][1] = display.newImage( file1, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][1].xScale = 0.8; showball[i][j][1].yScale = 0.8 showball[i][j][2] = display.newImage( file2, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][2].xScale = 0.8; showball[i][j][2].yScale = 0.8 showball[i][j][3] = display.newImage( file3, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][3].xScale = 0.8; showball[i][j][3].yScale = 0.8 showball[i][j][4] = display.newImage( file4, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][4].xScale = 0.8; showball[i][j][4].yScale = 0.8 showball[i][j][5] = display.newImage( file5, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][5].xScale = 0.8; showball[i][j][5].yScale = 0.8 showball[i][j][6] = display.newImage( file6, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][6].xScale = 0.8; showball[i][j][6].yScale = 0.8 showball[i][j][1].isVisible = true showball[i][j][2].isVisible = false showball[i][j][3].isVisible = false showball[i][j][4].isVisible = false showball[i][j][5].isVisible = false showball[i][j][6].isVisible = false end end

I’ve tried two ways, both don’t work and showing me errors

--showball[1][1][1]:addEventListener("touch", choosenone) --showball:addEventListener("touch", choosenone)

Can somebody help me and teach how i can solve my problem?

Best regards

Lucas

it works for me

--showball[1][1][1]:addEventListener("touch", choosenone) --showball:addEventListener("touch", choosenone)

This not works for me, its shows error whenever im using the first line or the second one

6gyd.png

The first line works for me.  Where are you calling it?

showball = {} -- create the matrix for i=1,7 do showball[i] = {} -- create a new row for j=1,7 do showball[i][j] = {} -- create a new row showball[i][j][1] = display.newImage( file1, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][1].xScale = 0.8; showball[i][j][1].yScale = 0.8 showball[i][j][2] = display.newImage( file2, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][2].xScale = 0.8; showball[i][j][2].yScale = 0.8 showball[i][j][3] = display.newImage( file3, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][3].xScale = 0.8; showball[i][j][3].yScale = 0.8 showball[i][j][4] = display.newImage( file4, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][4].xScale = 0.8; showball[i][j][4].yScale = 0.8 showball[i][j][5] = display.newImage( file5, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][5].xScale = 0.8; showball[i][j][5].yScale = 0.8 showball[i][j][6] = display.newImage( file6, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][6].xScale = 0.8; showball[i][j][6].yScale = 0.8 showball[i][j][1].isVisible = true showball[i][j][2].isVisible = false showball[i][j][3].isVisible = false showball[i][j][4].isVisible = false showball[i][j][5].isVisible = false showball[i][j][6].isVisible = false end end local function choosenone() print("click") end showball[1][1][1]:addEventListener("touch", choosenone)

Ah right, i was calling it right after declaring the table so it was before declaring the choosenone function.

Also the all table is working now, i’ve written :

for i=1,7 do for j=1,7 do for k=1,6 do showball[i][j][k]:addEventListener("touch", choosenone) end end end

Thank you Mr. JonPM  :slight_smile:

it works for me

--showball[1][1][1]:addEventListener("touch", choosenone) --showball:addEventListener("touch", choosenone)

This not works for me, its shows error whenever im using the first line or the second one

6gyd.png

The first line works for me.  Where are you calling it?

showball = {} -- create the matrix for i=1,7 do showball[i] = {} -- create a new row for j=1,7 do showball[i][j] = {} -- create a new row showball[i][j][1] = display.newImage( file1, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][1].xScale = 0.8; showball[i][j][1].yScale = 0.8 showball[i][j][2] = display.newImage( file2, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][2].xScale = 0.8; showball[i][j][2].yScale = 0.8 showball[i][j][3] = display.newImage( file3, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][3].xScale = 0.8; showball[i][j][3].yScale = 0.8 showball[i][j][4] = display.newImage( file4, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][4].xScale = 0.8; showball[i][j][4].yScale = 0.8 showball[i][j][5] = display.newImage( file5, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][5].xScale = 0.8; showball[i][j][5].yScale = 0.8 showball[i][j][6] = display.newImage( file6, 13+(i-1)\*43, 30+j\*39.8 ) showball[i][j][6].xScale = 0.8; showball[i][j][6].yScale = 0.8 showball[i][j][1].isVisible = true showball[i][j][2].isVisible = false showball[i][j][3].isVisible = false showball[i][j][4].isVisible = false showball[i][j][5].isVisible = false showball[i][j][6].isVisible = false end end local function choosenone() print("click") end showball[1][1][1]:addEventListener("touch", choosenone)

Ah right, i was calling it right after declaring the table so it was before declaring the choosenone function.

Also the all table is working now, i’ve written :

for i=1,7 do for j=1,7 do for k=1,6 do showball[i][j][k]:addEventListener("touch", choosenone) end end end

Thank you Mr. JonPM  :slight_smile: