PDA

View Full Version : [AS2] Drag and Drop, switch piece puzzle problem


Jefinho
09-25-2008, 08:03 AM
Hi,

I am trying to create a puzzle that has 18 pieces which the user can drag from on piece to the other. When the puzzle/game starts, the pieces will placed on the stage in random order.
When the user drags one piece over another, the 2 pieces will be switched.
This is done technically when the 'hit area' (a smaller mc inside of the main 'piece' mc) from one piece is over another.
When the user releases a piece, a 'switch' function will be called. This function uses x and y coordinates which are defined outside the function. It looks somehow like this:


// initial x and y coordinates form all the pieces, after they are ramdomly
// placed on the stage.
_root.startXkp_0 = this.kp_0._x;
_root.startYkp_0 = this.kp_0._y;
_root.startXkp_1 = this.kp_1._x;
_root.startYkp_1 = this.kp_1._y;
_root.startXkp_2 = this.kp_2._x;
_root.startYkp_2 = this.kp_2._y;
_root.startXkp_3 = this.kp_3._x;
_root.startYkp_3 = this.kp_3._y;
_root.startXkp_4 = this.kp_4._x;
_root.startYkp_4 = this.kp_4._y;
_root.startXkp_5 = this.kp_5._x;
_root.startYkp_5 = this.kp_5._y;
_root.startXkp_6 = this.kp_6._x;
_root.startYkp_6 = this.kp_6._y;
_root.startXkp_7 = this.kp_7._x;
_root.startYkp_7 = this.kp_7._y;
_root.startXkp_8 = this.kp_8._x;
_root.startYkp_8 = this.kp_8._y;
_root.startXkp_9 = this.kp_9._x;
_root.startYkp_9 = this.kp_9._y;
_root.startXkp_10 = this.kp_10._x;
_root.startYkp_10 = this.kp_10._y;
_root.startXkp_11 = this.kp_11._x;
_root.startYkp_11 = this.kp_11._y;
_root.startXkp_12 = this.kp_12._x;
_root.startYkp_12 = this.kp_12._y;
_root.startXkp_13 = this.kp_13._x;
_root.startYkp_13 = this.kp_13._y;
_root.startXkp_14 = this.kp_14._x;
_root.startYkp_14 = this.kp_14._y;
_root.startXkp_15 = this.kp_15._x;
_root.startYkp_15 = this.kp_15._y;
_root.startXkp_16 = this.kp_16._x;
_root.startYkp_16 = this.kp_16._y;
_root.startXkp_17 = this.kp_17._x;
_root.startYkp_17 = this.kp_17._y;

function wisselKaarten(nr):Void {
for (var i:Number = 0; i<18; i++) {

if (i != nr) {
if (this["kp_"+nr].detecteer.hitTest(_root["kp_"+i].detecteer)) {
// change x and y from piece A
this["kp_"+nr]._x = this["kp_"+i]._x;
this["kp_"+nr]._y = this["kp_"+i]._y;
// change x and y from piece B
this["kp_"+i]._x = _root["startXkp_"+nr];
this["kp_"+i]._y = _root["startYkp_"+nr];
// tell the Number-vars outside this function that x and y
// from 2 pieces are changed now
_root["startXkp_"+nr] = this["kp_"+nr]._x;
_root["startYkp_"+nr] = this["kp_"+nr]._y;
}
} else {
// if one piece is not over another (detected by the 'detecteer' mc
// inside every piece) go back to the old location.
this["kp_"+nr]._y = _root["startYkp_"+nr];
this["kp_"+nr]._x = _root["startXkp_"+nr];

}
}
// this function checks if the cards are in right order
checkKaarten();
}


The function above will be called everytime the user releases a piece:


this.kp_0.onRelease = function():Void {
stopDrag();
wisselKaarten(0);
};
this.kp_0.onReleaseOutside = this.kp_0.onRelease;


At this moment some pieces actually change location, others simply don't. They go back to the old location, or don't switch with another piece. I guess it has to do with the x- and y- vars of certain peices which cannot be defined correctly.

Can someone please, please help me out?
Thanks!

drumn4life0789
09-25-2008, 08:21 AM
I dont really understand what your trying to accomplish here.

Jefinho
09-25-2008, 09:02 AM
I guess it's a little hard to understand right now, so I made an example. See the attached ZIP (it includes a fla).

The intention is that a user can drag and drop a puzzle piece over another. When the user releases the piece, the 2 pieces will switch location. If the dragged piece is not over another piece, it will go back to it's old location.
I hope someone can help me.

Jefinho
09-25-2008, 03:14 PM
anyone... please... please...

Jefinho
09-25-2008, 10:30 PM
Alright... I solved the problem myself... Harder than I thought... Took me 2 days.

drumn4life0789
09-26-2008, 12:59 AM
well i guess this might be a little late. But in the part where you are picking up the piece just add a variable to remember where that piece was.

piecePosX = piece._x;
piecePosy = piece._y

then in your onRelease function add

if selected piece x and y are the same as another pieces x and y ( within a certain distance i suspect)

selected piece take new piece x and y
new piece take selected piece x and y

else
selected piece go back to the variables you have stored up there.