23
Events Chapter 7

Events

  • Upload
    korene

  • View
    28

  • Download
    2

Embed Size (px)

DESCRIPTION

Events. Chapter 7. Interactive. The real world is interactive User determines order of actions instead of programmer. Control of Flow. How sequence of actions in program is controlled What action happens first, next, and so on Animations - PowerPoint PPT Presentation

Citation preview

Page 1: Events

EventsChapter 7

Page 2: Events

Interactivity The real world is interactive User determines order of actions instead of

programmer

Page 3: Events

Control of Flow

How sequence of actions in program is controlledo What action happens first, next, and so on

Animationso Movie-style - programmer determines

sequence of actions prior program runningo Interactive - sequence of actions determined at

runtime by user

Page 4: Events

Event

Program reacts to o Mouse clicko Key press on keyboardo Changes that occur during program execution

Control of flow is now controlled by user

Page 5: Events

Events Each time user provides some sort of

input, we say an event is generated o An event is “something that happens”

An event mayo Trigger a responseo Move objects into positions that create a

condition that triggers a response Ex: A collision that moves objects

Page 6: Events

Event Handler Method Called when event occurs Linked to event in Event editor Behavior – when method linked to an

event is performed due to an action

Page 7: Events

Creating An Event

Click on create new event in Events window

Page 8: Events

Alice Events When the world starts When a key is typed When the mouse is clicked on something While something is true When a variable changes Let the mouse move <object> Let the arrow keys move <subject> Let the mouse move the camera Let the mouse orient the camera

See Table 7-1 on page 272 for details of each

Page 9: Events

Specialized Events Some events from the previous list do not

appear in the Events Editor To create specialized event

o Create general event in parenthesis belowo Right-click and select change to action on left below

Specialized Eventso While a key is typed (from When a key is typed)o While the mouse is pressed on something (from

When mouse clicked on)o While the world is running (from when world starts)o When something becomes true (from While

something becomes true)

Page 10: Events

Key Typed Event Alice can detect when a key on keyboard is

pressedo Event is triggered when the user types a keyo Keyboard key that triggers event must be

specified

o any key is placeholder until specific key is chosen Can have event activated when any key is pressed

o To associate event with key press Click on Nothing Choose custom method from resultant drop down menu

Page 11: Events

Tutorial 7-1: Key Press Event Have each fairy flap her wings and move up

and down when a key is pressed called flyo When G is pressed event to cause Gossamer to flyo When M is pressed event to cause Hazelnut to fly

Page 12: Events

Fairy Flying Algorithms

Event: G key is pressed

Response: Do together

Flap wings of Gossamer

Move Gossamer up ½ meter

Do together Flap wings of GossamerMove Gossamer down ½ meter

Event: M key is pressed

Response: Do together

Flap wings of Hazelnut

Move Hazelnut up ½ meter

Do together Flap wings of HazelnutMove Hazelnut down ½

meter

Page 13: Events

Programs for Faeries Flying

Both events can runat same time

Page 14: Events

Mouse Event Interactive programs often allow the user to

use a mouse to click Examples

o Buttons in a windows-based interfaceo Choose targets in a gameo Select items on a form checklist

Page 15: Events

Creating Mouse Event

Click on create new event and choose When the mouse is clicked on something

It can be o Anythingo Any object in the Alice World

Choose event handler that will be run by clicking on nothing

Page 16: Events

Tutorial 7-3: Mouse Event Click on refrigerator door causes door to

openo Can place Alice statement directly in event

declarationo Notice that there is no way to close the door

once it is opened

Page 17: Events

Fridge Door Opens and Closes To get door to open and close you need to

remember the state of the dooro Is it open or closed?o Use a Boolean variable: doorOpen

If doorOpen = true then door is openo Close the dooro Set doorOpen to false

If doorOpen = false then door is closedo Open the dooro Set doorOpen to true

Page 18: Events

Door Open/Close Algorithm

Event: Mouse clicked on fridge door

Response:If doorOpen = true

Turn door right ¼ revolutiondoorOpen = false

ElseTurn door left ¼ revolutiondoorOpen = true

Page 19: Events

Program to Open and Close Door

Page 20: Events

Create Your Own People Models

Use hebuilder and shebuildero In People gallery

o Choose all features of persono Has built-in methods

hello, angry, happy, walk, confused, no, yes

Page 21: Events

Choosing Body Features

• hebuilder/shebuilder• User selects the head, body, and leg types• User selects skin color,

eyes, and mouth• User selects hair style and

color• User selects the clothing

Page 22: Events

People Models Custom Methods

• hebuilder and shebuilder have custom methods

Page 23: Events

Homework Read chapter 6 sections 1 and 2 Do lab assignments in handout Due one week after assigned