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?
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.