Search code examples
spark-ar-studio

How do I stop an animation loop and reset it after an object turns visible?


I started picking up SparkAR Studio recently and I am not used to node programming.

I am making a simple blinking game where the user blinks their eyes to control an object.

The issue is that after the 'Game Over' screen is displayed (Checked Visibility in the Object properties), the loop still runs in the background. The problem with this is when the user taps on the screen to retry, if the timing is incorrect, the loop will cause the obstacle to stop right in front of the character, causing the player to lose again.

Is there a way to stop an animation loop the moment an object turns visible / invisible?

Either via script / Patch Editor.

(If you the working file is required, I will be glad to provide it)


Solution

  • In your script, there are very simple methods available:

    • driver.reset(); (if you need a set back)
    • driver.stop();

    In case you need to implement all the animation in the script:

    const Animation = require('Animation');
    //set up the length of the animations, 1000 = 1 second
    const driverParameters = {durationMilliseconds: 1000, loopCount: Infinity, mirror: false};
    var driver = Animation.timeDriver(driverParameters);
    var sampler = Animation.samplers.linear(startValue, endValue);
    myObject.transform.y = Animation.animate(driver, sampler);
    // requires a first call
    driver.start();