class-level methods

Post on 21-Jan-2016

87 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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 level object Examples A person walking A skater skating Unlike world class-level methods Which has access to multiple classes. - PowerPoint PPT Presentation

TRANSCRIPT

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 level object Examples

o A person walkingo A skater skating

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

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

Associate Animation with Skater

• Select iceSkater tile in Object Tree

• Select methods tab in details panel

• Click on create new method button

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

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

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

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

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

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

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

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

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

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

Demonstration Concept illustrated

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

Ex: penguin

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

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

state of computer monitor: on or off

On Off

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

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

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

Built-in Functions

We have been using built-in functions so far

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

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…

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

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

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

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

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

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!

Information Billboard

Can be used as sign for instructions

Creating Billboard

Can import images using Make Billboard

In File menu

Fog Alice can give a

look of “mist” to the world

The entire world becomes less visible

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

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

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

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

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!

Creating Dummy Objects

Dummy objects are invisible objects that are placed in the world

Camera then moves to the invisible object toget different perspectives

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

top related