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
This is a great observation!
What’s happening here is that the second
waitThen()
is also being paused. The reason for this is that theBoxPause
event you created is not part of the default "pause immunity." As a result, the secondwaitThen()
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 theBoxPause
event.Here’s how you can do it:
By including
nc.pauseEvents.BoxPause
as thepauseImmunity
parameter, you ensure that the secondwaitThen()
call is immune to theBoxPause
event, allowing the box to resume moving after a 3-second pause.There is an alternative solution to achieve the same pausing and unpausing behavior without needing to explicitly make the second
waitThen()
immune toBoxPause
.Here’s how it works using your original example:
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:
This approach simplifies handling pause immunity across multiple actions, especially when the pause event affects several parts of your logic.