6 Replies Latest reply on Jul 5, 2010 2:40 PM by DrTux

    Drag and Drop game animated bounce back

    DrTux Level 1

      Hi

       


      What I am trying to do is make an animated bounce back for a drag and drop game, so if the object is dropped in a location that doesn't satisfy the hitTest it bounces back to a pre set location.


      It seams simple but there is obviously something not working. Just so people know I am a total newby when it comes to coding in actionscript, so please be gentle as I probably won't understand what your talking about.

       

      Anyway here is the code for the movie click which is being dragged.

       

      on (press)
      {
          startDrag(this,false,0,0,600,400);
      }
      on (release)
      {
          stopDrag();
          if (this.hitTest(_parent.MC_TargetAmpmeter))
          {
              this._x=_parent.MC_TargetAmpmeter._x;
              this._y=_parent.MC_TargetAmpmeter._y;
              _root.AmpmeterCorrect = true;
          }
          else
          {
              BounceBack.call(mx.transitions.easing.Bounce.easeOut);
             
              function BounceBack(easetype)
              {
              var begin = getProperty(Ampmeter,_x);
              var xEnd = 475.3;
              var mc = Ampmeter;
              Bounceback = new mx.transitions.Tween(mc, "_x", easeType, begin, xEnd, .2, true);
              }
             
              //this._x=475.3;
              //this._y=98.0;
          }
      }

       

      If you can help I will be very grateful

       

      Thanks

      DrTux

        • 1. Re: Drag and Drop game animated bounce back
          Ned Murphy Adobe Community Professional & MVP

          A couple of shouldn'ts... You shouldn't nest named functions.  You shouldn't place code on objects... but we'll pass on that for now.  Try using the following code on the object...

           

          on (press)
          {
          this.startX = 475.3;  // use this._x instead of 475.3
              this.startY = 98.0;   // use this._y instead of 98.0
          startDrag(this,false,0,0,600,400);
          }

           
          on (release)
          {
              stopDrag();
              if (this.hitTest(_parent.MC_TargetAmpmeter))
              {
                  this._x=_parent.MC_TargetAmpmeter._x;
                  this._y=_parent.MC_TargetAmpmeter._y;
                  _root.AmpmeterCorrect = true;
              }
              else
              {       
                  _parent.bounceBack(this);
              }
          }

           

          And place the following code in a frame on the timeline that conatins the dragged movieclip...

           

          import mx.transitions.Tween;
          import mx.transitions.easing.Bounce;
             
          function bounceBack(mc)
          {
              var goBackX = new Tween(mc, "_x", Bounce.easeOut, mc._x, mc.startX, .2, true);
              var goBackY = new Tween(mc, "_y", Bounce.easeOut, mc._y, mc.startY, .2, true);
          }

          • 2. Re: Drag and Drop game animated bounce back
            DrTux Level 1

            Hi

             

            Thanks that work great. Just have to move it in to all other movie clips now.


            Thanks again.

             

            DrTux

            • 3. Re: Drag and Drop game animated bounce back
              Ned Murphy Adobe Community Professional & MVP

              You're welcome

              • 4. Re: Drag and Drop game animated bounce back
                DrTux Level 1

                I'm going to be a pain again

                how would i use this for a reset button which when they are all correct and locked to their target locations tweens them all back.

                 

                At the moment I have the following code.

                 

                on (press)
                {
                   
                    _parent.MC_Ampmeter._x=475.3;
                    _parent.MC_Ampmeter._y=98.0;
                   
                    _parent.MC_Battery._x=471.8;
                    _parent.MC_Battery._y=215.8;
                   
                    _parent.MC_Resistor._x=471.8;
                    _parent.MC_Resistor._y=355.1;
                   
                    _parent.MC_Voltmeter._x=471.8;
                    _parent.MC_Voltmeter._y=281.4;
                   
                    _root.BatteryCorrect = false;
                    _root.VoltmeterCorrect = false;
                   
                    _root.AmpmeterCorrect = false;
                    _root.ResistorCorrect = false;
                }

                 

                Would it be a simple case of putting

                 

                   MC_Ampmeter.startX = 475.3;
                   MC_Ampmeter.startY = 98.0;

                    _parent.bounceBack(MC_Ampmeter);

                 

                in place for each one?

                 

                Thanks

                 

                DrTux

                • 5. Re: Drag and Drop game animated bounce back
                  Ned Murphy Adobe Community Professional & MVP

                  One should... You should try things... it rewarding when something you think about works.  Each piece will already have its startX and startY assigned if it has been dragged, so there's no need to assign them again.  I would use an on(release) instead of an on(press).

                  • 6. Re: Drag and Drop game animated bounce back
                    DrTux Level 1

                    Thanks, you got it working.

                    I didn't exactly do much lol.