5 Replies Latest reply on Oct 27, 2010 7:36 AM by kglad

# [as2] Making a tween without using the tween class take 2

Hi. The previous topic was ok to use with paramaters that makes a square only.

If you try to make a movie clip travel along a rectangle diagonal within 90 seconds, the movie clip makes some curves.

Any help?

Thanks

• ###### 1. Re: [as2] Making a tween without using the tween class take 2

you must be using the code i suggested incorrectly.

• ###### 2. Re: [as2] Making a tween without using the tween class take 2

Sorry, no, I´m using your code correctly.

What happens is because the parameters don´t make a square but a rectangle.

Try to use your code with the following parameters.

x1=0

y1=0

x2=550

y2=400

If you draw a line from 0, 0 to 550, 400, you can notice

that the movie clip makes a curve.

Thanks

• ###### 3. Re: [as2] Making a tween without using the tween class take 2

the code i gave before induces no curve.  but because flash rounds object positions to the nearest .05 pixel you may be seeing an accumulation of rounding errors if you're using certain parameters and you're very picky about object position.  if that's the case, use the following to prevent rounding errors from accumulating:

function initTweenF(mc:MovieClip, x1:Number, y1:Number, x2:Number, y2:Number, sec:Number):Void {
var freq:Number = 70;
clearInterval(mc.tweenI);
mc.x = x1;
mc.y = y1
mc._x = mc.x;
mc._y = mc.y;

mc.tweenI = setInterval(tweenF, freq, mc, (x2-x1)*freq/(1000*sec), (y2-y1)*freq/(1000*sec), x2, y2);
}
function tweenF(mc:MovieClip, xInc:Number, yInc:Number, x2:Number, y2:Number):Void {
mc.x += xInc;
mc.y += yInc;
mc._x = mc.x;
mc._y = mc.y;
if (Math.abs(mc._x-x2)<Math.abs(xInc) && Math.abs(mc._y-y2)<Math.abs(yInc)) {
mc._x = x2;
mc._y = y2;
clearInterval(mc.tweenI);
}
}

• ###### 4. Re: [as2] Making a tween without using the tween class take 2

Could you explain the code that are commented?

Thank you so much

function initTweenF(mc:MovieClip, x1:Number, y1:Number, x2:Number, y2:Number, sec:Number):Void {
var freq:Number = 70;
clearInterval(mc.tweenI);

//this is just a variable?
mc.x = x1;
mc.y = y1

//

mc._x = mc.x;
mc._y = mc.y;

//the formula freq/(1000*sec) can you explain it?

mc.tweenI = setInterval(tweenF, freq, mc, (x2-x1)*freq/(1000*sec), (y2-y1)*freq/(1000*sec), x2, y2);
}
function tweenF(mc:MovieClip, xInc:Number, yInc:Number, x2:Number, y2:Number):Void {
mc.x += xInc;
mc.y += yInc;
mc._x = mc.x;
mc._y = mc.y;
if (Math.abs(mc._x-x2)<Math.abs(xInc) && Math.abs(mc._y-y2)<Math.abs(yInc)) {
mc._x = x2;
mc._y = y2;
clearInterval(mc.tweenI);
}
}

• ###### 5. Re: [as2] Making a tween without using the tween class take 2

function initTweenF(mc:MovieClip, x1:Number, y1:Number, x2:Number, y2:Number, sec:Number):Void {
var freq:Number = 70;
clearInterval(mc.tweenI);

//this is just a variable?

// yes, flash does not round variables to the nearest .05 so these

// two variables will not accumulate/compound rounding errors

mc.x = x1;
mc.y = y1

//

mc._x = mc.x;
mc._y = mc.y;

//the formula freq/(1000*sec) can you explain it?

// tweenF will be called 1000*sec/freq times so mc.x, for example, will be

// incremented 1000*sec/freq times.  ie, mc.x after all those calls will be

// x1+(x2-x1)*freq/(1000*sec) * 1000*sec/freq = x1+(x2-x1) = x2

mc.tweenI = setInterval(tweenF, freq, mc, (x2-x1)*freq/(1000*sec), (y2-y1)*freq/(1000*sec), x2, y2);
}
function tweenF(mc:MovieClip, xInc:Number, yInc:Number, x2:Number, y2:Number):Void {
mc.x += xInc;
mc.y += yInc;
mc._x = mc.x;
mc._y = mc.y;
if (Math.abs(mc._x-x2)<Math.abs(xInc) && Math.abs(mc._y-y2)<Math.abs(yInc)) {
mc._x = x2;
mc._y = y2;
clearInterval(mc.tweenI);
}
}