Adding an eventListener to an image in a table

Here is my code:

[lua]

local bgimage=display.newImageRect(“images/black.png”, 360, 570)

bgimage.x = display.contentCenterX

bgimage.y = display.contentCenterY

 

local pattern={

{0,1,1,1,1,1,1},

{0,1,0,0,1,0,0},

{0,1,0,0,1,0,0},

{0,1,0,0,1,0,0},

{0,1,0,0,0,0,0},

{0,1,0,0,0,0,0},

{0,1,0,0,0,0,0},

{0,1,0,0,0,0,0},

{0,1,0,0,0,0,0}}

 

function pattern1()

for row = 1, 8 do

for col = 1, 9 do

local image = display.newImageRect(“images/white.png”, 22,21)

image.x=math.floor(-10 + (31.3 * row))

image.y=math.floor(-10 + (35 * col))

local tf = display.newText(’’, 0, 0, ‘Arial’, 19)

tf:setTextColor(0, 0, 0)

tf.width = 22

tf.height = 21

tf.x = math.floor(-10 + (31.3 * row))

tf.y = math.floor(-10 + (35 * col))

– Change 0’s to Random Letters 

 

if(pattern[col][row] == 0) then

image:removeSelf()

image=nil

else

tf.text=“0”

 

end

 

end

end

 

end

 

pattern1()

local bgimage=display.newImageRect(“images/black.png”, 360, 570)

bgimage.x = display.contentCenterX

bgimage.y = display.contentCenterY

 

local pattern={

{0,1,1,1,1,1,1},

{0,1,0,0,1,0,0},

{0,1,0,0,1,0,0},

{0,1,0,0,1,0,0},

{0,1,0,0,0,0,0},

{0,1,0,0,0,0,0},

{0,1,0,0,0,0,0},

{0,1,0,0,0,0,0},

{0,1,0,0,0,0,0}}

 

function pattern1()

for row = 1, 8 do

for col = 1, 9 do

local image = display.newImageRect(“images/white.png”, 22,21)

image.x=math.floor(-10 + (31.3 * row))

image.y=math.floor(-10 + (35 * col))

local tf = display.newText(’’, 0, 0, ‘Arial’, 19)

tf:setTextColor(0, 0, 0)

tf.width = 22

tf.height = 21

tf.x = math.floor(-10 + (31.3 * row))

tf.y = math.floor(-10 + (35 * col))

– Change 0’s to Random Letters 

 

if(pattern[col][row] == 0) then

image:removeSelf()

image=nil

else

tf.text=“0”

 

end

 

end

end

 

end

 

pattern1()

[/lua]

This post can stay, it’s may be content that should have been posted on the other thread. I however have removed the other two duplicates of this.

Forum rules ask that you only ask your question once. We understand that there are sometimes hiccups and in particular first time posters may not notice that your first post is held until moderation.

All is good now. But be aware of the rule going forward. If you need to review the forum rules, there is a link in the right side bar on the forum index page.

Rob

@MajinZepar, The code markup button is <> in blue.

Please write the following:

A question: The title is not enough. What are you looking to achieve?

The problem: What error messages did you get to the console? How did you expect your code supposed to respond compared to how it actually responded?

Debugging: What have you tried to resolve the problem?

To place an addEventListener for the images, place it in the for loop area where you create your images.

what im trying to do is when you the click the image, the text above it will change into a blank, 0, or 1. I already put an eventListener to all of the images. But my problem is, how will I know which image I click and which text will I change?

do refer to @sdktester comment if you want help.  A piece of your code (that in no way covers touch events) and a wish does not a proper forum post make!

pointer for you a touch event will expose the element that raised the event.  i.e.  event.target  will tell you the image clicked.

sure you can work the rest out

I purposely removed the codes for the touch event. And I also tried the event.target  and print it out the console and it only says the table, is there any other way to know what image I clicked?

event.target IS the clicked object.  Objects are tables… what was you expecting?

Another choice might be to;  right after you create the object, add an ‘id’ or ‘name’ field to the object like so:

image.id = 1

image.name = “rect 1”

then on the event handler do this :

print(event.target.name)

also here is a small function I think Rob Miracle posted on the forum some time ago, that his helpful to print out table information at times when you are debugging your app:

function PRINT\_TABLE( t ) local print\_r\_cache={} local function sub\_print\_r(t,indent) if (print\_r\_cache[tostring(t)]) then print(indent.."\*"..tostring(t)) else print\_r\_cache[tostring(t)]=true if (type(t)=="table") then for pos,val in pairs(t) do if (type(val)=="table") then print(indent.."["..pos.."] =\> "..tostring(t).." {") sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8)) print(indent..string.rep(" ",string.len(pos)+6).."}") elseif (type(val)=="string") then print(indent.."["..pos..'] =\> "'..val..'"') else print(indent.."["..pos.."] =\> "..tostring(val)) end end else print(indent..tostring(t)) end end end if (type(t)=="table") then print(tostring(t).." {") sub\_print\_r(t," ") print("}") else sub\_print\_r(t," ") end print() end

Good luck.

Bob

Guess you can say he is making a 3D array?

@cybersparkstudios thanks that did the work

Last question, is it possible to change the text using its id?

Yes, using an if statement. 

if textObject.id == "text" then textObject.text = "New Text, wow" end

But, using the line inside of the if statement instead of using the whole if statement would be more ideal in some cases.

I did try that in my code but the one that change is the last “text” that has been created. I am using loop to create image and text and make them the same ids but like I said the one that change is the last “text” created by the loop. In short, how can I change text of my “text” when they have the same name but different ids? 

This post can stay, it’s may be content that should have been posted on the other thread. I however have removed the other two duplicates of this.

Forum rules ask that you only ask your question once. We understand that there are sometimes hiccups and in particular first time posters may not notice that your first post is held until moderation.

All is good now. But be aware of the rule going forward. If you need to review the forum rules, there is a link in the right side bar on the forum index page.

Rob

@MajinZepar, The code markup button is <> in blue.

Please write the following:

A question: The title is not enough. What are you looking to achieve?

The problem: What error messages did you get to the console? How did you expect your code supposed to respond compared to how it actually responded?

Debugging: What have you tried to resolve the problem?

To place an addEventListener for the images, place it in the for loop area where you create your images.

what im trying to do is when you the click the image, the text above it will change into a blank, 0, or 1. I already put an eventListener to all of the images. But my problem is, how will I know which image I click and which text will I change?

do refer to @sdktester comment if you want help.  A piece of your code (that in no way covers touch events) and a wish does not a proper forum post make!

pointer for you a touch event will expose the element that raised the event.  i.e.  event.target  will tell you the image clicked.

sure you can work the rest out

I purposely removed the codes for the touch event. And I also tried the event.target  and print it out the console and it only says the table, is there any other way to know what image I clicked?

event.target IS the clicked object.  Objects are tables… what was you expecting?

Another choice might be to;  right after you create the object, add an ‘id’ or ‘name’ field to the object like so:

image.id = 1

image.name = “rect 1”

then on the event handler do this :

print(event.target.name)

also here is a small function I think Rob Miracle posted on the forum some time ago, that his helpful to print out table information at times when you are debugging your app:

function PRINT\_TABLE( t ) local print\_r\_cache={} local function sub\_print\_r(t,indent) if (print\_r\_cache[tostring(t)]) then print(indent.."\*"..tostring(t)) else print\_r\_cache[tostring(t)]=true if (type(t)=="table") then for pos,val in pairs(t) do if (type(val)=="table") then print(indent.."["..pos.."] =\> "..tostring(t).." {") sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8)) print(indent..string.rep(" ",string.len(pos)+6).."}") elseif (type(val)=="string") then print(indent.."["..pos..'] =\> "'..val..'"') else print(indent.."["..pos.."] =\> "..tostring(val)) end end else print(indent..tostring(t)) end end end if (type(t)=="table") then print(tostring(t).." {") sub\_print\_r(t," ") print("}") else sub\_print\_r(t," ") end print() end

Good luck.

Bob