Go Back   kirupaForum > Flash > ActionScript 3.0

Reply
 
Thread Tools Display Modes
Old 11-26-2009, 06:24 PM   #1
vernski
Registered User
Location Ontario, Canada

Posts 4
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.
Attached Files
File Type: as Snapping.as (1.9 KB, 1 views)
vernski is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 11-26-2009, 07:42 PM   #2
IqAndreas
Registered User
Location Värmland, Sweden (and Illinois, USA)

Posts 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/
IqAndreas is offline   Reply With Quote
Old 11-26-2009, 07:49 PM   #3
IqAndreas
Registered User
Location Värmland, Sweden (and Illinois, USA)

Posts 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/
IqAndreas is offline   Reply With Quote
Old 11-27-2009, 10:25 AM   #4
vernski
Registered User
Location Ontario, Canada

Posts 4
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!
vernski is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:50 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com