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
[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]
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
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]