class-level methods chapter 6. class-level method is specific to a class of objects we can give a...

39
Class-level Methods Chapter 6

Upload: yazmin-ledbetter

Post on 14-Dec-2015

223 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Class-level Methods

Chapter 6

Page 2: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class level object Examples

o A person walkingo A skater skating

Unlike world class-level methodso Which has access to multiple classes

Page 3: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Example Skater object does not have a skate

method To create a skate method for ice skater

objects We need to:

(1) Tell Alice to associate a new method with iceSkater class

(2) Write a new method to animate ice skater skating

Page 4: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Associate Animation with Skater

• Select iceSkater tile in Object Tree

• Select methods tab in details panel

• Click on create new method button

Page 5: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Storyboard for skate Method

The slide actions each require several motion instructions

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

Page 6: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Stepwise Refinement

Refinement of slideLeftDo 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 slideRightDo in order Lift left leg and turn upper body forward Lower left leg and return body upright

Page 7: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Demonstration

Concepts illustratedo Method defined for a specific type of object

defines action for that objecto A method can call other methods

skate method calls slideRight and slideLeft

Page 8: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Reuse Writing methods that make ice skater

perform skating motion is complex task Would like to reuse iceSkater skate in other

worlds without writing methods again

Page 9: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Creating A New Class1) Rename iceSkater as

cleverSkater eithera) Double click object

nameb) Or right click name

2) Right click name to save as a new class

3) Alice saves new class as CleverSkater.a2c• Alice v2 Class

Page 10: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Inheritance

The CleverSkater classo Inherits all properties and methods from original

IceSkater classo Has newly defined methods

skate, slideLeft, slideRight

In other programming languageso Inheritance - creates new class based on

previously defined class

Page 11: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Importing CleverSkater An instance of the CleverSkater class can

be added to a new worldo Use File | Importo Set File Type to A2Co Choose class to import

Page 12: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Interacting With Other Objects Suppose you want to write a class-level

method where another object is involved? Ex: method to make skater skate around

another object, the penguin in this scene

Page 13: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Parameters in Class-level Method Solve skate around object by writing a class-level

method with an object parameter Allows you to pass a 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

Page 14: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Guidelines To avoid potential misuse of class-level

methods follow these guidelineso Avoid references to other objects

Use parameters if referencing another object is required

o Avoid calls to world-level methods Will not be saved with new class you created

o Play a sound only if sound has been imported and saved out as part of new class

Page 15: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Demonstration Concept illustrated

o Parameter whichObject is placeholder for the object value passed to it

Ex: penguin

Page 16: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Class-Level Variables as Properties Property is a variable that belongs to an

object Properties can be added to an object

through the creation of class-level variables When the object is saved as a new class the

variables are saved with it Common properties are:

o coloro opacityo isShowing

Page 17: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Tutorial 6-5: Turn Monitor On/Off Create class-level variable that keeps track of

state of computer monitor: on or off

On Off

Page 18: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Storyboard

turnOnOff

Do in order

If monitor is on

set screen color to black

is on set to false

Else

set screen color to no color

isOn set to true

Endif

Page 19: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Storyboard Translated to Alice Notice how new method is preceded by monitor not

worldo Means it is a class-level method not world-levelo Will be saved if class is saved o If world is saved the class will not be saved for future use in

other programs Can only use this in this new method in this specific program

Page 20: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Using turnOnOff Method Variable monitor.isOn is set initially to true In world.My first method

o Turn off monitor using turnOnOFF Variable monitor.isOn is now set to false

o Wait 1 second to see effecto Turn on monitor using turnOnOFF

Page 21: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Built-in Functions

We have been using built-in functions so far

Page 22: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

How a Function Works

A functiono Receives value(s) as arguments in parameterso Performs computations on the value(s)o Returns (sends back) a value

Page 23: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Types of Functions

The function type depends on the type of value it returns

Exampleso Numbero Specific objecto Boolean (true or false)o Coloro Other property values…

Page 24: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Create Function Click on create new function in class or world you want

it to be in

Name function and choose type of function A blank function will be created with return statement

Return statement will be returned when function is doneo Place value you want returned in return statement

Page 25: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Function Window Since no action is performed in a function all

instructions will be performed in sequential ordero No need for Do in order and Do togethero All will be Do in order

Notice that you can placereturn elsewhere in function

Page 26: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Calorie Example A high school jock wants to know how many

calories a cookie has He asks a lunch lady She consults the newly created calorie

functiono 1 meter wide cookie = 1000 caloriesNumber Function caloriesParameter: food

Local Number variable: numCaloriesIf food = cookie

numCalories = cookie’s width *1000Return numCalories

Page 27: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Storyboard Translated to Alice My first method has jock as lunch lady for the

calories in the cookie The lunch lady checks how many calories with the

calorie function and answers with the amount of calories

Page 28: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Visual Effects and Animation Billboards Fog Vehicles asSceneBy Circling Other Objects Pose Programming the Camera Creating Dummy Objects

Page 29: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Billboards

Graphical images that have been inserted into the world

Can insert images that are JPEG, GIF, TIF Known as “Billboard” Images are flat, 2D

with height and widthno depth

Can be used as backgrounds or scenery or used to giveinfo to the viewer!

Boy are youshallow,

you’re only 2D!

Page 30: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Information Billboard

Can be used as sign for instructions

Page 31: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Creating Billboard

Can import images using Make Billboard

In File menu

Page 32: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Fog Alice can give a

look of “mist” to the world

The entire world becomes less visible

Page 33: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Vehicle Property To have two objects move as one One object will be carried by another object

o Ex: person on horse Horse is vehicle for person

Choose properties tabo In property of object being carried

In vehicle choose object doing carrying

Page 34: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

asSceneBy Argument All objects are egocentric To move one object in relation to another choose

asSceneBy in more option of a method Ex: To rotate fish around island

o Without asSceneBy fish would go in straight line

Page 35: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Circling Other Objects• Object’s turn method spins the

object around• The turn method can also have

an object circle around anotherobject

• The asSeenBy argument causes an object to turn around the asSeenBy’s objects center point

• Example: the hawk turns one complete revolution, asSeenBy the tree, around the tree

Page 36: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Pose

Once an object has been put into a position or pose, the pose can be restored during an animationo Pose is a property of an

objecto setPose is a method of

an object

Page 37: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Programming the Camera

Camera can be programmed just like other objects

All the same primitive methods and functions are available for it:o Point at, Move, Turn, etc.

Camera can also be a vehicle for other objects!

Page 38: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Creating Dummy Objects

Dummy objects are invisible objects that are placed in the world

Camera then moves to the invisible object toget different perspectives

Page 39: Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class

Homework Read chapter 6 sections 1,2,6,7 Answer questions in handout Do lab assignments after handing in

answered questions Due one week after assigned with 1 week

grace