copyright 2008 wanda dann, steve cooper, don slater alice workshop variables & conditions

24
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Upload: barrie-ross

Post on 21-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Alice Workshop

Variables & Conditions

Page 2: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Recall: Variable

A variable is an item or value that is likely to change in value.

Variables can belong to: a specific method (local to that method) a class (instance, available to all objects of

that class) the world (global, available anywhere in the

program)

Page 3: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Instance variables

In Alice, an object’s properties are instance variables

Page 4: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Changing a property

A property can be changed, using set

Page 5: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Condition

The set of of variable values describe the condition of objects in an animation program.

As an animation program is running, each statement is likely to change the value of at least one property of an object.

Therefore, conditions change as a program runs.

Page 6: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

New variables

Properties are built-in variables, but we can create our own variables to track other values

Page 7: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Example Problem

A penguin (joe) is watching his son (junior) slide across the ice. A hole in the ice is nearby where junior could fall in. Create an animation where junior slides a random distance toward the water. If junior lands in the water, have him sink under the water and then joe dives in after him.

Page 8: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Textual Storyboard

Do in order junior faces the water junior slides forward a random distance if junior is in the water junior goes down under the surface joe goes in after him Do together

joe and junior pop back up to the surface

Page 9: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Random distance

World function to generate a random number in a range of 0.25 to 3

Page 10: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Checking a Condition

How do we know whether junior is in the water?

Page 11: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Is junior in the water?

Page 12: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Problem Solving

Variables can be used to represent values Let d be the distance of junior to the water Let r be the radius of the water

Page 13: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Creating a variable

A variable has a name and a type

Page 14: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Set variable value

Call a function to set the value of d

Page 15: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Use Math to compute radius

Cascading menus for binary operations

Page 16: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Conditional Execution

Alice provides an If/Else control structure to check a condition

Page 17: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Boolean Condition

Is junior’s distance to the center of the water less than the radius of the water?

Must be either true or false

Page 18: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

If/Else Statement

Create parameter, targetObjPull if/else control into editor

Page 19: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Relational operations are defined in World’s functions

Alice operators◦== means “is equal to”◦ != means “is not equal to”

Operators

Page 20: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Select placeholder operands

Page 21: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Placeholders

In this example, the operands selected from the cascading menus are just placeholders. Need to replace with calls to functions to obtain

distance of penguin to center of water

Page 22: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Demo

Implementation of if/else

Page 23: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Completed Code

Page 24: Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Text

Chapters 3 & 6 Conditions and If/Else

Chapter 10 Variables