Alice 3: Controlling the Behavior of Animations

Exclusive offer: get 50% off this eBook here
Alice 3 Cookbook

Alice 3 Cookbook — Save 50%

79 recipes to harness the power of Alice 3 for teaching students to build attractive and interactive 3D scenes and videos

$26.99    $13.50
by Vanesa S. Olsen | July 2011 | Open Source

Alice is a free and innovative 3D programming environment that makes it easy to create an animation for telling a story, playing an interactive game, or a video to share on the Web. Alice is a teaching tool designed as a revolutionary approach to teaching and learning introductory programming concepts.

In this article by Vanesa S. Olsen, author of Alice 3 Cookbook, we will cover:

  • Performing many statements in order
  • Using a counter to run statements many times
  • Running statements for different actors
  • Running statements for many actors
  • Running many statements at the same time
  • Repeating statements at the same time

 

Alice 3 Cookbook

Alice 3 Cookbook

79 recipes to harness the power of Alice 3 for teaching students to build attractive and interactive 3D scenes and videos

        Read more about this book      

(For more resources related to this subject, see here.)

Introduction

You need to organize the statements that request the different actors to perform actions. Alice 3 provides blocks that allow us to configure the order in which many statements should be executed. This article provides many tasks that will allow us to start controlling the behavior of animations with many actors performing different actions.

We will execute many actions with a specific order. We will use counters to run one or more statements many times. We will execute actions for many actors of the same class. We will run code for different actors at the same time to render complex animations.

Performing many statements in order

In this recipe, we will execute many statements for an actor with a specific order. We will add eight statements to control a sequence of movements for a bee.

Getting ready

We have to be working on a project with at least one actor. Therefore, we will create a new project and set a simple scene with a few actors.

  1. Select File | New... in the main menu to start a new project. A dialog box will display the six predefined templates with their thumbnail previews in the Templates tab.
  2. Select GrassyProject.a3p as the desired template for the new project and click on OK. Alice will display a grassy ground with a light blue sky.
  3. Click on Edit Scene, at the lower right corner of the scene preview. Alice will show a bigger preview of the scene and will display the Model Gallery at the bottom.
  4. Add an instance of the Bee class to the scene, and enter bee for the name of this new instance. First, Alice will create the MyBee class to extend Bee. Then, Alice will create an instance of MyBee named bee. Follow the steps explained in the Creating a new instance from a class in a gallery recipe, in the article, Alice 3: Making Simple Animations with Actors.
  5. Add an instance of the PurpleFlower class, and enter purpleFlower for the name of this new instance.
  6. Add another instance of the PurpleFlower class, and enter purpleFlower2 for the name of this new instance. The additional flower may be placed on top of the previously added flower.
  7. Add an instance of the ForestSky class to the scene.
  8. Place the bee and the two flowers as shown in the next screenshot:

    Alice 3: Controlling the Behavior of Animations

How to do it...

Follow these steps to execute many statements for the bee with a specific order:

  1. Open an existing project with one actor added to the scene.
  2. Click on Edit Code, at the lower-right corner of the big scene preview. Alice will show a smaller preview of the scene and will display the Code Editor on a panel located at the right-hand side of the main window.
  3. Click on the class: MyScene drop-down list and the list of classes that are part of the scene will appear.
  4. Select MyScene | Edit run.
  5. Select the desired actor in the instance drop-down list located at the left-hand side of the main window, below the small scene preview. For example, you can select bee. Make sure that part: none is selected in the drop-down list located at the right-hand side of the chosen instance.
  6. Activate the Procedures tab. Alice will display the procedures for the previously selected actor.
  7. Drag the pointAt procedure and drop it in the drop statement here area located below the do in order label, inside the run tab. Because the instance name is bee, the pointAt statement contains the this.bee and pointAt labels followed by the target parameter and its question marks ???. A list with all the possible instances to pass to the first parameter will appear.
  8. Click on this.purpleFlower. The following code will be displayed, as shown in the next screenshot:

    this.bee.pointAt(this.purpleFlower)

    Alice 3: Controlling the Behavior of Animations

  9. Drag the moveTo procedure and drop it below the previously dropped procedure call. A list with all the possible instances to pass to the first parameter will appear. Select this.purpleFlower getPart ??? and then IStemMiddle_IStemTop_IHPistil_IHPetal01, as shown in the following screenshot:

    Alice 3: Controlling the Behavior of Animations

  10. Click on the more... drop-down menu button that appears at the right-hand side of the recently dropped statement. Click on duration and then on 1.0 in the cascade menu that appears.
  11. Click on the new more... drop-down menu that appears. Click on style and then on BEGIN_AND_END_ABRUPTLY. The following code will be displayed as the second statement:

    this.bee.moveTo(this.purpleFlower.getPart(IStemMiddle_IStemTop_
    IHPistil_IHPetal01), duration: 1.0, style: BEGIN_AND_END_ABRUPTLY)

  12. Drag the delay procedure and drop it below the previously dropped procedure call. A list with all the predefined direction values to pass to the first parameter will appear. Select 2.0 and the following code will be displayed as the third statement:

    this.bee.delay(2.0)

  13. Drag the moveAwayFrom procedure and drop it below the previously dropped procedure call. Select 0.25 for the first parameter. Click on the more... drop-down menu button that appears and select this.purpleFlower getPart ??? and then IStemMiddle_IStemTop_IHPistil_IHPetal01. Click on the additional more... drop-down menu button, on duration and then on 1.0 in the cascade menu that appears. Click on the new more... drop-down menu that appears, on style and then on BEGIN_ABRUPTLY_AND_END_GENTLY. The following code will be displayed as the fourth statement:

    this.bee.moveAwayFrom(0.25, this.purpleFlower.getPart(IStemMiddle_
    IStemTop_IHPistil_IHPetal01), duration: 1.0, style: BEGIN_
    ABRUPTLY_AND_END_GENTLY)

  14. Drag the turnToFace procedure and drop it below the previously dropped procedure call. Select this.purpleFlower2 getPart ??? and then IStemMiddle_IStemTop_IHPistil_IHPetal05. Click on the additional more... drop-down menu button, on duration and then on 1.0 in the cascade menu that appears. Click on the new more... drop-down menu that appears, on style and then on BEGIN_ABRUPTLY_AND_END_GENTLY. The following code will be displayed as the fifth statement:

    this.bee.turnToFace(this.purpleFlower2.getPart(IStemMiddle_
    IStemTop_IHPistil_IHPetal05), duration: 1.0, style: BEGIN_
    ABRUPTLY_AND_END_GENTLY)

  15. Drag the moveTo procedure and drop it below the previously dropped procedure call. Select this.purpleFlower2 getPart ??? and then IStemMiddle_IStemTop_IHPistil_IHPetal05. Click on the additional more... drop-down menu button, on duration and then on 1.0 in the cascade menu that appears. Click on the new more... drop-down menu that appears, on style and then on BEGIN_AND_END_ABRUPTLY. The following code will be displayed as the sixth statement:

    this.bee.moveTo(this.purpleFlower2.getPart(IStemMiddle_IStemTop_
    IHPistil_IHPetal05), duration: 1.0, style: BEGIN_AND_END_GENTLY)

  16. Drag the delay procedure and drop it below the previously dropped procedure call. A list with all the predefined direction values to pass to the first parameter will appear. Select 2.0 and the following code will be displayed as the seventh statement:

    this.bee.delay(2.0)

  17. Drag the move procedure and drop it below the previously dropped procedure call. Select FORWARD and then 10.0. Click on the more... drop-down menu button, on duration and then on 10.0 in the cascade menu that appears. Click on the additional more... drop-down menu that appears, on asSeenBy and then on this.bee. Click on the new more... drop-down menu that appears, on style and then on BEGIN_AND_END_ABRUPTLY. The following code will be displayed as the eighth and final statement. The following screenshot shows the eight statements that compose the run procedure:

    this.bee.move(FORWARD, duration: 10.0, asSeenBy: this.bee, style:
    BEGIN_ABRUPTLY_AND_END_GENTLY)

    (Move the mouse over the image to enlarge it.)

  18. Select File | Save as... from Alice's main menu and give a new name to the project. Then you can make changes to the project according to your needs.

How it works...

When we run a project, Alice creates the scene instance, creates and initializes all the instances that compose the scene, and finally executes the run method defined in the MyScene class. By default, the statements we add to a procedure are included within the do in order block. We added eight statements to the do in order block, and therefore Alice will begin with the first statement:

this.bee.pointAt(this.purpleFlower)

Once the bee finishes executing the pointAt procedure, the execution flow goes on with the next statement specified in the do in order block. Thus, Alice will execute the following second statement after the first one finishes:

this.bee.moveTo(this.purpleFlower.getPart(IStemMiddle_IStemTop_
IHPistil_IHPetal01), duration: 1.0, style: BEGIN_AND_END_ABRUPTLY)

The do in order statement encapsulates a group of statements with a synchronous execution. Thus, when we add many statements within a do in order block, these statements will run one after the other. Each statement requires its previous statement to finish before starting its execution, and therefore we can use the do in order block to group statements that must run with a specific order.

The moveTo procedure moves the 3D model that represents the actor until it reaches the position of the other actor. The value for the target parameter is the instance of the other actor. We want the bee to move to one of the petals of the first flower, purpleFlower, and therefore we passed this value to the target parameter:

this.purpleFlower.getPart(IStemMiddle_IStemTop_IHPistil_IHPetal01)

We called the getPart function for purpleFlower with IStemMiddle_IStemTop_IHPistil_IHPetal01 as the name of the part to return. This function allows us to retrieve one petal from the flower as an instance. We used the resulting instance as the target parameter for the moveTo procedure and we could make the bee move to the specific petal of the flower.

Once the bee finishes executing the moveTo procedure, the execution flow goes on with the next statement specified in the do in order block. Thus, Alice will execute the following third statement after the second one finishes:

this.bee.delay(2.0)

The delay procedure puts the actor to sleep in its current position for the specified number of seconds. The next statement specified in the do in order block will run after waiting for two seconds.

The statements added to the run procedure will perform the following visible actions in the specified order:

  1. Point the bee at purpleFlower.
  2. Begin and end abruptly a movement for the bee from its position to the petal named IStemMiddle_IStemTop_IHPistil_IHPetal01 of purpleFlower. The total duration for the animation must be 1 second.
  3. Make the bee stay in its position for 2 seconds.
  4. Move the bee away 0.25 units from the position of the petal named IStemMiddle_IStemTop_IHPistil_IHPetal01 of purpleFlower. Begin the movement abruptly but end it gently. The total duration for the animation must be 1 second.
  5. Turn the bee to the face of the petal named IStemMiddle_IStemTop_IHPistil_IHPetal05 of purpleFlower2. Begin the movement abruptly but end it gently. The total duration for the animation must be 1 second.
  6. Begin and end abruptly a movement for the bee from its position to the petal named IStemMiddle_IStemTop_IHPistil_IHPetal05 of purpleFlower2. The total duration for the animation must be 1 second.
  7. Make the bee stay in its position for 2 seconds.
  8. Move the bee forward 10 units. Begin the movement abruptly but end it gently. The total duration for the animation must be 10 seconds. The bee will disappear from the scene.

The following screenshot shows six screenshots of the rendered frames:

(Move the mouse over the image to enlarge it.)

There's more...

When you work with the Alice code editor, you can temporarily disable statements. Alice doesn't execute the disabled statements. However, you can enable them again later. It is useful to disable one or more statements when you want to test the results of running the project without these statements, but you might want to enable them back to compare the results.

To disable a statement, right-click on it and deactivate the IsEnabled option, as shown in the following screenshot:

Alice 3: Controlling the Behavior of Animations

The disabled statements will appear with diagonal lines, as shown in the next screenshot, and won't be considered at run-time:

Alice 3: Controlling the Behavior of Animations

To enable a disabled statement, right-click on it and activate the IsEnabled option.

Alice 3 Cookbook 79 recipes to harness the power of Alice 3 for teaching students to build attractive and interactive 3D scenes and videos
Published: April 2011
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:
        Read more about this book      

(For more resources related to this subject, see here.)

Using a counter to run statements many times

In this task, we will learn to group many statements into a block that repeats the execution of the code a specific number of times. We will create a loop that repeats the execution of a group of statements with a synchronous execution the number of times specified by its required integer parameter.

Getting ready

We have to be working on a project with many actors. Therefore, we will use an existing project that has two flowers and a bee.

  1. Open the project saved in the Performing many statements in order recipe in this article.
  2. Click on Edit Code, at the lower-right corner of the big scene preview. Alice will show a smaller preview of the scene and will display the Code Editor on a panel located at the right-hand side of the main window.
  3. Click on the class: MyScene drop-down list and the list of classes that are part of the scene will appear.
  4. Select MyScene | Edit run.

How to do it...

Follow these steps to create a loop that repeats the execution of a group of statements:

  1. Drag the count statement, located at the top, and drop it below the first statement. Alice will display a green line indicating the position in which this new statement will be inserted, as shown in the following screenshot:

    Alice 3: Controlling the Behavior of Animations

  2. Select Other Integer... and the Enter custom integer dialog box will appear. Enter 5 and click on OK. The count up to statement will display a drop statement here placeholder.
  3. Place the mouse pointer over the light shade at the left-hand side of the statement below the count up to placeholder. The mouse pointer will appear as a link select hand.
  4. Drag the statement and drop it in the drop statement here area located within the count up to block, as shown in the following screenshot:

    (Move the mouse over the image to enlarge it.)

  5. Place the mouse pointer over the light shade at the left-hand side of the statement below the count up to placeholder. Drag this statement and drop it after the last statement within the count up to block.
  6. Repeat the aforementioned step for the next four statements. The count up to 5 statement will group the following block of code:

    this.bee.moveTo(this.purpleFlower.getPart(IStemMiddle_IStemTop
    _IHPistil_IHPetal01), duration: 1.0, style: BEGIN_AND_
    END_ABRUPTLY)
    this.bee.delay(2.0)
    this.bee.moveAwayFrom(0.25, this.purpleFlower.getPart(IStemMiddle_
    IStemTop_IHPistil_IHPetal01), duration: 1.0, style: BEGIN_
    ABRUPTLY_AND_END_GENTLY)
    this.bee.turnToFace(this.purpleFlower2.getPart(IStemMiddle_
    IStemTop_IHPistil_IHPetal05), duration: 1.0, style: BEGIN_
    ABRUPTLY_AND_END_GENTLY)
    this.bee.moveTo(this.purpleFlower2.getPart(IStemMiddle_IStemTop_
    IHPistil_IHPetal05), duration: 1.0, style: BEGIN_AND_END_GENTLY)
    this.bee.delay(2.0)

How it works...

We added a count up to 5 statement that allowed us to create a block of code that will be repeated 5 times. The count up to statement creates a loop that repeats the execution of a group of statements with a synchronous execution the number of times specified by its required integer parameter.

The statements added within the count up to block run one after the other. When the execution flow reaches the last statement within the count up to block, Alice checks the number of times that the block was executed. If the number of times hasn't reached the integer number specified as a parameter, the loop will start with the first statement in the block again. Once the number of times is reached, Alice goes on with the execution of the next statement after the count up to block.

We dragged and dropped many statements from the main do in order block to the count up to 5 block. The run procedure will perform the following visible actions in the specified order:

  1. Point the bee at purpleFlower.
  2. Set up a counter to 0.
  3. Increase the counter.
  4. Begin and abruptly end a movement for the bee from its position to the petal named IStemMiddle_IStemTop_IHPistil_IHPetal01 of purpleFlower. The total duration for the animation must be 1 second.
  5. Make the bee stay in its position for 2 seconds.
  6. Move the bee away 0.25 units from the position of the petal named IStemMiddle_IStemTop_IHPistil_IHPetal01 of purpleFlower. Begin the movement abruptly but end it gently. The total duration for the animation must be 1 second.
  7. Turn the bee to the face of the petal named IStemMiddle_IStemTop_IHPistil_IHPetal05 of purpleFlower2. Begin the movement abruptly but end it gently. The total duration for the animation must be 1 second.
  8. Begin and end abruptly a movement for the bee from its position to the petal named IStemMiddle_IStemTop_IHPistil_IHPetal05 of purpleFlower2. The total duration for the animation must be 1 second.
  9. Make the bee stay in its position for 2 seconds.
  10. If the counter hasn't reached 5, go back to step number 3. If the counter reached 5, go on with step number 11.
  11. Move the bee forward 10 units. Begin the movement abruptly but end it gently. The total duration for the animation must be 10 seconds. The bee will disappear from the scene.

The following image shows six screenshots of the rendered frames:

(Move the mouse over the image to enlarge it.)

Once the bee reaches the first flower, it will move to and from the two flowers five times. Finally, the bee will leave the flowers.

Running statements for different actors

In this recipe, we will run many statements to create an animation that calls procedures for many actors of the same class. We will coordinate the actions of many flowers available in our scene.

Getting ready

We have to be working on a project with at least two actors of the same class. Therefore, we will use an existing project that has two instances of MyPurpleFlower.

  1. Open the project saved in the Performing many statements in order recipe.
  2. Click on Edit Code, at the lower right corner of the big scene preview. Alice will show a smaller preview of the scene and will display the Code Editor on a panel located at the right-hand side of the main window.
  3. Click on the class: MyScene drop-down list and the list of classes that are part of the scene will appear.
  4. Select MyScene | Edit run.
  5. Delete all the existing statements. Right-click on each statement and select Delete Statement in the context menu that appears.

How to do it...

Follow these steps to call procedures for many instances of the same class:

  1. Drag the each in _ together statement, located at the top, and drop it in the drop statement here area located below the do in order label, inside the run tab.
  2. Click on Other Array and the Enter custom array dialog box will appear.
  3. Click on the type drop-down list and a drop-down menu will appear. Select MyTypes | MyPurpleFlower, as shown in the following screenshot:

    Alice 3: Controlling the Behavior of Animations

  4. Click on the Add... drop-down menu button that appears at the right-hand side of value. Click on this.purpleFlower in the menu that appears.
  5. Click on the Add... drop-down menu button that appears at the right-hand side of value and below [0] this.purpleFlower. Click on this.purpleFlower2 in the menu that appears. The dialog box will display this.purpleFlower and this.purpleFlower2 as the two values, as shown in the following screenshot:

    Alice 3: Controlling the Behavior of Animations

  6. Click on OK. The each in _ together statement will display a drop statement here placeholder, as shown in the following screenshot:

    Alice 3: Controlling the Behavior of Animations

  7. Select variable: m in the instance drop-down list located at the left-hand side of the main window, below the small scene preview. Make sure that part: none is selected in the drop-down list located at the right-hand side of the chosen instance.
  8. Activate the Procedures tab. Alice will display the procedures for the local m variable that represents each MyPurpleFlower instance available in the scene.
  9. Drag the roll procedure and drop it in the drop statement here area located within the each in _ together block. The roll assignment statement contains the m and roll labels followed by the direction and amount parameters and their question marks ???. A list with all the predefined direction values to pass to the first parameter will appear.
  10. Click on LEFT and then on 0.125 in the cascade menu that appears.
  11. Click on the more... drop-down menu button that appears at the right-hand side of the recently dropped statement. Click on duration, select Other Real Number... and the Enter custom real number dialog box will appear. Enter 5 and click on OK.
  12. Click on the new more... drop-down menu that appears. Click on asSeenBy and then on m.
  13. Click on the new more... drop-down menu that appears. Click on style and then on BEGIN_AND_END_ABRUPTLY. The following code will be displayed:

    m.roll LEFT, 0.125, duration: 5.0, asSeenBy: m, style: BEGIN_AND_
    END_ABRUPTLY

  14. Drag the roll procedure and drop it below the previously dropped procedure call, within the each in _ together block. A list with all the predefined direction values to pass to the first parameter will appear.
  15. Click on RIGHT and then on 0.125 in the cascade menu that appears.
  16. Click on the more... drop-down menu button that appears at the right-hand side of the recently dropped statement. Click on duration, select Other Real Number... and the Enter custom real number dialog box will appear. Enter 5 and click on OK.
  17. Click on the new more... drop-down menu that appears. Click on asSeenBy and then on m.
  18. Click on the new more... drop-down menu that appears. Click on style and then on BEGIN_ABRUPTLY_AND_END_GENTLY. The following code will be displayed and it will appear as the second statement for the each in _ together block, as shown in the following screenshot:

    m.roll RIGHT, 0.125, duration: 5.0, asSeenBy: m, style: BEGIN_
    ABRUPTLY_AND_END_GENTLY

    (Move the mouse over the image to enlarge it.)

  19. Select File | Save as... from Alice's main menu and give a new name to the project. Then you can make changes to the project according to your needs.

How it works...

We added an each in _ together statement that allowed us to create a block of code that executes statements for each instance of the specified class. There are two instances of the MyPurpleFlower class, purpleFlower, and purpleFlower2. We selected MyPurpleFlower as the type for a new array when we added the each in _ together statement. The following code defines the each in _ together block:

each (MyPurpleFlower m in new MyPurpleFlower[]{ this.purpleFlower,
this.purpleFlower2 }) together

We added two calls to the roll procedure for the m instance within the each in _ together block. The first roll procedure rolls the two MyPurpleFlower instances to the left. Because we called m.roll within the block, Alice calls the roll method for each of the MyPurpleFlower instances specified after the in keyword. Alice calls the roll method for each of these instances at the same time, together. Therefore, the wind rolls the two flowers to the right at the same time.

Once the first call to the roll procedure finishes for both flowers, the second call to this procedure also runs for the instances specified after the in keyword at the same time, together. However, this time, the two flowers roll to the left, back to their original angle.

The two calls to the roll procedure run with a synchronous execution. However, they run together for the instances specified in the array defined after the in keyword. Thus, you will see the two flowers rolling at the same time. The following screenshot shows three screenshots of the rendered frames:

(Move the mouse over the image to enlarge it.)

Running statements for many actors

In this task, we will learn to call procedures for different actors. We will see the results of animating two actors of different classes. Eight statements will call procedures for the bee instance and three statements will call procedures for the redbird instance.

Getting ready

We have to be working on a project with at least two actors of different classes. Therefore, we will use an existing project and we will add an actor to the scene.

  1. Open the project saved in the Performing many statements in order recipe.
  2. Click on Edit Scene, at the lower-right corner of the scene preview. Alice will show a bigger preview of the scene and will display the Model Gallery at the bottom.
  3. Add an instance of the Redbird class to the scene, and enter redbird for the name of this new instance. First, Alice will create the MyRedbird class to extend Redbird. Then, Alice will create an instance of MyRedbird named redbird. Follow the steps explained in the Creating a new instance from a class in a gallery recipe, in the article, Alice 3: Making Simple Animations with Actors.
  4. Place the red bird as shown in the following screenshot:

    Alice 3: Controlling the Behavior of Animations

How to do it...

Follow these steps to call procedures for the bee instance and the redbird instance:

  1. Click on Edit Code, at the lower-right corner of the big scene preview. Alice will show a smaller preview of the scene and will display the Code Editor on a panel located at the right-hand side of the main window.
  2. Click on the class: MyScene drop-down list and the list of classes that are part of the scene will appear.
  3. Select MyScene | Edit run. The eight existing statements that control the movement of the bee will appear.
  4. Select redbird in the instance drop-down list located at the left-hand side of the main window, below the small scene preview. Make sure that part: none is selected in the drop-down list located at the right-hand side of the chosen instance.
  5. Activate the Procedures tab. Alice will display the procedures for the previously selected actor.
  6. Drag the moveTo procedure and drop it below the second statement. Alice will display a green line indicating the position in which this new statement will be inserted. A list with all the possible instances to pass to the first parameter will appear. Select this.purpleFlower2 getPart ??? and then IStemMiddle_IStemTop_IHPistil_IHPetal03.
  7. Click on the more... drop-down menu button that appears at the right-hand side of the recently dropped statement. Click on duration and then on 10.0 in the cascade menu that appears.
  8. Click on the new more... drop-down menu that appears. Click on style and then on BEGIN_AND_END_GENTLY. The following code will be displayed as the new third statement:

    this.redbird.moveTo(this.purpleFlower2.getPart(IStemMiddle_
    IStemTop_IHPistil_IHPetal03), duration: 10.0, style: BEGIN_AND_
    END_GENTLY)

  9. Drag the delay procedure and drop it below the fourth statement, this.bee.delay(2.0). A list with all the predefined direction values to pass to the first parameter will appear. Select 1.0 and the following code will be displayed as the new fifth statement:

    this.redbird.delay(2.0)

  10. Drag the move procedure and drop it below the last statement. Select FORWARD, click on Other Real Number... and the Enter custom real number dialog box will appear. Enter 5 and click on OK.
  11. Click on the more... drop-down menu button, on duration, select Other Real Number... and the Enter custom real number dialog box will appear. Enter 6 and click on OK.
  12. Click on the additional more... drop-down menu that appears, on asSeenBy and then on this.redbird. Click on the new more... drop-down menu that appears, on style and then on BEGIN_GENTLY_AND_END_ABRUPTLY. The following code will be displayed as the last statement. The three procedure calls for the redbird instance mixed with calls for the bee instance:

    this.redbird.move(FORWARD, duration: 6.0, asSeenBy: this.redbird,
    style: BEGIN_GENTLY_AND_END_ABRUPTLY)

    (Move the mouse over the image to enlarge it.)

  13. Select File | Save as... from Alice's main menu and give a new name to the project. Then you can make changes to the project according to your needs.

How it works...

The run procedure defined in the MyScene class executes 11 statements in order with a synchronous execution. Eight statements call procedures for the bee instance and three statements call procedures for the redbird instance. The three statements added to the run procedure that call procedures for the redbird instance will perform the following visible actions in the specified order:

  1. Begin and abruptly end a movement for the redbird from its position to the petal named IStemMiddle_IStemTop_IHPistil_IHPetal03 of purpleFlower2. The total duration for the animation must be 10 seconds.
  2. Make the redbird stay in its position for 1 second.
  3. Move the bee forward 5 units. Begin the movement gently but end it abruptly. The total duration for the animation must be 6 seconds. The redbird will disappear from the scene.

However, the scene will show only one actor performing actions at a time. For example, while the redbird flies towards one of the petals of purpleFlower2, the bee won't move. This happens because each statement is executed in order with a synchronous execution and there aren't two statements running at the same time, no matter the actor that calls the procedure. The following screenshot shows three screenshots of the rendered frames:

(Move the mouse over the image to enlarge it.)

Alice 3 Cookbook 79 recipes to harness the power of Alice 3 for teaching students to build attractive and interactive 3D scenes and videos
Published: April 2011
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:
        Read more about this book      

(For more resources related to this subject, see here.)

Running many statements at the same time

In this recipe, we will learn to organize many calls to procedures for different actors to run them at the same time. We will see the results of animating two actors concurrently. We will make the redbird fly at the same time that the bee flies.

Getting ready

We have to be working on a project with at least two actors of different classes that must fly. Therefore, we will use an existing project with a redbird and a bee.

  1. Open the project saved in the Running statements for many actors recipe.
  2. Click on Edit Code, at the lower-right corner of the big scene preview. Alice will show a smaller preview of the scene and will display the Code Editor on a panel located at the right-hand side of the main window.
  3. Click on the class: MyScene drop-down list and the list of classes that are part of the scene will appear.
  4. Select MyScene | Edit run. The 11 existing statements that control the movement of the bee and the red bird will appear.

How to do it...

Follow these steps to animate two actors concurrently:

  1. Open the project saved in the Running statements for many actors recipe.
  2. Drag the do together statement, located at the top, and drop it above the first statement. Alice will display a green line indicating the position in which this new statement will be inserted. The do together statement will display a drop statement here placeholder.
  3. Drag the do in order statement, located at the top, and drop it in the drop statement here area located within the do together block. The do in order statement will display a drop statement here placeholder.
  4. Drag another do in order statement, located at the top, and drop it below the previously dropped statement. Make sure that this do in order statement becomes part of the statements for the do together block. This do in order statement will also display a drop statement here placeholder, as shown in the following screenshot:

    Alice 3: Controlling the Behavior of Animations

  5. Place the mouse pointer over the light shade at the left-hand side of the statement below the do together placeholder. The mouse pointer will appear as a link select hand.
  6. Drag the statement and drop it in the drop statement here area located within the first do in order block.
  7. Place the mouse pointer over the light shade at the left-hand side of the statement below the do together placeholder. Drag this statement and drop it after the last statement within the first do in order block.
  8. Repeat the aforementioned step for the remaining six statements that call procedures for bee. The first do in order statement will group the eight statements that call procedures for bee.
  9. Place the mouse pointer over the light shade at the left-hand side of the statement below the do together placeholder that calls a procedure for redbird. The mouse pointer will appear as a link select hand.
  10. Drag the statement and drop it in the drop statement here area located within the second do in order block.
  11. Place the mouse pointer over the light shade at the left-hand side of the statement below the do together placeholder that calls a procedure for redbird. Drag this statement and drop it after the last statement within the second do in order block.
  12. Repeat the aforementioned step for the remaining statement that calls a procedure for redbird. The second do in order statement will group the three statements that call procedures for redbird, as shown in the following screenshot:

    (Move the mouse over the image to enlarge it.)

How it works...

We added a do together statement that allowed us to create a block of code that will run all the statements we add to the block at the same time. This means that all the statements added to this block will start their execution together.

We added two do in order statements within the do together block. Each of this do in order blocks will start their execution at the same time. We wanted to run an animation for the bee and to control the redbird at the same time. Thus, the first do in order block has the eight statements that control the bee and the second do in order block includes the three statements that control the redbird.

Alice allows us to group many statements in blocks to organize how the code has to be executed. In this case, we have three levels:

  1. The first do in order block, defined by default for the run procedure
  2. The do together block
  3. Each do in order block within the do together block

The scene will show two actors performing actions at the same time, in parallel. For example, while the redbird flies towards one of the petals of purpleFlower2, the bee checks the pollen available in the two flowers. This happens because each group of statements is executed in order in two independent blocks that run together. The following image shows three screenshots of the rendered frames:

(Move the mouse over the image to enlarge it.)

There's more...

When you work with the Alice code editor, you can convert or dissolve groups of statements included in a block. If you dissolve a block, the statements included in the block become part of the higher level block.

To dissolve a block, right-click on its label and select the Disolve option, followed by the block's description. For example, a do in order block shows a Disolve DoInOrder option, as shown in the following screenshot:

Alice 3: Controlling the Behavior of Animations

Repeating statements at the same time

In this task, we will repeat a group of statements for many actors of the same class. We will combine blocks that organize the code to animate two actors concurrently and to repeat this animation many times.

Getting ready

We have to be working on a project with at least two actors of the same class that must run animations concurrently. Therefore, we will use an existing project with two flowers.

  1. Open the project saved in the Running statements for different actors recipe.
  2. Click on Edit Code, at the lower-right corner of the big scene preview. Alice will show a smaller preview of the scene and will display the Code Editor on a panel located at the right-hand side of the main window.
  3. Click on the class: MyScene drop-down list and the list of classes that are part of the scene will appear.
  4. Select MyScene | Edit run. The each in _ together block with the two statements will appear.

How to do it...

Follow these steps to repeat a group of statements for many actors of the same class:

  1. Drag the count statement, located at the top, and drop it on top of the first statement within the each in _ together block. Alice will display a green line indicating the position in which this new statement will be inserted.
  2. Select Other Integer... and the Enter custom integer dialog box will appear. Enter 5 and click on OK. The count up to statement will display a drop statement here placeholder.
  3. Place the mouse pointer over the light shade at the left-hand side of the statement below the count up to block. The mouse pointer will appear as a link select hand.
  4. Drag the statement and drop it in the drop statement here area located within the count up to block, as shown in the following screenshot:

    Alice 3: Controlling the Behavior of Animations

  5. Place the mouse pointer over the light shade at the left-hand side of the statement below the count up to placeholder. Drag this statement and drop it after the last statement within the count up to block. The count up to 5 statement will group the two calls to the roll procedure for the m variable, as shown in the following screenshot:

    (Move the mouse over the image to enlarge it.)

How it works...

We added a count up to 5 statement that allowed us to create a block of code that executes two statements five times. The following two lines of code will run five times and they are also within the each in _ together block:

m.roll (LEFT, 0.125, duration: 5.0, asSeenBy: m, style: BEGIN_AND_
END_ABRUPTLY)
m.roll (RIGHT, 0.125, duration: 5.0, asSeenBy: m, style: BEGIN_
ABRUPTLY_AND_END_GENTLY)

The statements added within the count up to block run one after the other. However, each statement performs actions to many instances. In this case, each statement works with the two flowers at the same time. When the execution flow reaches the last statement within the count up to block, Alice checks the number of times that the block was executed. If the number of times hasn't reached the integer number specified as a parameter, the loop will start with the first statement in the block again. Once the number of times is reached, Alice goes on with the execution of the next statement after the count up to block.

We dragged and dropped two statements from the each in _ together block to the count up to 5 block. The combination of the two blocks allows us to repeat statements for many actors at the same time. You will see the two flowers rolling at the same time five times. The following screenshot shows six screenshots of the rendered frames:

(Move the mouse over the image to enlarge it.)

Summary

This article provided many tasks that will allowed us to start controlling the behavior of animations with many actors performing different actions. We executed many actions with a specific order. We used counters to run one or more statements many times. We executed actions for many actors of the same class. We ran code for different actors at the same time to render complex animations.


Further resources related to this subject:


About the Author :


Vanesa S. Olsen

Vanesa Olsen is a Speech Therapist. She has been working for more than five years in therapeutic centers, schools, and hospitals. She has been applying modern technologies in language disorders and learning disability treatments, and in helping schools to keep the students in the least restrictive environment. Specifically, she has been working with Moodle as an e-Learning platform, Alice and other tools, combined with the usage of Web 2.0 and general purpose modern hardware as gamepads, pen sketches, touch screens, netbooks and joysticks. She enjoys helping children and teenagers to improve their skills.

She has written another book for Packt Publishing, Moodle 1.9 for Teaching Special Education Children (5-10): Beginner's Guide.

She lives with her husband, Gaston, and her little son, Kevin. When she is not working she devotes her spare time to her family and hobbies. She enjoys modeling in cold porcelain, swimming, and researching about new technologies and techniques to apply in her treatments.
You can contact her at vanesaolsen@gmail.com and at olsenvanesa@live.com

Books From Packt


Scratch 1.4: Beginner’s Guide
Scratch 1.4: Beginner’s Guide

Away3D 3.6 Essentials
Away3D 3.6 Essentials

OpenSceneGraph 3.0: Beginner's Guide
OpenSceneGraph 3.0: Beginner's Guide

Panda3D 1.7 Game Developer's Cookbook
Panda3D 1.7 Game Developer's Cookbook

ZBrush 4 Sculpting for Games: Beginner's Guide
ZBrush 4 Sculpting for Games: Beginner's Guide

Blender 2.5 Materials and Textures Cookbook
Blender 2.5 Materials and Textures Cookbook

SketchUp 7.1 for Architectural Visualization: Beginner's Guide
SketchUp 7.1 for Architectural Visualization: Beginner's Guide

OGRE 3D 1.7 Beginner's Guide
OGRE 3D 1.7 Beginner's Guide


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
U
7
1
U
B
y
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software