11 Replies Latest reply on Sep 12, 2009 11:04 PM by kglad

    snapping objects together

    Shad0wdrag0n Level 1

      I've been looking for a tutorial that describes how to make it so dragable objects can snap together when one is dropped in the right place next to another, but so far I haven't had much luck. Does anyone happen to know of such a tutorial, or can post something here about how to do it?

       

      For example, how do I make something like this: http://www.flashpuzzlezone.com/jigsaw/people/sports/earnhardtjr/ and how do I make it recognise when all the pieces are attached together?

        • 1. Re: snapping objects together
          kglad Adobe Community Professional & MVP

          each moveable movieclip should have an exact _droptarget. when you drop a movieclip you check if the movieclip has hit its _droptarget and, if so, you position it to match the _droptarget.

          1 person found this helpful
          • 2. Re: snapping objects together
            Shad0wdrag0n Level 1

            I seem to have a little problem getting this to work properly. This is what I have:

             

            blueBall1.onPress = function(){
                if (!alreadyDragged2){
                    startDrag(this);
                }
            }
            blueBall1.onRelease = blueBall1.onReleaseOutside = function(){
                if(this._droptarget = "/blueBasket"){
                    this._x=125.5
                    this._y=255.5
                    alreadyDragged2=true;
                    stopDrag();
                }
                else{
                    stopDrag();
                }
            }

             

            I have two objects, the first is a dragable button called Blueball1, and the other is a movie clip called blueBasket. The problem is that no matter where blueBall1 is released it snaps to 125.5 and 255.5. for some reason I can't make it detect that it isn't on top of blueBasket. What do I have wrong? Am I not using _droptarget correctly?

            • 3. Re: snapping objects together
              kglad Adobe Community Professional & MVP

              use:

               


               

              blueBall1.onPress = function(){
                  if (!alreadyDragged2){
                      startDrag(this);
                  }
              }
              blueBall1.onRelease = blueBall1.onReleaseOutside = function(){
                  if(eval(this._droptarget) == blueBasket){  // your main problem was using a single = instead of double equal (==)
                      this._x=blueBasket._x;
                      this._y=blueBasket._y
                      alreadyDragged2=true;
                      stopDrag();
                  }
                  else{
                      stopDrag();
                  }
              }

               

               

              • 4. Re: snapping objects together
                Shad0wdrag0n Level 1

                Nope, blueBall1 isn't detecting blueBasket. I drop the ball right on the basket and it's just like dropping it anywhere.

                • 5. Re: snapping objects together
                  kglad Adobe Community Professional & MVP

                  what's the following show when blueBall is dropped onto blueBasket?

                   


                   


                   

                  blueBall1.onPress = function(){
                      if (!alreadyDragged2){
                          startDrag(this);
                      }
                  }
                  blueBall1.onRelease = blueBall1.onReleaseOutside = function(){

                  trace(eval(this._droptarget)+" "+blueBasket);

                      if(eval(this._droptarget) == blueBasket){  // your main problem was using a single = instead of double equal (==)
                          this._x=blueBasket._x;
                          this._y=blueBasket._y
                          alreadyDragged2=true;
                          stopDrag();
                      }
                      else{
                          stopDrag();
                      }
                  }

                   

                   

                  • 6. Re: snapping objects together
                    Shad0wdrag0n Level 1

                    I get undefined _level0.blueBasket when I drop the ball onto the basket. So, it's detecting the blueBasket but it's not snapping into position or changing the alreadyDragged value. Why would it be doing that?

                    • 7. Re: snapping objects together
                      kglad Adobe Community Professional & MVP

                      because you're not dropping blueBall1 on anything.   i know it looks like you're dropping onto the bluebasket to you but it doesn't to flash.

                       

                      flash determines the drop target not by looking at blueBall1.  it looks at the mouse pointer when you release blueBall1.  if the pointer is over the basket, the basket will be the _droptarget.  if the pointer is over empty stage, _droptarget will be undefined.

                      1 person found this helpful
                      • 8. Re: snapping objects together
                        Shad0wdrag0n Level 1

                        Ah, you're right, no matter where I drop blueBall1 I get that message. I made sure to have the mouse right over the blueBasket movieclip when I released the button but I just got the same message. Why wouldn't BlueBall1 detect blueBasket? Would it be better to use something like a hittest instead of _droptarget? Something that detects if blueBall1 is touching blueBasket when the mouse button is released?

                        • 9. Re: snapping objects together
                          kglad Adobe Community Professional & MVP

                          if bluebasket is not a movieclip, that code won't work.  is bluebasket a button or graphic or shape?

                          • 10. Re: snapping objects together
                            Shad0wdrag0n Level 1

                            blueBasket is a Movie Clip, not a Button or Graphic. Do you want me to attach the .fla file to a post here (that can be done can't it)?

                             

                            Edit: Wait, do both objects have to be movie clips? blueBasket is a movie clip, but the dragable object is a button.

                            • 11. Re: snapping objects together
                              kglad Adobe Community Professional & MVP

                              both have to be movieclips.  buttons don't have a _droptarget property.

                               

                              buttons also don't have startDrag() and stopDrag() methods (though those can be added).