PDA

View Full Version : jigsaw help


drumn4life0789
02-18-2007, 06:43 PM
I am working on a jigsaw puzzle game and I have th puzzle all set up to work how i wnat except one thing. I want the pieces to appear randomly on the stage when the game is started.

I know I have to set a x and y boundary but i dont know how to go about doing so.

I would like the pieces between 10 and 300 in the x coordnate and between 10 and 500 on the y coordnate.



I also need some help for when the puzzle is completed to make it go to a different frame and say like congrats or something. I have the frame and all that stuff ready. I just need to know what the actionscript would be to detect if the puzzle is completed.

One more thing which is not as important, but it would be cool, i was wandering i fanyone know how to make the individual pieces snap together to each other and then become one. Even if they are not on the target pieces which i have made.

neilmmm
02-18-2007, 09:26 PM
i am sure there are more efficient ways to do this but something like this may get you going

somethinig like this to create random position

var xposition:Number;
var yposition:Number;
function genXPosition () {
xposition=Math.floor((Math.random()*291+10))
}

function genYPosition () {
yposition=Math.floor((Math.random()*491+10))
}
genYPosition ();
genXPosition ();
pieceA_mc._y=yposition;
pieceA_mc._x=xposition;
trace(pieceA_mc._x);
trace(pieceA_mc._y);

and something like this in onEnterFrame or on stopDrag function to check position

if (pieceA_mc._x==27 && pieceB_mc._x==27 && pieceC_mc._x==27) {
gotoAndStop("new_frame");

}

hope this helps


just saw the other bit

you do a hitTest on a shape that is invisible (target_mc) exactly where you want it to go and say it's _x and _y = the target_mc _ and _y

drumn4life0789
02-18-2007, 09:33 PM
Thanx for the help ill try it out.

I have it where it snaps to the right place but i was wandering if there is a way to make the individual pieces snap together before they are in the right place in the puzzle area.

If u know what i mean.

drumn4life0789
02-18-2007, 09:44 PM
piece1_mc.onPress = piece2_mc.onPress=piece3_mc.onPress=piece4_mc.onPr ess=function () {
this.startDrag();
this.swapDepths(1);
};




checkIfMatching = function (piece, target) {
piece.stopDrag();
if (piece.hitTest(target) == true) {
piece._x = target._x;
piece._y = target._y;
piece.onPress = null();
}
};



piece1_mc.onRelease = function() {
checkIfMatching(piece1_mc, target1_mc);
};
piece2_mc.onRelease = function() {
checkIfMatching(piece2_mc, target2_mc);
};
piece3_mc.onRelease = function() {
checkIfMatching(piece3_mc, target3_mc);
};
piece4_mc.onRelease = function() {
checkIfMatching(piece4_mc, target4_mc);
};




piece1_mc.onReleaseOutside = function() {
checkIfMatching(piece1_mc, target1_mc);
};
piece2_mc.onReleaseOutside = function() {
checkIfMatching(piece2_mc, target2_mc);
};
piece3_mc.onReleaseOutside = function() {
checkIfMatching(piece3_mc, target3_mc);
};
piece4_mc.onReleaseOutside = function() {
checkIfMatching(piece4_mc, target4_mc);
};




theres a copy of what I have at the moment. It works fine but i just want the random at the beginning. and the on enterframe thing and if pieces are together in the right place but not on the targets for them to stick together then move as one piece.

drumn4life0789
02-19-2007, 02:38 AM
Ok i figured out how to make the pieces start out in random place on the screen.

Still wandering if there is anyone who could help me out on some code that basically says, when two pieces are joined together but not yet on the target, they actually go together and become one piece able to move accross the screen.

rrh
02-19-2007, 04:26 AM
Probably the way I would do it is:
1) Create for each piece an array of the pieces it is attached to.
2) When you drag a piece, it will apply the same movement to every piece listed in that array. I don't think you can apply startDrag to more than one piece, so you will instead have to create a function that moves the piece based on the movement of the mouse.

drumn4life0789
02-19-2007, 05:31 AM
thanks on the number1 but i dont understand what you mean on number 2.

If you are talking about the wave i have my code written where, mc1=mc2=startdrag, that actually does work to make them all move. It actually makes the file size not as big as well because there is less coding

one more thing i was working on was making a button that rescrambles the puzzle. I got it to rescramble the pieces but once you do that then the pieces become non clickable again. Does anyone know how to make this work like you just started the game. I made the button do a math random on all the pieces, and that scrambles it but pieces cant move.

sd9sd
02-19-2007, 05:49 AM
Dont know exactly what kind of game you're making, or what logic you'd like to use, but if I was making a jigsaw puzzle game, I'd have all the pieces as objects. Each with their own number code and row and column variables.
A 2D array would be responsible for the positioning of the pieces.
So when the pieces are all in the correct position, their number codes would start with 0,1,2,3...... etc. The number codes, being the array positions.
For randomly placing them, just have another array in which you generate the number codes randomly and place the pieces according to the generated numbers.
When the codes in the second array match the ones in the first array, the puzzle is solved!

rrh
02-19-2007, 07:10 AM
If you are talking about the wave i have my code written where, mc1=mc2=startdrag, that actually does work to make them all move. It actually makes the file size not as big as well because there is less coding That wasn't what I was talking about, though it's not the way I would do it. I would do this:

home=this;
numPieces=4;
for (count=1;count<=numPieces;count++) {
home.[piece+"count"+_mc].onPress=function() {
this.startDrag();
this.swapDepths(1);
}
}

Because then when I add more pieces, all I need to change is the 4 to 5.

But that wasn't what I was talking about. I was talking about
startDrag();

If I do
movie1.startDrag();
movie2.startDrag();
then only movie2 will be dragged. So you will have to create your own function that will move the movie clip based on _xmouse and _ymouse. That way, you can apply it to ever piece that is attached to the one you press.