Touch Listeners In For Loop - Assertion Failed

Not sure what I’m doing wrong here.

Basically, I’ve created a grid layout of rectangles using a for loop.

[lua]for i = 1, 10 do

local myRect = display.newRect(0, 0, 20, 20)

end[/lua]
The layout code I’m using works perfectly well, however, I want to register a touch listener on each of the rectangles. I been trying to do this within the for loop using something like:

[lua]myRect:addEventListener(“tap”, goToFunction)[/lua]

When I attempt to run the code with the above listener code within the for loop, I get a Runtime Error:

Runtime error: assertion failed!
stack traceback:
[C]: ?
[C]: in function ‘assert’
?: in function ‘getOrCreateTable’
?: in function ‘addEventListener’

Is this because the rectangle is always named ‘myRect’ and it’s attempting to assign the listener to that object name every time it loops? If so, how would I go about changing the name on each loop (myRect1, myRect2, etc)?
Many thanks

[import]uid: 74503 topic_id: 21207 reply_id: 321207[/import]

Do it like this :

[code]

local function myFunction(event)
end

local rects = {}

for i = 1, 10 do
rects[i] = display.newRect(0, 0, 20, 20)
rects[i]:addEventListener(“tap”, myFunction)
end
[/code] [import]uid: 84637 topic_id: 21207 reply_id: 83978[/import]

Thanks for the quick reply Danny. I’ll give that a go right now. [import]uid: 74503 topic_id: 21207 reply_id: 83982[/import]

@Danny That works great thanks. One other thing though…

Using your above example, let’s say that I wanted to use the listener to pass a parameter to myFunction. Can this be done?

For example:

[lua]local function myFunction(event)

– Print ‘rectangleNumber’ from passed parameter (below)

end

local rects = {}

for i = 1, 10 do
rects[i] = display.newRect(0, 0, 20, 20)

– Create a string variable to use as a parameter later

local rectangleNumber = “rectangle”…i

– How can rectangleNumber (above) be passed to myFunction?

rects[i]:addEventListener(“tap”, myFunction)
end[/lua]

Thanks [import]uid: 74503 topic_id: 21207 reply_id: 83994[/import]

Sure :

[code]
local function myFunction(event)
local target = event.target

print(target.name)
end

local rects = {}

for i = 1, 10 do
rects[i] = display.newRect(0, 0, 20, 20)

– Create a string variable to use as a parameter later

rects[i].name = “rectangle” … i

– How can rectangleNumber (above) be passed to myFunction?

rects[i]:addEventListener(“tap”, myFunction)
end [import]uid: 84637 topic_id: 21207 reply_id: 83996[/import]

Once you’ve created your array of display objects, you can assign any number of properties to each object, e.g.

[lua]rect[i].id = i
rect[i].canTouch = true[/lua]

Then within your ‘myFunction’, you can access all the properties of the rectangle that was touched.

[lua]local myFunction = function (event)

local obj = event.target

if event.phase == “ended” and obj.canTouch then

print ("Rectangle "…obj.id…“touched”)

end

end[/lua]
[import]uid: 93133 topic_id: 21207 reply_id: 83997[/import]

Awesome - works perfectly thank you.

Great support and community once again. [import]uid: 74503 topic_id: 21207 reply_id: 84008[/import]

Glad to help :slight_smile: [import]uid: 84637 topic_id: 21207 reply_id: 84010[/import]

I know this is old but this is the closest answer i can find for my problem.

I have create grid of images using for loop and i can’t figure out how to add tap on each images…even with the solution you gave to iNSERT CODE.

here is my code : 

local images = {} images[1] = { file = "image01.png", width = 91, height = 122 } images[2] = { file = "image02.png", width = 91, height = 122 } images[3] = { file = "image03.png", width = 91, height = 122 } images[4] = { file = "image01.png", width = 91, height = 122 } local xPos = 50 -- this is the first image position local yPos = 0 -- this is the yPosition of all my images for i = 1, 4 do local myRectangle = display.newImageRect(images[i].file, images[i].width, images[i].height) scrollView:insert( myRectangle ) myRectangle.x = xPos myRectangle.y = yPos myRectangle.anchorY = 1 myRectangle.y = display.actualContentHeight + display.screenOriginY -- Increment the xPos so each rectangle is not on the same spot xPos = xPos + 100 end

How to add tap event for “images[1]” for example?

Thank you

I know this is old but this is the closest answer i can find for my problem.

I have create grid of images using for loop and i can’t figure out how to add tap on each images…even with the solution you gave to iNSERT CODE.

here is my code : 

local images = {} images[1] = { file = "image01.png", width = 91, height = 122 } images[2] = { file = "image02.png", width = 91, height = 122 } images[3] = { file = "image03.png", width = 91, height = 122 } images[4] = { file = "image01.png", width = 91, height = 122 } local xPos = 50 -- this is the first image position local yPos = 0 -- this is the yPosition of all my images for i = 1, 4 do local myRectangle = display.newImageRect(images[i].file, images[i].width, images[i].height) scrollView:insert( myRectangle ) myRectangle.x = xPos myRectangle.y = yPos myRectangle.anchorY = 1 myRectangle.y = display.actualContentHeight + display.screenOriginY -- Increment the xPos so each rectangle is not on the same spot xPos = xPos + 100 end

How to add tap event for “images[1]” for example?

Thank you