11-26-2009, 06:24 PM
|
#1
|
|
|
Having the first object snap back when a second object is placed in the same place
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.
|
|
|
11-26-2009, 07:42 PM
|
#2
|

 |
Värmland, Sweden
(and Illinois, USA) |
|
 |
1,329 |
|
|
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/
(I should get some sortof comission for how many beginners I have sent towards TweenLite.
Also, use "currentTarget" instead of "target" for this purpose. Google could probably explain better than me why.
__________________
Give someone code, and they will have code for a day.
Teach someone to code, and they will have code for life.
Support a starving developer. Click ads in my blog...
http://iqandreas.blogspot.com/
|
|
|
11-26-2009, 07:49 PM
|
#3
|

 |
Värmland, Sweden
(and Illinois, USA) |
|
 |
1,329 |
|
|
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...
ActionScript Code:
//...
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); } }
__________________
Give someone code, and they will have code for a day.
Teach someone to code, and they will have code for life.
Support a starving developer. Click ads in my blog...
http://iqandreas.blogspot.com/
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -4. The time now is 03:50 PM.
|
|