PDA

View Full Version : rotate pieces


drumn4life0789
02-25-2007, 06:14 PM
Im in the middle of making a jigsaw puzzle and need help on a few things.

Basically the rotation part.

I want the pieces to be randomly rotated(on 90 degree intervals) once the user clicks the start_btn.

Then i would like the piece which was last selected to be rotated by hitting space. (I know the keyListener script and no how to set it to one piece but not the most recently selected piece.)

And finally, I want the pieces to snap into place only if they are rotated in the same way that the target piece is rotated.


Thanx in advance.

inhan
02-25-2007, 07:22 PM
var totalPieces:Number = 5;
var selectedPiece:Object;
start_btn.onRelease = function() {
for (var i:Number = 1; i<=totalPieces; i++) {
var rot:Number = Math.floor(Math.random()*4);
//trace(rot*90);
var mc:MovieClip = _root["piece"+i];
mc._rotation = rot*90;
// assuming your pieces are named like "piece1", "piece2" etc in the _root.
// mc._x = define x pos.
// mc._y = define y pos.
mc.onPress = function() {
selectedPiece = this;
_root.onKeyDown = function() {
if (Key.isDown(Key.SPACE)) {
trace(selectedPiece);
selectedPiece._rotation += 90;
}
}
};
}
};
Key.addListener(this);

inhan
02-27-2007, 01:44 AM
Here's the example :)

drumn4life0789
02-28-2007, 04:26 AM
thanks, that helps alot. And the reason why i had piece_alpha = o at the end was because for some reason it was still showing it when it switched to scene 2.

So making the alpha go to to zero made it dissapear.


but thinks u cleared up a few basic things for me.

drumn4life0789
02-28-2007, 05:02 AM
also what did the mc.id = 1 or whatever it was mean

drumn4life0789
02-28-2007, 06:11 AM
and i dont really understand how it knows which piece to rotate.

And how would i keep the pieces on the stage, right now you can just drag them off.

inhan
02-28-2007, 02:32 PM
Okay more explanations:

In the 2nd line I created an object called "selectedPiece" and I'm assigning it the pressed (selected) piece in the 46th line so until I press another piece, the "selectedPiece" represents the last piece pressed. That's how I can control the "selected" piece in the overall movie. Note that I'm emptying its value with "null" in onRelease function. Objects can be movie clips, text fields, and buttons (if I haven't missed anything) so they're quite handly when it comes to control specific mc's, or features of them. Hope you got a brief idea of what I did...

About this.id thing...
In the following example, whichever piece you press, it gives the final value of "i" (4) because it almost instantly (said to be in 2 millisecs) becomes the final value:

for (i=1; i<=4; i++) {
_root["piece"+i].onRelease = function() {
trace(i);
}

So the above function, no matter which "piece" mc you press, will return the number "4". But, if you want to get the number of each button/mc, you have to assign a variable in the for loop:

for (i=1; i<=4; i++) {
_root["piece"+i].id = i;
// the variable "id" is my choice, you can use any non-reserved word.
// this makes "piece1.id=1", "piece2.id=2" and so on, so that...
_root["piece"+i].onRelease = function() {
trace(this.id);
// because "this" refers to the specific piece in this condition,
// for e.g. "piece3", this.id becomes "piece3.id" which is 3.
}

The last issue is one of the most-asked issues in the forums. I did the same mistake over and over again until I learned what I should do to prevent getting the final value of the "i" variable. "For" loops are so practical, but they have little tricks like the one above. If you have, say 100 pieces, you would write a function with the loop that takes only 5-7 lines whereas it would take min of 300 lines if you referred to each piece separately. That's why we like for loops so much :)

Anyway about keeping the pieces on the stage...

startDrag has several parameters like:
startDrag(lockCenter:Boolean, left:Number, top:Number, right:Number, bottom:Number);

lockCenter parameter gets "false" if you don't want piece(s) to snap their centers into the cursor Try it with "true" and press on the corner of a piece. You'll see what I mean.

The limits are defined with left/top/right/bottom parameters. Note that they represent the values of the "parent" of this piece. So the following will limit the dragging to the stage (for the mc's with registration points in their centers):

piece1.onPress = function() {
this.startDrag(false, this._width/2, this._height/2, Stage.width-this._width/2, Stage.height-this._height/2);
};