PDA

View Full Version : Drag & Drop puzzle problems.


Xenozip
09-27-2003, 11:40 PM
I'm trying to create a puzzle-style flash game that involves 5 pieces and 5 targets. I plan on adding more pieces and targets later.

I also want a dynamic text to display weather the piece was placed in the right spot or not. Actually, I want the target movie clips to advance a frame if the piece is in the correct spot, and to advance two frames if it's not. However, a dynamic text is fine for now.

I've looked through the sample quizes and the actionscript tutorial puzzle for help on how to make a puzzle. I also checked the drag&drop tutorials located on this site. Still, I seem to be advancing very slowly, and now I'm totally stuck.

Here's what I have so far :

// Actions for Frame 1 (main timeline)
function strDrag(whichP) {
xCor = _root[whichP]._x;
yCor = _root[whichP]._y;
startDrag(_root[whichP], true, 31, 31, 450, 350);
}
function sNap(whichP) {
stopDrag();
if (eval(_root[whichP]._droptarget) == _root.B1) {
_root[whichP]._x = eval(_root[whichP]._droptarget)._x;
_root[whichP]._y = eval(_root[whichP]._droptarget)._y;
} else if (eval(_root[whichP]._droptarget) == _root.B2) {
_root[whichP]._x = eval(_root[whichP]._droptarget)._x;
_root[whichP]._y = eval(_root[whichP]._droptarget)._y;
} else if (eval(_root[whichP]._droptarget) == _root.B3) {
_root[whichP]._x = eval(_root[whichP]._droptarget)._x;
_root[whichP]._y = eval(_root[whichP]._droptarget)._y;
} else if (eval(_root[whichP]._droptarget) == _root.B4) {
_root[whichP]._x = eval(_root[whichP]._droptarget)._x;
_root[whichP]._y = eval(_root[whichP]._droptarget)._y;
} else if (eval(_root[whichP]._droptarget) == _root.B5) {
_root[whichP]._x = eval(_root[whichP]._droptarget)._x;
_root[whichP]._y = eval(_root[whichP]._droptarget)._y;
} else {
_root[whichP]._x = xCor;
_root[whichP]._y = yCor;
}
}
function checkIt(whichP) {
testTxt = "x"+eval(_root[whichP]._droptarget)._x;
testTwo = "y"+eval(_root[whichP]._droptarget)._y;
if (_root[whichP]._x == 440 && _root[whichP]._y == 40) {
if (_root[whichP]._name=P1) {
anS = "P1 is on B1!";
}
if (_root[whichP]._name != P1) {
anS = "Incorrect placement!";
}
}
}

I have one piece currently set up in the main timeline that is an MC named "P1", inside that clip is another movieclip "Piece Actions".

// Actions for "Piece Actions" MC located in "P1" MC
_visible = false;
_parent.onPress = function() {
_root.strDrag(_parent._name);
};
_parent.onRelease = function() {
_root.sNap(_parent._name);
_root.checkIt(_parent._name);
};


The puzzle will snap the piece back to its original position if the piece is not dropped on a target.

If the piece is dropped on any of the targets 2-5, the clip will react like it's supposed to by displaying the current pieces _x and _y positions, however the "incorrect" will not display. Also, if the piece is placed on Target1, the text will display properly (both "P1 is on B1" and the _x and _y positions). After that, however, if I click anywhere in the movie, the entire thing will attempt to drag.

If I remove the last few lines of code :
// Last few lines of code in main timeline (within checkIt() function)
if (_root[whichP]._x == 440 && _root[whichP]._y == 40) {
if (_root[whichP]._name=P1) {
anS = "P1 is on B1!";
}
if (_root[whichP]._name != P1) {
anS = "Incorrect placement!";
}
}
The clip will then work perfectly fine.
It's only untill I add that If/Then statement that the whole thing goes haywire.

I attached the FLA. I could really use any suggestions on how to get it to work.
That last If/Then statement is supposed to make it so that if Piece1 is on Target1, the dynamic text will display that information, otherwize if Piece1 is not on Target1, the dynamic text will display "Incorrect".

I really would appreciate some help here (I'm totally stumped).

vosgien
09-28-2003, 04:42 PM
Hi,
Change those last few lines of code from:

// Last few lines of code in main timeline (within checkIt() function)
if (_root[whichP]._x == 440 && _root[whichP]._y == 40) {
if (_root[whichP]._name=P1) {
anS = "P1 is on B1!";
}
if (_root[whichP]._name != P1) {
anS = "Incorrect placement!";
}
}
// TO:

if (_root[whichP]._x == 440 && _root[whichP]._y == 40 && _root[whichP]._name==P1)
{

anS = "P1 is on B1!";
}
else
{
anS = "Incorrect placement!";
}
}



Hope that helps

Vosgien

Xenozip
09-28-2003, 09:30 PM
That doesn't work right, either. It did fix the problem of the whole movie being dragged after the piece was placed on target1, though, thanks.

Now however, even when piece1 (P1) is placed on target1 (B1), the dynamic text still displays "Incorrect placement!", instead of what it's supposed to display ("P1 is on B1!"). Meaning even when the correct piece is placed on the correct target, the script is still returning false for that If statement (meaning it doesn't work). Why? :|

I tried changing the function to this :
function checkIt(whichP) {
testTxt = "x"+eval(_root[whichP]._droptarget)._x;
testTwo = "y"+eval(_root[whichP]._droptarget)._y;
daPiece = _root[whichP]._name;
daTarg = eval(_root[whichP]._droptarget)._name;
if (daPiece == P1 && daTarg == B1) {
anS = "P1 is on B1!";
} else {
anS = "Incorrect placement!";
}
}
The dynamic text displays "daPiece" as "P1" and the dynamic text "daTarg" displays as "B1", but the dynamic text "anS" is displaying "Incorrect placement!", which means the If statement still returns false. I don't understand...

Edit:
Oh! never mind. I fixed it.
I put P1 and B1 in quotes --making them expressions-- and now it works properly. Silly me.

Thanks for your help. :)

The statement I'm now using is this :
if (daPiece == "P1" && daTarg == "B1") {

Xenozip
09-28-2003, 11:08 PM
Ok, I have another question.

The puzzle works great thus far, except when a piece is placed on top of another piece (rather than a target or on an undefined area). Which is a problem, because I don't want the pieces to interact with one another.

This is the script I had for the "snap" function :

function sNap(whichP) {
stopDrag();
daDrop = eval(_root[whichP]._droptarget)._name;
if (eval(_root[whichP]._droptarget) == undefined) {
_root[whichP]._x = xCor;
_root[whichP]._y = yCor;
} else {
daTries = daTries+1;
isItDone();
_root[whichP]._x = eval(_root[whichP]._droptarget)._x;
_root[whichP]._y = eval(_root[whichP]._droptarget)._y;
}
}

With this script, the pieces will snap to their _droptarget, unless the droptarget is undefined, then it will return to its original position. However, the pieces will "hang" on top of one another if their droptarget is another piece, rather than returning to their original positions.

So, basically my question is, how to you prevent the pieces from interacting with other pieces?

I tried changing the sNap() function so that if the _droptarget was not "B1" through "B5", then the piece would return to its original position. However, it still did not work properly.

I had it set like this :

function sNap(whichP) {
stopDrag();
daDrop = eval(_root[whichP]._droptarget)._name;
if (daDrop != "B1" || "B2" || "B3" || "B4" || "B5") {
_root[whichP]._x = xCor;
_root[whichP]._y = yCor;
} else {
daTries = daTries+1;
isItDone();
_root[whichP]._x = eval(_root[whichP]._droptarget)._x;
_root[whichP]._y = eval(_root[whichP]._droptarget)._y;
}
}
This prevented "P2" from being dropped on top of "P1" (which is good), however it caused two new problems. If P1 was dropped on P2, then it would hang and not return to its original position (wtf is with that?). Also, reguardless of where P2 was dropped, it will always return to its original position instead of snapping to its _droptarget (even if it's dropped on the correct target -- "B2").

I attached the fla file to this message.

Any suggestions would be appreciated, as always.

Edit: The reason I don't want the pieces to interact with one another, is because I want to keep track of how many pieces have been moved over to a target (with the "daTries" variable), thus the puzzle is done when 5 out of 5 pieces have been moved over to a target. If a piece is moved over to another piece, it adds to the counter as well (which is bad).

Xenozip
09-29-2003, 12:07 AM
Arg, this didn't work either..
function sNap(whichP) {
stopDrag();
daDrop = eval(_root[whichP]._droptarget)._name;
if (daDrop == "B1" || daDrop == "B2" || daDrop == "B3" || daDrop == "B4" || daDrop == "B5") {
daTries = daTries+1;
isItDone();
_root[whichP]._x = eval(_root[whichP]._droptarget)._x;
_root[whichP]._y = eval(_root[whichP]._droptarget)._y;
} else {
_root[whichP]._x = xCor;
_root[whichP]._y = yCor;
}
}This fixed the problem I was having with my counter. It also prevents P2 from being droped on P1. But, for some reason, P1 will drop on top of P2 and "hang" on top of it, resetting it's starting positions.

Basically, the script works except for when P1 is dropped on top of another piece. Any way to prevent this from happening (perhaps it's a layer problem)?

Please help. *cries*

Xenozip
09-29-2003, 12:30 AM
Nevermind, I fixed it.

Incase it helps anyone else, this is what I did :

// This is the code that did not work.
//The actions for the "Piece Actions" MC inside each puzzle piece.

_visible = false;
_parent.onPress = function() {
_root.strDrag(_parent._name);
};
_parent.onRelease = function() {
_root.sNap(_parent._name);
_root.checkIt(_parent._name);
};

// I changed this to :

_visible = false;
_parent.onPress = function() {
_root.strDrag(_parent._name);
};
_parent.onRelease = _parent.onReleaseOutside=function() {
_root.sNap(_parent._name);
_root.checkIt(_parent._name);
};Obviously the answer was pretty simple. I needed the onReleaseOutside event to be included with the onRelease event.

vosgien
09-29-2003, 03:07 AM
Hi,
Glad you fixed it. I posted late last night so couldn't get back until this AM - seems you didn't need any help after all - one of the best things about these forums is that posting a problem often helps you see the answer as you go.

Well done

Good Luck

Vosgien

Xenozip
09-29-2003, 06:49 AM
Originally posted by vosgien
one of the best things about these forums is that posting a problem often helps you see the answer as you go. Hehe, yeah. I feel like I've been doing that all night.

This forum doesn't seem to get much action in the late evenings/early mornings, and that's usually when I'm up doing stuff. C'est la vie.