Ping pong tween movement


(Leandro Maguna) #1

hi guys! I’ve been playing with flashpunk for a while, i’m trying to make something like a continuous bouncing movemente like this http://imgur.com/mMWKsxM (it’s made in construct 2) but i can’t understant how linearmotion or any tween can do this.

movement = new VarTween(null, LOOPING);
movement.tween(this, "x", 480, 5,Ease.sineInOut);
addTween(movement);

The code above works but it’s only one way. How could i make this in ping pong way? Thanks!


(Martí Angelats i Ribera) #2

Basically do the same that the Tween does (it’s more like copy paste).

//variables we need
public function _totalTimer:Number = 0;

//in the update function of your custom entity
_totalTimer += FP.elapsed();
x = Ease.sineInOut(_totalTimer*k); //this k is a number that controls the speeed. I think it is the inverse of the time you want in seconds

(Jacob Albano) #3

He’s asking for a tween, and this is not a tween. Using a sine wave is a good approach but let’s answer the question as it was asked instead of suggesting whole new approaches.


(Jacob Albano) #4

You can achieve this effect with a completion callback:

var target:Object = this;

function to():void {
    var movement:VarTween = new VarTween(from, ONESHOT);
    movement.tween(target, "x", 480, 5, Ease.sineOut);
    addTween(movement);
}

function from():void {
    var movement:VarTween = new VarTween(to, ONESHOT);
    movement.tween(target, "x", 0, 5, Ease.sineOut);
    addTween(movement);
}

to();

This can all go inside a function, since it’s legal to define functions inside of other functions. The line var target:Object = this is important because inside the callbacks this can refer to something else than it normally would.


(Martí Angelats i Ribera) #5

Oh sorry, i missunderstood the question.

Actually if you think of this problem as an Entity attached into a elastic rope or an spring, the resulting function is actually a sine or cosine function.


(Leandro Maguna) #6

I didn’t think in this aproach, looks cool to try. I found this while searching, I’ll give it a try soon