skip to Main Content

I’m working on a project where I set a graphic object in motion, then pause and resume it after specific time intervals. However, I’m having trouble with the resume functionality.

Here’s a simplified version of my code:

class ProjectMain {

    init() {

        let graphic = new GraphicObject( nc.graphicAssets.WhiteBox, nc.mainScene, "Box" );
        graphic.position.addMotion.y( -300, 300, 2 );

        // create a PauseEvent
        nc.definePauseEvent("BoxPause");

        // wait 3 seconds, then call "pause", passing BoxPause as the PauseEvent
        nc.waitThen( 3, nc, "pause", [nc.pauseEvents.BoxPause] );

        // wait 6 seconds, then call "resume"
        nc.waitThen( 6, nc, "resume", nc.pauseEvents.BoxPause );
     }
}

The motion is correctly paused after 3 seconds, but the resume call after 6 seconds doesn’t seem to work. The box never resumes its motion.

What am I missing or doing wrong? How can I resume the motion?

2

Answers


  1. This is a great observation!

    What’s happening here is that the second waitThen() is also being paused. The reason for this is that the BoxPause event you created is not part of the default "pause immunity." As a result, the second waitThen() call is affected by the pause and doesn’t proceed as intended.

    To make this work as expected, you’ll need to ensure that the second waitThen() is immune to the BoxPause event.

    Here’s how you can do it:

    // Wait 6 seconds, then call "resume".
    // Note: At this point, "waitThen" will be paused by the BoxPause event.
    // To avoid this, pass the BoxPause event as the "pauseImmunity" parameter.
    nc.waitThen(6, nc, "resume", nc.pauseEvents.BoxPause, undefined, nc.pauseEvents.BoxPause);
    

    By including nc.pauseEvents.BoxPause as the pauseImmunity parameter, you ensure that the second waitThen() call is immune to the BoxPause event, allowing the box to resume moving after a 3-second pause.

    Login or Signup to reply.
  2. There is an alternative solution to achieve the same pausing and unpausing behavior without needing to explicitly make the second waitThen() immune to BoxPause.

    Here’s how it works using your original example:

    let graphic = new GraphicObject(nc.graphicAssets.WhiteBox, nc.mainScene, "Box");
    graphic.position.addMotion.y(-300, 300, 2);
    
    nc.definePauseEvent("BoxPause");
    
    // Pause with BoxPause event
    nc.waitThen(3, nc, "pause", [nc.pauseEvents.BoxPause]);
    
    // Set the default pause immunity to the BoxPause event
    nc.defaultPauseImmunity = nc.pauseEvents.BoxPause;
    
    // Now, waitThen() is immune to the BoxPause event by default, so resume will work as expected
    nc.waitThen(6, nc, "resume", nc.pauseEvents.BoxPause);
    

    While this doesn’t reduce the number of lines of code in this simple example, it becomes particularly useful in more complex scenarios. For instance, if you need to execute multiple actions in a block, setting the default pause immunity once at the top prevents you from adding immunity to each individual function call.

    Pseudocode example:

    {
        nc.waitThen(3, nc, "pause", [nc.pauseEvents.BoxPause]);
    
        doABunchOfOtherStuff();
    }
    
    function doABunchOfOtherStuff() {
        // Set the default pause immunity to BoxPause
        nc.defaultPauseImmunity = nc.pauseEvents.BoxPause;
    
        // Now all subsequent code is immune to BoxPause
    }
    

    This approach simplifies handling pause immunity across multiple actions, especially when the pause event affects several parts of your logic.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search