Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class-level Methods and Inheritance Part 2

Similar presentations


Presentation on theme: "Class-level Methods and Inheritance Part 2"— Presentation transcript:

1 Class-level Methods and Inheritance Part 2
Alice

2 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 that something is wrong.

3 Bad Example 1

4 Bad Example 2

5 Problem What if you are convinced that 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.

6 Solution 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

7 Translating design into code
Most of the skateAround storyboard design is straightforward and easy to code. The last two steps, however, require some thought: cleverSkater move to whichObject -- what distance should the cleverSkater move? cleverSkater turn around whichObject -- how do we tell cleverSkater to turn (in a circle) around another object?

8 Built-in Questions The built-in question, distance to can be used to determine the distance the skater must move.

9 Calling the question 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.

10 Expressions 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:

11 asSeenBy To have the skater skate around another object, pass whichObject as an argument to the asSeenBy parameter in a turn instruction. For more details on asSeenBy, review Tips & Techniques 2.

12 Demo Demonstrate skateAround

13 Testing Each time you create a new class, test it:
Add an instance of the new class to a new world. Write a short testing program to test each new method. Testing increases your confidence in the ability to reuse your code in other worlds.

14 Lab Lab 4-3 Chapter 4 Project


Download ppt "Class-level Methods and Inheritance Part 2"

Similar presentations


Ads by Google