vernski
November 26th, 2009, 05:24 PM
So I have two circles and a square on stage, myCircle1, myCircle2, and mySquare. You can drag the two circles, if you drop either one outside the square it'll snap back to where it originally was, if it's touching the box it'll snap to the center of the square.
I want it so when the second circle (either one) snaps to the center of the square, the first one will go back to it's original co-ordinates. So only one circle can be in the square at any time.
I've attached my as file with the current code.
IQAndreas
November 26th, 2009, 06:42 PM
Here is one suggestion.
Add another property "objectInSquare". If this value is null, there is nothing "inside" of the square.
If "objectInSquare" is a movieclip, move the current movieClip out of the way, and replace it with whatever object was dragged onto the square. I'll post some actual code in one second, but try to think on your own how to accomplish this, and if you understand this first. Just ask, and I'll explain or clarify anything you do not understand.
Also, I would recommend TweenLite, which is a lot easier to use than Adobe's built in Tween engine. The library is free and can be found and downloaded here:
http://blog.greensock.com/tweenliteas3/ (http://blog.greensock.com/tweenliteas3/)
(I should get some sortof comission for how many beginners I have sent towards TweenLite. http://kirupa.com/forum/images/smilies/wink.gif
Also, use "currentTarget" instead of "target" for this purpose. Google could probably explain better than me why.
IQAndreas
November 26th, 2009, 06:49 PM
Here is the actual code, but instead of just using it, try to understand how it works, and type up the code yourself based on the idea, instead of just using copy/paste.
Also, you might have one slight bug here which you might have to work out on your own. ;) Not all things in life are free...
//...
private var objectInSquare:MovieClip = null;
private function drop(e:MouseEvent){
currentObject.stopDrag();
removeEventListener(MouseEvent.MOUSE_UP, drop);
if(currentObject.hitTestObject(mySquare)){
if (objectInSquare != null)
{
//Move the current object in the square out of the way
snapperx = new Tween (currentObject, "x", Elastic.easeInOut, currentObject.x, currentObject.startX, 1, true);
snappery = new Tween (currentObject, "y", Elastic.easeInOut, currentObject.y, currentObject.startY, 1, true);
}
//Now, set the objectInSquare to whatever was clicked
objectInSquare = currentObject;
//Now, move the current object to inside of the square
snapperx = new Tween (currentObject, "x", Elastic.easeInOut, currentObject.x, mySquare.startX, 1, true);
snappery = new Tween (currentObject, "y", Elastic.easeInOut, currentObject.y, mySquare.startY, 1, true);
}else{
snapperx = new Tween (currentObject, "x", Elastic.easeInOut, currentObject.x, currentObject.startX, 1, true);
snappery = new Tween (currentObject, "y", Elastic.easeInOut, currentObject.y, currentObject.startY, 1, true);
}
}
vernski
November 27th, 2009, 09:25 AM
Ha! That cracks me up. I'll let you know when I figure out the code.
I am actually very interested in truly understanding what I'm doing (hence the random swf that doesn't really do anything) and I appreciate the help!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.