create new method

advertisement
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
o

A person walking
A skater skating
Unlike world class-level methods
o
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
skate:
Do together
move skater forward 2 meters
Do in order
slide on left leg
slide on right leg


The slide actions each require several
motion instructions
Break down these two actions into smaller
steps
Stepwise Refinement
Skate:
Do together
1) move forward 2 meters
2) Do in order
slideLeft
slideRight
Refinement of slideLeft
Do in order
Lift right leg and turn upper body forward
Lower right leg and return body upright
Refinement of slideRight
Do in order
Lift left leg and turn upper body forward
Lower left leg and return body upright
Demonstration

Concepts illustrated
o
o

Method defined for a specific type of object defines
action for that object
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 Class
1) Rename iceSkater as
cleverSkater either
a) Double click object
name
b) 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 class
o
o
Inherits all properties and methods from original
IceSkater class
Has newly defined methods


skate, slideLeft, slideRight
In other programming languages
o
Inheritance - creates new class based on previously
defined class
Importing CleverSkater

An instance of the CleverSkater class can be
added to a new world
o
o
o
Use File | Import
Set File Type to A2C
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 guidelines
o
Avoid references to other objects

o
Avoid calls to world-level methods

o
Use parameters if referencing another object is required
Will not be saved with new class you created
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
o
o
color
opacity
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 world
o
o
o
Means it is a class-level method not world-level
Will be saved if class is saved
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

o
o
Variable monitor.isOn is now set to false
Wait 1 second to see effect
Turn on monitor using turnOnOFF
Built-in Functions

We have been using built-in functions so far
How a Function Works

A function
o
o
o
Receives value(s) as arguments in parameters
Performs computations on the value(s)
Returns (sends back) a value
Types of Functions


The function type depends on the type of value it
returns
Examples
o
o
o
o
o
Number
Specific object
Boolean (true or false)
Color
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 done
o
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 order
o
o

No need for Do in order and Do together
All will be Do in order
Notice that you can place
return 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 function
o
1 meter wide cookie = 1000 calories
Number Function calories
Parameter: food
Local Number variable: numCalories
If food = cookie
numCalories = cookie’s width *1000
Return 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
Boy are you
Known as “Billboard”
shallow,
Images are flat, 2D
you’re only
2D!
with height and width
no depth
Can be used as backgrounds
or scenery or used to give
info to the viewer!
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 tab
o
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 another
object
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
animation
o
o
Pose is a property of an
object
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 to
get 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
Download