add event listener .id to a board

Hello…

I have a set of images that I can identify with an .id number

when I make a loop like this

&nbsp;&nbsp;&nbsp; local pNo = 1 &nbsp;&nbsp;&nbsp; for i = 1, 16 do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; p[i] = display.newImageRect ( sceneGroup, staff1[i], 106, 106) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;p[i].x = pin[pNo].x &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;p[i].y = pin[pNo].y &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;p[i]:scale (escaleX, escaleY) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;p[i].id = pNo --\<------- RIGHT HERE &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;p[i]:addEventListener( "touch", listenerOne ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;pNo = pNo + 1 &nbsp;&nbsp;&nbsp; end

This is in a HORIZONTAL way one ROW

QUESTION


If I have a table like a board.

Horizontal and vertical

I use for i and for j

How do I add the event listener and the .id to the <for i> and the <for j>?

So each of the squares has a unique .id number

&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; local tiles = {} &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;function createBoard()&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for i = 1, 4 do&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for j = 1, 4 do&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;tiles[i] = display.newRect(800, 800, 40, 40) &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;tiles[i]:setFillColor(0, 0, 1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;tiles[i].x = 50\*(i-1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;tiles[i].y = 50\*(j-1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;createBoard()

Thanks for all your help

Victor

@Victor,

I’ll assume you want to add a touch listener:

local function onTouch( self, event ) if(event.phase == "began" ) then print("Touched: ", self.myID ) end return true end local tiles = {} function createBoard() for i = 1, 4 do for j = 1, 4 do local aTile = display.newRect(800, 800, 40, 40) tiles[i] = aTile -- For efficient instead of multiple lookups. aTile:setFillColor(0, 0, 1) aTile.x = 50\*(i-1) aTile.y = 50\*(j-1) aTile.myID = (i-1) \* 4 + j -- not sure where your ids are coming from, so I did this aTile.touch = onTouch aTile:addEventListener( "touch" ) end end end createBoard()
1 Like

Notice that I used the field ‘myID’ and not ‘id’.  ‘id’ is too generic and likely to collide with an existing field name or one that could be added to display objects in the future, whereas, ‘myID’ is likely to be collision free.

When I say collision I mean over-riding an existing field because the names are the same (collide).

Thanks for you help, that works really well.

Now

I have this function

&nbsp;&nbsp;&nbsp; function green5jlistener( event ) &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if event.phase == "began" then &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if ( event.target.id ) == 1 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("1") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 2 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("2") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 3 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("3") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 4 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("4") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 5 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("5") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 6 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("6") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 7 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("7") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 8 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("8") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 9 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("9") &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return true&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp;&nbsp; end

I use to do things to the tiles

if tile is 3 do this, but if it’s 15 do something else

But if I have 100 tiles or 200

I have to type a really long function…

if it’s 1 …

if it’s 2 …

if its’ 3 …

really long.

how can I have something like detecting where people “touch”

like

hereIsX = eventGetTheX( ) – tell me where people is touching

hereIsY = eventGetTheY( )

and then on my images have some function

if you touch in this image change the alpha or do something

but if you touch this other image do this thing…

I know it’s kind of confusing, but that’s how I am

I hope you understand what i am trying to do.

Victor

How many questions were in that last post?  Please use titles or numbers or something.  I want to answer, but have limited time to figure out the question.  

As a rule of thumb, “If you spend more time posting the question than someone else spends answering it, you’ve done the right thing.” 

Also, it is much preferred that you ask one question per forum post or it because much less useful as a future reference for others.

Unique Actions Per Tile

I think your first question could be summed up as, “how do I do a unique action per tile or set of tiles?”

(At least) Two possibilities exist:

A. Create a table of functions and do a lookup:

-- This example assumes a maximum of 4 objects for brevity -- local testFunc1( self ) print("BOBBY", self.myID ) end local testFunc2( self ) print("BILLY", self.myID ) end local testFunc3( self ) print("BUD", self.myID ) end local actions = {} actions[1] = testFunc1 actions[2] = testFunc2 actions[3] = testFunc3 actions[4] = testFunc1 -- A simple touch listener that dereferences the action local function onTouch( self, event ) if( event.phase == "began" ) local myFunc = actions[self.myID] myFunc( self ) end return end

B. Assign functions directly to the objects:

local testFunc1( self ) print("BOBBY", self.myID ) end local testFunc2( self ) print("BILLY", self.myID ) end local testFunc3( self ) print("BUD", self.myID ) end local actions = {} actions[1] = testFunc1 actions[2] = testFunc2 actions[3] = testFunc3 -- A simple touch listener that dereferences the action local function onTouch( self, event ) if( event.phase == "began" ) self:myAction() end return end -- Basic object builder loop to demonstrate principle+ for i = 1, 100 do local tmp = display.newCircle( someX, someY, someRadius ) -- pseudo-code tmp.myID = i tmp.myAction = actions[i%3+1] tmp.touch = onTouch tmp:addEventListener("touch") end

Detecting Touch Location

This is in the event as specified in the documentation.  https://docs.coronalabs.com/daily/api/event/touch/index.html

More?

Not sure if there was more to the last post…

Thanks… for the tips… I learned something new.

And you are right I have a lot of questions. Last post was very confusing, but you

gave me a great answer in the B example.


QUESTION 1 –

Could you explain this more … tmp.myAction = actions[i%3+1] …?

I have never seen this … % …

I had the circles and each gives me a different number ( 1 to 100 ) This is great!

but it only gives me 3 different actions ( BILLY, BUD, BOBBY)


QUESTION 2 –

If I want each circle to do something different,

do I need to type 100 functions?


And final question for this…

I can access an object like this

local rr = { } --\< I create this table for i = 1, 4 do &nbsp;&nbsp; rr[i] = display.newCircle( 200, 200, 100 ) -- pseudo-code &nbsp;&nbsp; rr[i].x = 100&nbsp; + i\*500 &nbsp;&nbsp; rr[i].y = 100 end rr[2].y = 200 --\<-- and move it later HERE

but in your example

for i = 1, 100 do &nbsp;&nbsp; tmp = display.newCircle( 200, 200, 30 ) -- pseudo-code &nbsp;&nbsp; tmp.x = 100 &nbsp;&nbsp; tmp.y = 100 + i\*60 &nbsp;&nbsp; tmp.myID = i &nbsp;&nbsp; tmp.myAction = actions[i%3+1] &nbsp;&nbsp; tmp.touch = onTouch &nbsp;&nbsp; tmp:addEventListener("touch") end tmp[3].x = 400 tmp[3].y = 400

I don’t see the … tmp[i]

I don’t see the … local tmp = { }

so trying this … tmp[3].x = 400 is not working.

QUESTION 3 –

How can I access an object later in the code, using your B example?

\\\\\\\\\

And believe me, I really appreciate all the time you take to help me

and so many other that will read this later. If it wasn’t for all your help

I would never be able to make apps

Thank you one more time…

Victor

@Victor,

I’ll assume you want to add a touch listener:

local function onTouch( self, event ) if(event.phase == "began" ) then print("Touched: ", self.myID ) end return true end local tiles = {} function createBoard() for i = 1, 4 do for j = 1, 4 do local aTile = display.newRect(800, 800, 40, 40) tiles[i] = aTile -- For efficient instead of multiple lookups. aTile:setFillColor(0, 0, 1) aTile.x = 50\*(i-1) aTile.y = 50\*(j-1) aTile.myID = (i-1) \* 4 + j -- not sure where your ids are coming from, so I did this aTile.touch = onTouch aTile:addEventListener( "touch" ) end end end createBoard()

Notice that I used the field ‘myID’ and not ‘id’.  ‘id’ is too generic and likely to collide with an existing field name or one that could be added to display objects in the future, whereas, ‘myID’ is likely to be collision free.

When I say collision I mean over-riding an existing field because the names are the same (collide).

Thanks for you help, that works really well.

Now

I have this function

&nbsp;&nbsp;&nbsp; function green5jlistener( event ) &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if event.phase == "began" then &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if ( event.target.id ) == 1 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("1") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 2 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("2") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 3 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("3") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 4 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("4") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 5 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("5") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 6 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("6") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 7 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("7") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 8 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("8") &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.target.id ) == 9 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("9") &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return true&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp;&nbsp; end

I use to do things to the tiles

if tile is 3 do this, but if it’s 15 do something else

But if I have 100 tiles or 200

I have to type a really long function…

if it’s 1 …

if it’s 2 …

if its’ 3 …

really long.

how can I have something like detecting where people “touch”

like

hereIsX = eventGetTheX( ) – tell me where people is touching

hereIsY = eventGetTheY( )

and then on my images have some function

if you touch in this image change the alpha or do something

but if you touch this other image do this thing…

I know it’s kind of confusing, but that’s how I am

I hope you understand what i am trying to do.

Victor

How many questions were in that last post?  Please use titles or numbers or something.  I want to answer, but have limited time to figure out the question.  

As a rule of thumb, “If you spend more time posting the question than someone else spends answering it, you’ve done the right thing.” 

Also, it is much preferred that you ask one question per forum post or it because much less useful as a future reference for others.

Unique Actions Per Tile

I think your first question could be summed up as, “how do I do a unique action per tile or set of tiles?”

(At least) Two possibilities exist:

A. Create a table of functions and do a lookup:

-- This example assumes a maximum of 4 objects for brevity -- local testFunc1( self ) print("BOBBY", self.myID ) end local testFunc2( self ) print("BILLY", self.myID ) end local testFunc3( self ) print("BUD", self.myID ) end local actions = {} actions[1] = testFunc1 actions[2] = testFunc2 actions[3] = testFunc3 actions[4] = testFunc1 -- A simple touch listener that dereferences the action local function onTouch( self, event ) if( event.phase == "began" ) local myFunc = actions[self.myID] myFunc( self ) end return end

B. Assign functions directly to the objects:

local testFunc1( self ) print("BOBBY", self.myID ) end local testFunc2( self ) print("BILLY", self.myID ) end local testFunc3( self ) print("BUD", self.myID ) end local actions = {} actions[1] = testFunc1 actions[2] = testFunc2 actions[3] = testFunc3 -- A simple touch listener that dereferences the action local function onTouch( self, event ) if( event.phase == "began" ) self:myAction() end return end -- Basic object builder loop to demonstrate principle+ for i = 1, 100 do local tmp = display.newCircle( someX, someY, someRadius ) -- pseudo-code tmp.myID = i tmp.myAction = actions[i%3+1] tmp.touch = onTouch tmp:addEventListener("touch") end

Detecting Touch Location

This is in the event as specified in the documentation.  https://docs.coronalabs.com/daily/api/event/touch/index.html

More?

Not sure if there was more to the last post…

Thanks… for the tips… I learned something new.

And you are right I have a lot of questions. Last post was very confusing, but you

gave me a great answer in the B example.


QUESTION 1 –

Could you explain this more … tmp.myAction = actions[i%3+1] …?

I have never seen this … % …

I had the circles and each gives me a different number ( 1 to 100 ) This is great!

but it only gives me 3 different actions ( BILLY, BUD, BOBBY)


QUESTION 2 –

If I want each circle to do something different,

do I need to type 100 functions?


And final question for this…

I can access an object like this

local rr = { } --\< I create this table for i = 1, 4 do &nbsp;&nbsp; rr[i] = display.newCircle( 200, 200, 100 ) -- pseudo-code &nbsp;&nbsp; rr[i].x = 100&nbsp; + i\*500 &nbsp;&nbsp; rr[i].y = 100 end rr[2].y = 200 --\<-- and move it later HERE

but in your example

for i = 1, 100 do &nbsp;&nbsp; tmp = display.newCircle( 200, 200, 30 ) -- pseudo-code &nbsp;&nbsp; tmp.x = 100 &nbsp;&nbsp; tmp.y = 100 + i\*60 &nbsp;&nbsp; tmp.myID = i &nbsp;&nbsp; tmp.myAction = actions[i%3+1] &nbsp;&nbsp; tmp.touch = onTouch &nbsp;&nbsp; tmp:addEventListener("touch") end tmp[3].x = 400 tmp[3].y = 400

I don’t see the … tmp[i]

I don’t see the … local tmp = { }

so trying this … tmp[3].x = 400 is not working.

QUESTION 3 –

How can I access an object later in the code, using your B example?

\\\\\\\\\

And believe me, I really appreciate all the time you take to help me

and so many other that will read this later. If it wasn’t for all your help

I would never be able to make apps

Thank you one more time…

Victor