Solved - Creating a button to toggle between animation timelines

Hi, I’m relatively new to ZapWorks and trying to code a button interaction. I have a model of a pyramid shape that can fold closed and fold open again when the user presses a button. To make this I’ve written a simple script:

  let AnimClosed:boolean = false;
 
  parent.on("pointerdown", (e) => {
  
  	if (AnimClosed === false) {
  		symbol.controllers.sqPyramidAnimation.elements.sqPyrFoldClosed.play(1, false);
  		AnimClosed = true;} 
  
  	else if (AnimClosed === true) {
 		symbol.controllers.sqPyramidAnimation.elements.sqPyrFoldOpen.play(1, false);
  		AnimClosed = false;}
  });

This code works perfectly the first and second time the button is pressed, closing then opening the object as expected, but the third time the ‘FoldClosed’ animation does not play properly.

The boolean is definitely functioning, when I change the ‘loop’ parameters from false to true so the animations play indefinitely and I can see it switches between them correctly on the third press. Right now, I think the problem is to do with the animations not wanting to play more than once, but I can’t find a way to make them play from the beginning every time the button is pressed (except by setting this up in the action editor, but I’m not sure how to create a toggle button from there).

Can anyone please provide some insight?

Thanks very much!

EDIT, SOLVED:
Really simple solution in case it helps anyone else, added the reset() function like so:

 if (AnimClosed === false) {

		symbol.controllers.sqPyramidAnimation.elements.sqPyrFoldClosed.reset();
		symbol.controllers.sqPyramidAnimation.elements.sqPyrFoldClosed.play(1, false);
		AnimClosed = true;} 

 	else if (AnimClosed === true) {

		symbol.controllers.sqPyramidAnimation.elements.sqPyrFoldOpen.reset();
		symbol.controllers.sqPyramidAnimation.elements.sqPyrFoldOpen.play(1, false);
 		AnimClosed = false;}
1 Like