Issue with addEventListener in a loop

Hi!
I would like to make a sliding puzzle using a for loop. Everything works fine until i try to add an event listener to each pieces (“room”). Any suggestion appreciated :slight_smile:

[code]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

local gridSubdivision = 3
local pieceCount = 0
local lastPiece = (gridSubdivision * gridSubdivision) - 1
local step = 64
local xRoom = 0
local yRoom = 0
local roomNumber = 1

–> Create grid
local puzzle = display.newGroup()

for yp=1,gridSubdivision do
for xp=1,gridSubdivision do
if pieceCount == lastPiece then
local blackPiece = display.newImage( “black.png”, xRoom, yRoom )
local xBlack = xRoom
local yBlack = yRoom
puzzle:insert( blackPiece )
break end
local room = display.newImage( “room” … roomNumber … “.png”, xRoom, yRoom )
room:addEventListener( “touch”, clickHandler )
puzzle:insert( groom )
xRoom = xRoom + step
roomNumber = roomNumber + 1
pieceCount = pieceCount + 1
end
xRoom = 0
yRoom = yRoom + step
end

–> Interaction
local clickHandler = function( event )
– in progress
end
[/code] [import]uid: 25327 topic_id: 7312 reply_id: 307312[/import]

yes it can’t see clickHandler in your loop because you havent defined it yet

do this:

[lua]local puzzle = display.newGroup()
local clickHandler – forward reference

for…
– creation loop etc
… end

function clickHandler(event) – still local due to forward reference[/lua]

[import]uid: 6645 topic_id: 7312 reply_id: 25690[/import]

Wow ok… My bad!
Thanks a lot! [import]uid: 25327 topic_id: 7312 reply_id: 25721[/import]

Hi again!
As I completed the little program, I tough it would be a good idea to share with the community as I didn’t find any Corona sliding puzzle tutorial on the net. So here is the source code. Should it be posted in Code Exchange?
Any comments to improve it are welcome!

[code]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

local gridSubdivision = 3
local lastPiece = (gridSubdivision * gridSubdivision)
local step = 64
local xRoom = 0
local yRoom = 0
local roomNumber = 1
local xBlack = 0
local yBlack = 0
–> Interaction
local function clickHandler( event )
local piece = event.target
if piece.x == xBlack - step and piece.y == yBlack then
–transition.to( piece, { time=500, x=(x-step), transition=easing.inOutExpo } )
piece.x = piece.x + step
xBlack = xBlack - step
elseif piece.x == xBlack + step and piece.y == yBlack then
piece.x = piece.x - step
xBlack = xBlack + step
elseif piece.y == yBlack + step and piece.x == xBlack then
piece.y = piece.y - step
yBlack = yBlack + step
elseif piece.y == yBlack - step and piece.x == xBlack then
piece.y = piece.y + step
yBlack = yBlack - step
else return
end
end
–> Create grid
local puzzle = display.newGroup()

for yp=1,gridSubdivision do
for xp=1,gridSubdivision do
if roomNumber == lastPiece then
xBlack = xRoom
yBlack = yRoom
break end
local room = display.newImage( “room” … roomNumber … “.png”, xRoom, yRoom )
room.xReference = -( step / 2 )
room.yReference = -( step / 2 )
room:addEventListener( “touch”, clickHandler )
puzzle:insert( room )
xRoom = xRoom + step
roomNumber = roomNumber + 1
end
xRoom = 0
yRoom = yRoom + step
end

puzzle.xReference = (step * gridSubdivision) / 2
puzzle.yReference = (step * gridSubdivision) / 2
puzzle.x = display.contentWidth / 2
puzzle.y = display.contentHeight / 2
[/code] [import]uid: 25327 topic_id: 7312 reply_id: 25907[/import]

By the way, I’ve started to use transition to move the pieces but it doesn’t work… [import]uid: 25327 topic_id: 7312 reply_id: 25908[/import]

that should be

[lua]transition.to( piece, { time=500, x=(piece.x-step), transition=easing.inOutExpo } )[/lua] [import]uid: 6645 topic_id: 7312 reply_id: 25911[/import]

It works perfectly! Thanks!
I would like to know if it is possible to keep track of each pieces independently?
If i want to change the color of specific piece for exemple.

Michael [import]uid: 25327 topic_id: 7312 reply_id: 25925[/import]