Vizard 7 » Tutorials & Examples » Flow control » Actions » Tutorial: Applying an action
7.6

Tutorial: Applying an action

Now we need to apply our new actions to the wheel barrow by using the command <node3d>.addAction(). Add the following code:

wheel.addAction(moveForward)
wheel.addAction(turnRight)

Run your script now. You will see a wheel barrow moving forward then turn 90 degrees to the right. To make the wheel barrow move in a complete square all we would need to do is repeat the above code 3 more times. However, if we plan on doing this multiple times in our script, the size of our code will start to increase very quickly. The vizact library has an action called vizact.sequence() which makes this easier for us. The sequence action is simply a sequence of actions that can be represented by one action.

 

Replace the above code with the following code to create an action that will move the wheel barrow in a complete square:

moveInSquare = vizact.sequence(moveForward,turnRight,4)

This creates an action that will execute the moveForward and turnRight actions 4 times. Now apply the moveInSquare action to the wheel barrow:

wheel.addAction(moveInSquare)

Run your script to see the wheel barrow move in a complete circle. We can reuse this action as many times as we want. For instance, let's allow the user to move the wheel barrow in a circle by pressing the spacebar. Replace the above line with the following:

vizact.onkeydown(' ', wheel.addAction, moveInSquare)

This says that when the spacebar is pressed wheel.addAction will be called and moveInSquare will be passed as the argument. Now run your script and press the spacebar to trigger this action. If you press the spacebar multiple times in a row, the action will be queued and performed after all previous actions are completed. Let's allow the user to cancel all the queued actions and the current action by pressing the 'c' key. Add another vizact.onkeydown to do this.

vizact.onkeydown('c', wheel.clearActions)

Now run your script again. Press the spacebar multiple times in a row. While the wheel barrow is moving, press the 'c' key and it should stop in its tracks.

Action objects

Building an action

Applying an action