Play for a Specified Number of Times

This tutorial makes use of an "if" statement, a feature that can deliver a considerable amount of power for the creation of interactive content.

This tutorial follows on from where the switch animation on and off in 3d tutorial left off and shares the same robot demo from the help menu switch anim in 3d demo:

The first operation for this tutorial begins at operation 9 so it is necessary to run the demo for the previous tutorial and have some understanding of it before beginning this tutorial.

Unlike the previous tutorial where we create a toggle switch for our box (alternates between on and off each time we click), here we create a "switch on" only type switch but the animation will automatically stop playing after a specified number of cycles.

9. Add global variable to remember turn off time

Variables are nothing more than memory. Our animation script will need to be able to compare the current time to some point in time to know when it should stop. For this we need some memory to store this time and so this is why a variable is added. A global variable unlike a local variable remembers it's value from frame to frame. A variable is global if it is not inside a function (not a descendent of a function node). So by adding the variable "r" to the scene node we can feel rest assured our memory for our future event will not be forgotten by the time it is needed. The variable is a double type because to store time we need "double floating point precision" but don't let this scare you, it is just a number type variable suitable for the task of storing time.

10. Only switch on (don't toggle)

The toggle script is simplified to:

spin.play = true

This simply sets the animation to play when we click the box.

11. Save click time + 3 cycles of time

The next line for our onTouchTime function (that only runs when we click on the box) is entered:

r = getTime() + spin.period * 3

This line gets the current time, adds 3 cycles of time to it and stores this value to the variable "r".

Note: the asterisk * is the standard multiply character used in programming languages.

12. Add "if" statement

The "if" statement is added to our onFrame function:

if(getTime() < r)

and our line that causes the box to spin:

p.rotation = Rotation(0, 0, 1, PI * 2 * v)

is draged so that it branches out from the line containing the "if" statement.

The "if" statement asks if the current time is less than r.

The code for our onFrame function reads as:

If the current time is less than r, spin the box.

Note: unlike the  onTouchTime function which runs only when we click on the box, the  onFrame function runs for each frame. So our script does not stop the onFrame function from running it only stops some code within the function from running.

13. Click once to cycle 3 times

The box is clicked and the box animates for 3 cycles and then stops.


 
Stopping the Anim Node After the Specified Time

 

In the previous tutorial we never actually stopped the Anim node to stop the animation. This can have advantages but for some tasks we may want to turn off the Anim node which is  shown in this tutorial. This tutorial begins at operation 14 continuing on from the previous tutorial.

14. Stop Anim node too

The spin line:

p.rotation = Rotation(0, 0, 1, PI * 2 * v)

is dragged from the "if" line to below the "if" as a sibling so that the "if" no longer makes a decision whether to run the spin line when the onFrame function is called.

A new CodeLine node is added to the "if" line and the code:

spin.play = false

is entered for the new line.

This causes the Anim node to stop when the "if" is true so that the onFrame function stops being called.

To make it so that this line is only executed after the current time passes the time specified by r, the "less than" symbol "<" is replaced with a "greater than" symbol ">"

This now reads as:

If the current time is greater than r stop the Anim node.

15. Complete


[3d Modelling Software] [Tutorials] [Forum] [Features] [Download] [Gallery] [FAQ] [Worlds] [Avatars] [Links] [Thyme]