16 Replies Latest reply on Oct 26, 2010 4:16 PM by kglad

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

Hi.

Is there any way to make a movie clip start at a given x and y point, and ends at another x and y point, in 90 seconds?.

I´ve done a lot of code but I´m not getting a good result.

The main problem is, I want the movie clip to travel along a straight line. And by this code below the "b" movie clips makes some curves.

Here´s what I´ve done.

"p" is point movie clip

"b" is ball movie clip

fullTime = 90000;
_currentTime = fullTime;
oldTime = undefined;
onEnterFrame = function () {
if (count) {
_currentTime = _currentTime-(Math.abs(oldTime-getTimer()));
}
count = true;
oldTime = getTimer();
speedX = (p._x-b._x)/((_currentTime*24)/1000);
speedY = (p._y-b._y)/((_currentTime*24)/1000);
b._x += speedX;
b._y += speedY;
if (b._y>=p._y) {
//do something

}
};

Thanks

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

I'm not sure why you don't want to use the Tween class.

Also I'm not seeing any curves from the "b" clip. Can you post a swf that shows what you are seeing?

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

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.tweenI = setInterval(tweenF,freq,mc,(x1-x2)*1000/(sec*freq),(y1-y2)*1000/(sec*freq),x2,y2);

}

function tweenF(mc:MovieClip,xInc:Number,yInc:Number,x2:Number,y2:Number):Void{

mc._x+=xInc;

mc._y+=yInc;

if(Math.abs(mc._x-x2)<1&&Math.abs(mc._y-y2)<1){

mc._x=x2;

mc._y=y2;

clearInterval(mc.tweenI);

}

}

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

The "b" movie clip does smooth curves along the line.

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

couldn´t get the result

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

my error.  that should be:

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.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;
if (Math.abs(mc._x-x2)<1 && Math.abs(mc._y-y2)<1) {
mc._x = x2;
mc._y = y2;
clearInterval(mc.tweenI);
}
}

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

I know it may seems unusual. But, the code makes what one of my previous code makes.

It doesn´t travel in a straight line. I mean, a line that is created from the start point to the end point.

You can test it for yourself, if you want.

So, I thought, it could be good, if I calculate the distance at each function call. It worked a little.

But I still didn´t get what I wanted.

So, I thought it could be the difference of time from one function call to another ( I was making on onEnterFrame ).

So, I´ve made the code that I´ve posted here.

And the result was smooth curves along the line.

Any idea or workaround?

Thanks alot

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

Here´s what happens, you can see the gray circle isn´t aligned with the green line.

The dark point at the gray circle should be aligned with the green line, while moving down.

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

The registration point of your circle is most likely not where you think it is.

Or if that center point is making curves around the green line, then you must have some kind of tween (like a timeline tween) inside of your grey circle clip. There is nothing in the code you provided (or Kglad's nice reusable code) that would cause the center point to move relative to the green line.

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

the code i suggested works without problem.  what makes you think it doesn't work?

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

To me it works ok with short time to travel.

But not with 90 seconds.

Have you tested with 90 seconds?

If yes, and it works, please send the prototype fla to marcelozepgames@yahoo.com

Thanks

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

Unfortunately there´s no tween inside the gray circle

I really don´t know why it doesn´t work

Both movie clips are top-left registration point.

And the line crosses exactly the center of both movie clips.

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

It seems right .

I´ll test it thoroughly.

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

Try to make a non 45 angle diagonal with the parameters like 550 for x2 and 400 for y2.

You can see it doesn´t work.

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

Sorry KGlad the code only works for 45º angles.

Try to see what happens with another angles, using 550 for x2 and 400 for y2.

Thanks

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

use:

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.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;
trace(mc._x+" "+mc._y+" "+xInc+" "+yInc);
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);
}
}