Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Chapter 4 Sections 1 and 2 World-level methods involve two or more objects break a large problem into smaller, logical units follow a design.

Similar presentations

Presentation on theme: "Review of Chapter 4 Sections 1 and 2 World-level methods involve two or more objects break a large problem into smaller, logical units follow a design."— Presentation transcript:

1 Review of Chapter 4 Sections 1 and 2 World-level methods involve two or more objects break a large problem into smaller, logical units follow a design process called stepwise refinement make the program more organized, easier to edit and debug, and allow the re-using of code (by calling the method more than once)

2 Parameters Allow methods to be “generic” i.e. reusable with different values Types are Number Boolean Object Other (such as sound or color)

3 Parameters The value to be used by the method is sent via an argument in the method call The “calling” method sends the specific value The method executes its code using that value

4 Class-level Methods and Inheritance Chapter 4, Section 3

5 Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person walking A wheel rolling We can write our own methods to define an action for a specific class of objects -- a class-level method.

6 An example (building technique) How can we create a skate method for ice skater objects? We need to: (1)tell Alice to associate the new method (the one we are about to write) with an ice skater, and (2)write a new method to animate the ice skater skating.

7 Demo: The solution First, to associate the animation with the ice skater select the iceSkater tile in the Object Tree select the methods tab in the details panel click on the create new method button

8 Storyboard for skate The slide actions each require several motion instructions, so we will break down these two actions into smaller steps Skate: Do together move skater forward 2 meters Do in order slide on left leg slide on right leg

9 Stepwise Refinement Refinement of slideLeft Do in order Lift right leg and turn upper body forward Lower right leg and return body upright Skate: Do together 1) move forward 2 meters 2) Do in order slideLeft slideRight Refinement of slideRight Do in order Lift left leg and turn upper body forward Lower left leg and return body upright

10 Demo Ch04Lec3Skater Concepts illustrated in this example world A method defined for a specific type of object defines an action for that object. A method can call other methods. In this example, the skate method calls slideRight and slideLeft.

11 Reuse Writing methods to make an ice skater perform a skating motion is an intricate task. We would like to have the iceSkater skate in other worlds without having to write the methods again. The idea of being able to use previously written program code in another program is known as reuse.

12 A new class 1) Rename iceSkater as cleverSkater. 2) Save out as a new class. Alice saves the new class as CleverSkater.a2c

13 Inheritance The CleverSkater class inherits all the properties and methods from the original IceSkater class, and also has the newly defined methods (skate, slideLeft, slideRight) In other programming languages, the concept of creating a new class based on a previously defined class is called inheritance.

14 Importing CleverSkater An instance of the CleverSkater class can be added to a new world – use File|Import.

15 Guidelines To avoid potential misuse of class-level methods, follow these guidelines: Avoid references to other objects Avoid calls to world-level methods Play a sound only if the sound has been imported and saved out as part of the new class If these guidelines are not followed and an instance of the new class is added to another world, Alice will open an Error dialog box to tell you something is wrong.

16 Bad Example What if there is no penguin in the new world where a cleverSkater object is imported?

17 Problem Suppose you really want to write a class-level method where another object is involved? For example, a method to make the skater skate around another object-- in this scene, the penguin.

18 Parameter A solution is to write a class-level method with an object parameter that allows you to pass in the specific object. cleverSkater.skateAround Parameter: whichObject Do in order Do together cleverSkater turn to face whichObject cleverSkater lift right leg cleverSkater move to whichObject cleverSkater turn around whichObject

19 Translation to Code Most of the skateAround storyboard design is straightforward and easy to code. One step, however, requires some thought: cleverSkater move to whichObject -- what distance should the cleverSkater move?

20 Calling a built-in function The instruction to move the skater to whichObject (penguin, in this example) would look like this: Unfortunately, the skater will collide with the penguin because the distance between two objects is measured center-to-center.

21 Expression To avoid a collision, use a math operator to create an expression that adjusts the distance. Math operators in Alice: addition + subtraction  multiplication * division / Example:

22 Demo Ch04Lec3SkateAround Concepts illustrated: A parameter acts as a placeholder for the object that will be passed in A call to the distance to function returns a number value A math expression can be created as part of an instruction

23 Tips & Techniques 4 Visible and Invisible Objects

24 Opacity Opacity is a measure of how "see- through" an image or an object is. The less opaque an object is, the more see- through it is. Opacity of 100%, cannot see through the object Opacity of 0%, object is transparent (like clear glass) In Alice, an object with an opacity of 0% is invisible.

25 Changing the Opacity A change in opacity can be used to simulate real world conditions. Example A fish swimming away from the camera should fade away because water blurs our vision of distant objects.

26 Demo Ch04Lec4LilfishOpacity Concepts illustrated in this example As opacity is gradually decreased, the fish becomes less visible. At an opacity of 0%, the object is still in the world (can still see it listed in the object tree) but is effectively invisible.

27 isShowing The isShowing property has a Boolean value, either true or false. Setting an object's isShowing property to false makes the object invisible in the world. Once again, the object is not removed from the world, it is simply not visible in the world.

28 Demo Ch04Lec4ChickenIsShowing Concepts illustrated in this example A billboard can be used to display a title screen for the animation. A 2-second wait gives the user time to read the billboard. Changing the isShowing property to false makes the title screen disappear.

29 Two properties isShowing and opacity are two different properties. isShowing works like an on/off switch opacity works like a dimmer switch Although each can be used to make an object invisible, changing one does not automatically change the value of the other!

30 Practical Uses of Invisible Objects An invisible object is sometimes useful as a stationary marker that creates a target for a move to instruction an external reference point for object rotational motion

31 Demos Ch04Lec4InvisibleTarget Invisible circle makes it possible to move the object to a landing target. Ch04Lec4InvisibleReferencePoint External reference point acts as a pivot for the rotational movement of each object.

32 Assignment Chapter 4, Section 3 Lab For next time: Read Chapter 5, Section 1 Create storyboard for Project 2Project 2

Download ppt "Review of Chapter 4 Sections 1 and 2 World-level methods involve two or more objects break a large problem into smaller, logical units follow a design."

Similar presentations

Ads by Google