Sei sulla pagina 1di 54

Using Procedures

Copyright 2012, Oracle. All rights reserved.

Using Procedures

What Will I Learn?


Objectives Toggle and describe the visual difference between the scene editor and code editor Correlate storyboard statements with program execution tasks Identify the correct procedure to move an object Use procedures to move objects Add Java programming procedures to the code editor Demonstrate how procedure values can be altered Add a control statement to the code editor Use random numbers to randomize motion

Copyright 2012, Oracle. All rights reserved.

Using Procedures

Why Learn It?


Purpose In movies, actors perform the actions described in a script, under the guidance of a director. Like a director, you will program objects to move and act, and make frequent changes as you go. You will need to know how to switch back and forth between the editors used to set up your scene and program your animation. You will also need to understand how to choose the correct programming instructions to make objects move as you intended.

Copyright 2012, Oracle. All rights reserved.

Using Procedures

Prepare to Program
To prepare to program, use a top-down development approach: 1. Define the high-level scenario for the animation. 2. Document the actions that take place in the scenario, step-by-step, in a textual storyboard. Doing this helps you gain a thorough understanding of all of the actions that need to occur in the animation. 3. Create a table where you match the storyboard steps to the exact instructions that you need to program in Alice 3. 4. Review the table at several points during the animation's development to ensure you are creating it correctly and on-track to completion. Revise the table as necessary throughout the animation's development.

Copyright 2012, Oracle. All rights reserved.

Using Procedures

Prepare to Program (cont.)


Review this example textual storyboard:
Do the following steps in order Chicken walks by Cat turns to look at Chicken Cat says, Dinner time! Chicken says, Oh no! Chicken turns to right Do the following steps together Chicken walks away quickly Cat walks away quickly

Copyright 2012, Oracle. All rights reserved.

Using Procedures

Prepare to Program (cont.)


Create a table to match the storyboard actions to the programming instructions needed for the animation.
Storyboard Action Program Instructions Order of Instructions Do in order Chicken walks by Cat looks at chicken Cat says, Dinner time! Chicken says, Oh no! Chicken and cat turn right Cat chases Chicken Chicken moves forward 2 meters Cat turns to face chicken Cat says Dinner Time! Chicken says Oh no! Chicken turns right .5 meters Cat turns right .125 meters Chicken move forward 2 meters Cat move forward 2 meters Do together

Copyright 2012, Oracle. All rights reserved.

Using Procedures

Prepare to Program (cont.)


Note that the object being programmed changes. Ensure that the correct object is selected when choosing a programming instruction.
Storyboard Action Program Instructions Order of Instructions Do in order Chicken walks by Cat looks at chicken Cat says, Dinner time! Chicken says, Oh no! Chicken and cat turn right Cat chases Chicken Chicken moves forward 2 meters Cat turns to face chicken Cat says Dinner Time! Chicken says Oh no! Chicken turns right .5 meters Cat turns right .125 meters Chicken move forward 2 meters Cat move forward 2 meters Do together

Copyright 2012, Oracle. All rights reserved.

Using Procedures

Prepare to Program (cont.)


Consider grouping the programming instructions together logically and inserting comments into your program to help organize your programing tasks.
//chicken and cat movement
Chicken walks by Cat looks at chicken Chicken moves forward 2 meters Cat turns to face chicken

//chicken and cat speaking


Cat says, Dinner time! Chicken says, Oh no! Cat says Dinner Time! Chicken says Oh no!

//chicken and cat movement away


Chicken and cat turn right Chicken turns right .5 meters Cat turns right .125 meters Chicken move forward 2 meters Cat move forward 2 meters
8

Cat chases Chicken

Copyright 2012, Oracle. All rights reserved.

Using Procedures

Prepare to Program (cont.)


Once the objects are added to the scene in the scene editor, you can use one-shot procedures to precisely position them for the initial scene, or starting point, of your animation.
When positioning objects in a scene, a one-shot procedure is a piece of program code that is executed once to define a single movement for an object in the scene editor. Alice 3 has a set of these procedures available for each class.

Copyright 2012, Oracle. All rights reserved.

Using Procedures

Prepare to Program (cont.)


To call a one-shot procedure for an object, right-click on the object, select the procedure from the drop-down list, and specify the procedure arguments. The object will immediately move based on what you specified.

Copyright 2012, Oracle. All rights reserved.

10

Using Procedures

Writing the Animation Program


Once you have positioned the objects and are ready to write the animation program, from Scene View, click Edit Code to display the code editor.

Copyright 2012, Oracle. All rights reserved.

11

Using Procedures

Writing the Animation Program (cont.)


A scene can have multiple actors. Always make sure you are creating a programming instruction for the correct actor. Just below the scene view window in the code editor, there is an instance pull down menu. Use the instance pull down menu to select the actor, or object, for which you want to specify a programming instruction.

Copyright 2012, Oracle. All rights reserved.

12

Using Procedures

Writing the Animation Program (cont.)


For example, if you have a pajama fish and a clown fish in your scene, and you want the pajama fish to move forward choose the pajama fish in the instance menu before creating the programming step to move forward.

Copyright 2012, Oracle. All rights reserved.

13

Using Procedures

Writing the Animation Program (cont.)


To create a programming instruction for a specific object, click and drag the programming instruction into the myFirstMethod area of the code editor.

Copyright 2012, Oracle. All rights reserved.

14

Using Procedures

Writing the Animation Program (cont.)


Below the instance pull down menu is the Methods Panel containing three tabs that organize the Procedures, Functions, and Properties for the object selected in the instance pull down menu: Procedures tab: initially displays all of the pre-defined procedures for an object. Functions tab: displays all of the pre-defined functions for an Switch between the object. tabs in the Methods Properties tab: displays all of Panel to select a the properties of an object. procedure, function, or object property.
Copyright 2012, Oracle. All rights reserved. 15

Using Procedures

Programming Statements
The Procedures tab: Displays a pre-defined set for each class Displays procedures declared for the class Procedure examples: Move Turn Roll
A procedure is a piece of program code that defines how the object should execute a task. Alice 3 has a set of procedures for each class, however, users can create (declare) new procedures.

Copyright 2012, Oracle. All rights reserved.

16

Using Procedures

Programming Arguments
There are two components that follow the name of the object in a programming statement: Procedure name Procedure arguments Programming arguments initially display with ??? to indicate the location at which the argument will be placed.
Procedure Arguments

Copyright 2012, Oracle. All rights reserved.

17

Using Procedures

Programming Arguments (cont.)


Arguments are selected after the procedure is dragged into the code editor using cascading menus. Argument types can include: Direction Amount Duration Target Text
An argument describes how to perform the procedure.

Copyright 2012, Oracle. All rights reserved.

18

Using Procedures

Programing Arguments (cont.)


Use the menus to select the appropriate argument for a procedure. You may need to simply select a placeholder argument that can be changed later.

Copyright 2012, Oracle. All rights reserved.

19

Using Procedures

Programming Arguments (cont.)


To change an argument use the menu accessed by clicking the down pointing arrow to the right of the argument value. Select the applicable option from the argument list.

Copyright 2012, Oracle. All rights reserved.

20

Using Procedures

Executing A Program
Click the Run button to execute the programming instruction.

Copyright 2012, Oracle. All rights reserved.

21

Using Procedures

Object Direction
An object can move in six directions: Up Down Forward Backward Right Left

Object direction is egocentric. For example, if an object is facing you, and you specify an instruction for the object to turn left, the object will turn to its left, not to your left.

Copyright 2012, Oracle. All rights reserved.

22

Using Procedures

Move Procedure
The move procedure moves the object in all six directions. You select the direction and distance to move.

Copyright 2012, Oracle. All rights reserved.

23

Using Procedures

Move Toward Procedure


The move toward procedure moves the object toward another object. You select the object to move, the object to move toward and the distance the object will move.

Copyright 2012, Oracle. All rights reserved.

24

Using Procedures

Move Away From Procedure


The move away from procedure moves the object away from another object. You select the object to move away from, and the distance to move.

Copyright 2012, Oracle. All rights reserved.

25

Using Procedures

Move To Procedure
The move to procedure will move an object the distance to the center of the target object. You select the object to move to, and the program calculates the distance for you.

You may want to use additional procedures to adjust the position of the moving object so it does not stay in the center of the other object.

Copyright 2012, Oracle. All rights reserved.

26

Using Procedures

Move and Orient To Procedure


The move and orient to procedure will move an object the distance to the center of the target object. Additionally, it adjusts the moving object's orientation to match the orientation of the target object. You select the object to move to, and the program calculates the distance for you as well as adjusts the object's orientation.

Copyright 2012, Oracle. All rights reserved.

27

Using Procedures

Delay Procedure
The Delay procedure will halt an object for a specific number of seconds. You select the number of seconds to delay until the next procedure is executed.

In this example, the clock delays one second, then the hour hand on the clock rolls to the left 0.25 meters.

Copyright 2012, Oracle. All rights reserved.

28

Using Procedures

Say Procedure
The Say procedure will create a call out bubble with text to make an object appear to talk. You can use the pre-defined text or choose to annotate your own text.

Copyright 2012, Oracle. All rights reserved.

29

Using Procedures

Rotation Procedures
There are two procedures for rotating objects: Turn Roll The turn procedure rotates objects on their center point: Left Right Forward Backward The roll procedure rolls objects on their center point: Left Right

Copyright 2012, Oracle. All rights reserved.

30

Using Procedures

Turn and Roll Procedures Compared


The object below rotates (turns) on its center point, right and left.

Stationary

Right Turn

Left Turn

The object below rotates (rolls) on its center point, right and left.

Stationary

Right Roll

Left Roll
31

Copyright 2012, Oracle. All rights reserved.

Using Procedures

Roll Procedure Example


Roll Right and Left Example:

Some objects have sub-parts that are also movable. In this example, the pocket-watch object has hour and minute hand sub-parts. The sub-parts may be animated to roll on the clock's center point.

Copyright 2012, Oracle. All rights reserved.

32

Using Procedures

Examining Sub-part Rotation


Rotation can be applied to an entire object, or select parts (sub-parts) of the object.

Above, the object's sub-part (its head) displays rings; the rings show the sub-parts' range of motion.

Copyright 2012, Oracle. All rights reserved.

33

Using Procedures

Edit Programming Statements


You use the drag-and-drop method to bring procedures into the code editor. You can reorder your objects using dragand-drop also. There is a dotted area to the left of the object name. Use this dotted area to drag-and-drop without changing an argument's value.

Copyright 2012, Oracle. All rights reserved.

34

Using Procedures

Edit Programming Statements (cont.)


Right click on programming statements to delete them.

Copyright 2012, Oracle. All rights reserved.

35

Using Procedures

Control Statements
Procedures listed in the code editor will execute sequentially unless instructions are given to the program to execute in a different manner. These instructions are referred to as control statements. Control statements define how to execute a sequence of programming statements in a manner other than sequentially. Examples: Do in order: Execute statements in sequential order; referred to as sequential control Do together: Execute statements simultaneously Count: Execute statements a specific number of times While: Execute while a condition is true

Copyright 2012, Oracle. All rights reserved.

36

Using Procedures

Control Statements (cont.)


Control statement tiles are located below the programming statements you have entered into the code editor. You can enter control statements before or after programming statements are entered into the code editor. Example 1: you can enter programming statements into the code editor, run the program, and then determine you want two procedures to execute at the same time. You can then drag a do together control statement into your code editor and move the two procedures into the control statement. Example 2: you can pre-determine that you want two programming statements to execute simultaneously. You can drag a do together control statement into your code editor and then create the two procedures within the control statement.
Copyright 2012, Oracle. All rights reserved. 37

Using Procedures

Step 1: Insert Control Statement


Drag-and-drop the first control statement into the code editor.

Copyright 2012, Oracle. All rights reserved.

38

Using Procedures

Step 2: Select Instance


Select the correct instance object from the instance pull down menu.

Copyright 2012, Oracle. All rights reserved.

39

Using Procedures

Step 3: Enter Procedure


Drag the first procedure into the control statement in the code editor.

Copyright 2012, Oracle. All rights reserved.

40

Using Procedures

Step 4: Select Argument Values


Select the values for the arguments to create the complete programming statement. Test as necessary.

Copyright 2012, Oracle. All rights reserved.

41

Using Procedures

Step 5: Repeat Steps 1-4


Repeat steps 1-4 until all of the programming statements are entered into the control statement.

Copyright 2012, Oracle. All rights reserved.

42

Using Procedures

Step 6: Run the Animation


Test that the animation works as intended at run-time.

Copyright 2012, Oracle. All rights reserved.

43

Using Procedures

Step 7: Edit Programming Statements


It may take several attempts to get the animation to work as desired. Edit programming arguments as necessary. These steps are often referred to as testing and debugging.

Copyright 2012, Oracle. All rights reserved.

44

Using Procedures

Debug the Animation


You may run your animation many times, making adjustments to the arguments after each execution. This refinement is referred to as debugging and testing. Be sure to save often while debugging your program.

Copyright 2012, Oracle. All rights reserved.

45

Using Procedures

Random Numbers
Random numbers are a sequence of numbers generated by the computer with no pattern in their sequence. For example: 15674 -6934022.1133 03 Computers require random number generation for: Security: Randomly generated passwords Simulation: Earth science modeling (e.g., erosion over time)

Copyright 2012, Oracle. All rights reserved.

46

Using Procedures

Random Numbers (cont.)


In the real world, animals do not move in straight geometric lines. They change direction, even if slightly, as they walk, swim and fly. Random numbers may be utilized in the distance argument of a procedure, so that an object moves in a less predictable manner that appears more lifelike. Examples:

The floating motion of a fish

The flying motion of a bird

Copyright 2012, Oracle. All rights reserved.

47

Using Procedures

Random Numbers (cont.)


When you use random numbers you specify a low and high value for the range of numbers from which to pull a randomized number. Examine the example below. An up and down move procedure has been established for the clownFish. You replace the placeholder value with the Random option.

Copyright 2012, Oracle. All rights reserved.

48

Using Procedures

Random Numbers (cont.)


You choose the argument containing the ??? placeholders.
NextRandomRealNumberInRange

Then you specify the values desired from the list of available values or by specifying a custom decimal number for each range value.

Custom Decimal Number

Copyright 2012, Oracle. All rights reserved.

49

Using Procedures

Random Numbers (cont.)


Be sure to specify both the minimum and maximum range values.

Copyright 2012, Oracle. All rights reserved.

50

Using Procedures

Random Numbers (cont.)


When the animation is run, the fish will move a random amount up and down.

Copyright 2012, Oracle. All rights reserved.

51

Using Procedures

Terminology
Key terms used in this lesson included: Argument Control statement Instructions Orientation One shot procedure Procedure Program Random numbers

Copyright 2012, Oracle. All rights reserved.

52

Using Procedures

Summary
In this lesson, you learned how to: Toggle and describe the visual difference between the scene editor and code editor Correlate storyboard statements with program execution tasks Identify the correct procedure to move an object Use procedures to move objects Add Java programming procedures to the code editor Demonstrate how procedure values can be altered Add a control statement to the code editor Use random numbers to randomize motion

Copyright 2012, Oracle. All rights reserved.

53

Using Procedures

Practice
The exercises for this lesson cover the following topics: Working with the code editor tools Creating a program with procedures Using random numbers to randomize motion Presenting your work

Copyright 2012, Oracle. All rights reserved.

54

Potrebbero piacerti anche