Skip to main content
Participant
July 5, 2010
Answered

Drag and Drop game animated bounce back

  • July 5, 2010
  • 1 reply
  • 1196 views

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

This topic has been closed for replies.
Correct answer Ned Murphy

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);
}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
July 5, 2010

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);
}

DrTuxAuthor
Participant
July 5, 2010

Hi

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


Thanks again.

DrTux

Ned Murphy
Legend
July 5, 2010

You're welcome