Event Driven Programming

Download Report

Transcript Event Driven Programming

Event Driven
Programming
Chapter 5
Sequential Programming




Computer-Centric
Program Runs as Programmer
Intended
Programmer Sets the Order of Action
Programmer Controls the Program’s
Flow
Event Driven Programming




User-Centric
Computer User Determines the Order
of Actions
Programs are Interactive
Flow of Control is Determined at
Runtime
• User Clicks Mouse / Presses Key
• Object in Scene Moves to create a
Condition
Event


Something that happens
Can be caused by computer or user
• Mouse Click
• Key Press
• System Clock Tick


Can occur differently each time the
program is executed
Events are WORLD Level
Event Handling




Response to an event
Action or Sequence of Actions carried
out when Event Occurs (Methods)
Events TRIGGER response
Programmer must think of all
possible events
• Plan a Response for Each (Event
Handler)
Linking

Event Handling Methods must be
linked to the Specific Event
Event Occurs which causes the Event
Handling Method to execute
Testing

Testing of Event Driven Programs is much
more difficult
• Programmer does not know flow of program
• Flow of Program changes every time program
is executed
• User’s Actions are difficult to pre-determine

Use Incremental Development and Testing
•
•
•
•
•
Write a Method
Test it
Write a Method
Test it
Etc.
Example Program

Create a “Whack A Mole” program.
Use the whackAmoleBooth object
from the amusement park gallery.
• Pressing the “M” key makes the mole
pop up
• Clicking the mouse on the mole makes
the bopper try to hit the mole
Design Storyboards
Event: Key Press Letter M
Response:
Make mole lift out of booth
Mole says “Get me if you can!”
Make mole go back into booth
Design Storyboard
Event: Mouse Click on Mole Object
Response:
Lift Bopper off Booth
Move Bopper to Booth towards Mole
Lift Bopper off Booth
Create Methods

World Level or Class Level Methods?
• Why

Create moleAppear method
Linking Event to Method


Create New Event
When a Key is Typed
Linking Events to Methods


Select Specific Key
M  Make Mole Appear
Link Event to Method

Select Method to Link
wackAmoleBooth, moleAppears

TEST

Create Method



Create bopMole method
Link to Mouse Click on Mole Event
Test
Run the Program

Try to Whack The Mole
• Click “M” to make moleAppear Event
Handling Method execute
• Mouse Click on Mole to make bopMole
Event Handling Method execute
Homework


Chapter 5
5-1 Exercises
• 1  Flight Simulator
• 4  Typing Tutor
Events with Parameters

Remember: Parameters are how we
send information to the methods
• Customize Methods for different
objects/values
Create Scenes/Methods

Example: (Textbook page 125)
A firetruck has been called to an
emergency in a burning building. A
person and a fire object has been
placed on each floor. When each
person is selected, the truck will
need to extend its ladder so that
each person can climb down to
safety.
Design






Event 1 – Click on 1st person
Responding Method – Save person on 1st
floor
Event 2 – Click on 2nd person
Responding Method – Save person on 2nd
floor
Event 3 – Click on 3rd person
Responding Method – Save person on 3rd
floor
Design 2


Events: Click on Person
Responding Method: SavePerson
Point the ladder at the correct floor (parameter –
Which floor)
Extend the ladder correct amount (parameter –
How far)
Specific person (parameter – Which person)
slides down ladder to the firetruck
Pull the ladder back in correct amount
(parameter – How far)
Create Scene



Burning Building (building)
Firetruck (vehicles)
3 People – use whichever you want
• RandomGirl1
• RandomGirl2
• RandomGuy2

FireAnim – www.alice.org
• Gallery
• Save As name.a2c (not ZIP file)

Put in correct Alice gallery folder:
Alice\Alice\Required\Gallery\
Create Method as Before


World Method or Class Method?
Parameters:
• whichFloor (object)
• whichPerson (object)
• howFar (number)
SavePerson Method
Link Method to Event
Test the Program


Run the program
VERY Important when using
parameters
• Run several times
• Number Parameters



Small Number
Large Number
Negative Number
Complex Problems


Responses to events may involve
multiple actions
Methods can call other methods
• Parameters can be used when methods
call methods
Homework

5-2
• Problems 12 & 14