snapingBack

  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

With a ball_mc instance on the stage....

how do I compute the direction for the ball to snapBack to its starting point, travelling along the line indicated by the tracker?

do I return to Math.atan instead of Math.atan2?

Thank you in advance

// actionscript 2.0

var radDeg:Number=180/Math.PI;
var posX:Number=Stage.width/2;
var posY:Number=Stage.height/2;

var tracker:MovieClip=this.createEmptyMovieClip("tracker",0);

tracker._x=posX;
tracker._y=posY;
tracker.lineStyle(0,0x0,100);
tracker.moveTo(0,0);
tracker.lineTo(100,0);


this.ball_mc.onPress=function(){
posX=this._x;
posY=this._y;
this.startDrag(false,Stage.width,Stage.height,20);
_root.tracker._x=posX;
_root.tracker._y=posY;
}

this.ball_mc.onRelease=this.ball_mc.onReleaseOutside=function(){
stopDrag();
snapBack();
}

tracker.onMouseMove=function(){
var angle:Number=Math.atan2(_ymouse-this._y, _xmouse-this._x);
tracker._rotation=angle*radDeg;
updateAfterEvent();
}

function snapBack(){
this.ball_mc.onEnterFrame=function(){
//ball travels line back to startDrag start point

}
}
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

is the direction a line, an arc, a circle or what?
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

straight back along a straight line to its starting point
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

do you need something like this ?
btw, why you need atan2 or atan?
don't you know the straight line equation?
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

thank you iceCold,

that's a great resource.

I am trying to wirte a script that makes the mc (onRelease) travel back to where it was first pressed - at the start of the Drag.

onRelease (after the mc has been dragged - anywhere - around the stage) the mc travels back along a straight line to where it was when it was pressed. It does not need to ease to a stop -- because it will fade from view before it returns all the way back.

mc.onRelease=function(){
stopDrag();
snapBack();
}

function snapBack(){
this.onEnterFrame=function(){
this._x-=20;
this._y+=30; //here it will travel down in the general
//direction of where it came from, not back
//to its original position.

this._alpha-=8;
if(this._alpha<10){
this.removeMovieClip();
}
}
}

I was using atan2 to draw a straight line which follows the mouse. (refer to the original post) in an attempt to find the math for line back to the origin.

but maybe it's simpler than that.
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

yes, much simpler.
you can either use the tween class which is implemented in mx,
or just calculate the y while you change the x with a certain amount.
1)check this post
2)straight line equation:
y = y0 + (x - x0)*(y1-y0)/(x1-x0);
x0, y0 ---------------x, y----------------------- x1, y1
(start point......... current point............... end point)

that means, when you start to drag, onPress, save the position of your mc -> that would be the end point.
When you stop drag, you have your start point.
Then, start to add to the x ... let's say 1/10 of the distance (x0 - x1) / 10 -> if x0 - x1 is negative, then it will add a negative value (substract), meaning it will move from right to left. If it's positive, it will add a positive value -> move from left to right. So that means you don't have to check if x0 - x1 is negative or positive.
After this, calculate the y with the given formula and set your movie coordinates to the calculated x, y.
This will repeat untill Math.round(x1) == Math.round(x);
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

yes yes that's it

a straight line equation

so x0,y0 are set as values in the onPress handler and xN,yN are set in the onRelease,

I am then trying to pass that info to a onEnterFrame handler in the form of this._x += someValue, this._y+= someValue

is that right?
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

yes, almost right.
someValue added to _x is fixed,
_y is calculated, not added
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

thanks again professor
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

you're welcome. Let me know if you have problems implementing this.

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 11 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.