Detect if a motion is ended


#1

hello,

i have follows problem(s)…

i have an entity, with several tweens and motions… (at the moment there are 6 different tweens and motions, it will be more at the end of the project)

from time to time the entity gets a call to make a movement… no problem… 2 frames later perhaps it gets the next call to make another (!) movement… thats makes the problem:

how i can detect, the old (first called) movement is already finished?

i cannot do a chain with callbacks! the callback of every tween has already a(nother) callback… the calls (and the tweens coupled to it) are independent and must be it further…

thanks in advance kolibri


(Eric Dand) #2

The easiest way is to find a way to detect if your Entity is already tweening. If so, do a little bit of extra handling.

I would suggest you keep track of the state of your entity with a “state” flag, and if a tween preempts another, call the callback yourself just as if the first tween had completed. Basically, if your entity is doing a motion/tween, set its state to something like TWEENING, like this:

override public function update():void 
{
    // Some code to determine if this.shouldTween here...
    if (this.shouldTween)
    {
        if (this.state == TWEENING)
        {
            doneTweening(); // Call the "tween completed" callback manually.
        }
        this.state = TWEENING; // Set the state to tweening.
        myTween.start();
    }
    // More code here...
}

// And the doneTweening callback...
private function doneTweening():void
{
    this.state = STILL; // STILL = not TWEENING.
    // Do more callback logic here...
}

If you need to keep track of which of your six tweens is going on, you could keep a variable called currentTween and do some conditional logic based on that. If you need a different callback depending on whether a tween completed or was preempted, the above method should allow for that too – just write a different callback and call that yourself in the case of preemption.


#3

okay… thank you very much for your help…

i think that can be the solution :slight_smile:


(christopf) #4

dont you need another if for

this.state = TWEENING;
myTween.start();

? i thought otherwise the doneTweening() is somehow useless, caus at the end of they day the state is always TWEENING


(Eric Dand) #5

It’s already surrounded by an if block checking the somewhat-contrived “shouldTween” flag. I assume the flag is set somewhere else in code.