0
votes

i need help with my first app with cocos2d, I'm trying to develop puzzle game, the game has image pieces and it's transparent background , and i need to match thees peices to the correct place/position ! my problem is that i don't know how to find the exact position/ place of the piece on it's place on the image ? can any one help me please ?

trying to make game like this : Animal Puzzle

and puzzle image and pieces like this : Puzzle Image & pieces

1

1 Answers

0
votes

If I'm understanding correctly, you want to detect when the user has dragged a puzzle piece into the correct slot.

If that's the case, and correct me if I'm misunderstanding, you'll need at least two pieces of information to make it happen: the position of the puzzle piece's correct location (i.e. the position the piece should be in when the puzzle is put together), and the same puzzle piece's current location as it's being moved by the user. Once you have this information, perhaps as two CGPoints (correctPosition and currentPosition), you can get the distance between them with ccpDistance():

CGPoint currentPosition = selectedPuzzlePiece.position;
float distanceFromCorrectPos = ccpDistance(correctPosition, currentPosition);

In the above example, correctPosition could be a CGPoint stored before the pieces are mixed up, and selectedPuzzlePiece could be the CCSprite the user is currently touching. You could then use the distanceFromCorrectPos value to determine when the user has moved the piece close enough to snap it into place:

float snapIntoPlaceThreshold = 10.0f;
if(distanceFromCorrectPos <= snapIntoPlaceThreshold) {
    selectedPuzzlePiece.position = correctPosition;
}