Hi!
I need to get the index of a group’s children when I touch it.
Is it possible?
Thanks. [import]uid: 25327 topic_id: 7396 reply_id: 307396[/import]
http://developer.anscamobile.com/reference/index/groupnumchildren [import]uid: 12108 topic_id: 7396 reply_id: 26100[/import]
Sorry, i made a mistake in my question (english is not my first language).
Well, actually I need to get the index number of a group’s child when I touch it. [import]uid: 25327 topic_id: 7396 reply_id: 26106[/import]
Why? Could you explain what you are trying to do?
Anyway, use the .parent property to get the group of the tapped object, and then loop through the children of that group using the numChildren property above. [import]uid: 12108 topic_id: 7396 reply_id: 26107[/import]
Actually, I’m working on a sliding puzzle game wich is working but i need more control to expand it.
I would like to keep track of each pieces to create winning conditions for exemple.
Can you help me? I’m going crazy 
Thanks a lot 
[code]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR
local gridHeight = 2
local gridWidth = 2
local lastPiece = (gridHeight * gridWidth)
local pieceStep = 64
local xPiece = 0
local yPiece = 0
local pieceNumber = 1
local xBlack = 0
local yBlack = 0
–> Interaction
local function clickHandler( event )
local piece = event.target
if piece.x == xBlack - pieceStep and piece.y == yBlack then
transition.to( piece, { time=300, x=(piece.x+pieceStep), transition=easing.inOutExpo } )
xBlack = xBlack - pieceStep
elseif piece.x == xBlack + pieceStep and piece.y == yBlack then
transition.to( piece, { time=300, x=(piece.x-pieceStep), transition=easing.inOutExpo } )
xBlack = xBlack + pieceStep
elseif piece.y == yBlack + pieceStep and piece.x == xBlack then
transition.to( piece, { time=300, y=(piece.y-pieceStep), transition=easing.inOutExpo } )
yBlack = yBlack + pieceStep
elseif piece.y == yBlack - pieceStep and piece.x == xBlack then
transition.to( piece, { time=300, y=(piece.y+pieceStep), transition=easing.inOutExpo } )
yBlack = yBlack - pieceStep
else return
end
end
–> Create puzzle
local function createPuzzle()
local puzzle = display.newGroup()
for yp=1,gridHeight do
for xp=1,gridWidth do
if pieceNumber == lastPiece then
xBlack = xPiece
yBlack = yPiece
break end
local piece = display.newImage( “room” … pieceNumber … “.png”, xPiece, yPiece )
– I would like to generate a unique name for this object!
piece.xReference = -( pieceStep / 2 )
piece.yReference = -( pieceStep / 2 )
piece:addEventListener( “touch”, clickHandler )
puzzle:insert( piece )
xPiece = xPiece + pieceStep
pieceNumber = pieceNumber + 1
end
xPiece = 0
yPiece = yPiece + pieceStep
end
puzzle.xReference = (pieceStep * gridWidth) / 2
puzzle.yReference = (pieceStep * gridHeight) / 2
puzzle.x = display.contentWidth / 2
puzzle.y = display.contentHeight / 2
end[/code] [import]uid: 25327 topic_id: 7396 reply_id: 26113[/import]
You can add new properties to objects at will. So just add an “id” to each tile and use that to keep track of them:
piece.id = 20
Then the solution to every puzzle is simply stored in a table that you loop through whenever a piece is moved (pseudocode):
[code]
solution = {}
solution[20] = position
etc.
if solution.[piece.id] == piece.position then
[/code] [import]uid: 12108 topic_id: 7396 reply_id: 26121[/import]
I understand that you can add an ID to each tile wich i did like this in the puzzle creation function :
for yp=1,gridHeight do
for xp=1,gridWidth do
if pieceNumber == lastPiece then
xBlack = xPiece
yBlack = yPiece
break end
local piece = display.newImage( "room" .. pieceNumber .. ".png", xPiece, yPiece )
piece.xReference = -( pieceStep / 2 )
piece.yReference = -( pieceStep / 2 )
piece.id = pieceNumber --\> Add ID
piece:addEventListener( "touch", clickHandler )
puzzle:insert( piece )
xPiece = xPiece + pieceStep
pieceNumber = pieceNumber + 1
end
xPiece = 0
yPiece = yPiece + pieceStep
end
But I don’t really understand how you control it.
For exemple, if I want to call a tile by its ID to tell him to change its color or whatever. How can i do?
Thanks for your help. I really want to understand this 
[import]uid: 25327 topic_id: 7396 reply_id: 26174[/import]
create an array (table) of pieces…
local pieces = {}
then each new image goes in that array by index
piece[gridWidth * (y-1) + x] = newImage etc
piece[gridWidth * (y-1) + x].posx = x
piece[gridWidth * (y-1) + x].posy = y
now you can reference piece at x,y with piece[gridWidth * (y-1) + x]
In your touch function you can just use event.target.posx to check which piece it is, and event.target.alpha = 0.5 for example to manipulate a propery of the image.
If you definatly need to reference the piece directly you can use:
piece[gridWidth * (event.target.posy-1) + event.target.posx]
something like that anyway… i’m tired
[import]uid: 8872 topic_id: 7396 reply_id: 26185[/import]
Thanks!
I will try to work around that
[import]uid: 25327 topic_id: 7396 reply_id: 26230[/import]