Chapter 6

Chapter 11:
Events
Starting Out with Alice:
A Visual Introduction to Programming
First Edition
by Tony Gaddis
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 11 Objectives
To understand:
– what an event is and how to specify what type of
event can be used
– how to specify a different method to be executed
when the world starts
– the components of a BDE event
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-2
Today’s Agenda
1.
2.
3.
4.
Responding to Events
Handling Key Press and Mouse Events
Using Events in Simulations and Games
Tips for Games and Simulations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-3
6.1
Responding to Events
Event
– Action that take place while a world is playing
– Alice worlds detect events and respond to them
– For example:
• when a user clicks an object with a mouse…it’s an
EVENT!
• when a user types a key on the keyboard…it’s an
EVENT!
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-4
6.1
Alice Events
Process of responding to an event is called
handling the event
Tile for an event MUST appear in the Events
Editor
When world
is first created,
the event when the world starts appears in the
Events Editor
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-5
6.1
Alice Events
Most event tiles require that additional
arguments be specified
The method that is executed in response to the
event is called the event handler
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-6
6.1
Alice Events
When the world starts
While the world is running
When a key is typed
While a key is pressed
When the mouse is clicked on something
While the mouse is pressed on something
While something is true
When something becomes 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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-7
6.1
Specialized Events
Some events from the previous list do not
appear in the Events Editor
– General event must
first be created
– Right-click and
select change to
Changes an event
to a specialized
event
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-8
6.2
Handling Key Press and Mouse Events
Alice can detect when a key on the keyboard has
been pressed
Key Press Events (such as when a key is typed)
– The event is triggered when the user
types a key
– Key that will trigger the event must
be specified
– any key is the placeholder until the
“real” key is selected
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-9
6.2
Handling the While a key is pressed Event
The event occurs as long as the user holds down
a key
This tile has four different placeholder slots:
–
–
–
–
any key
Begin
During
End
Known as BDE event
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-10
6.2
BDE Event
Begin
– Specifies an action to take place at the beginning
During
– Specifies an action to take place during the event
End
– Specifies an action to take place at the end (when the
event stops occurring)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-11
6.2
Mouse Events / While the mouse is clicked…
When the mouse is clicked on something
Event is triggered when the mouse is clicked on
an object in the world
While the mouse is clicked on something
Event occurs as long as the user holds down the
mouse button while the pointer is positioned over
an object
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-12
6.3
Using Events in Simulations and Games
Programs can have numerous events
– They respond to various user interactions and
internal events
– Note the various events that appear in Island Rescue
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-13
6.4
Tips for Games and Simulations
Random Numbers
– Function that returns a fractional number between 0
and 1
– Function can be added by altering the more editing
tag
• minimum: specifies a minimum value for the random
number
• maximum: specifies a minimum value for the random
number
• integerOnly: if the value is true, the function returns only
whole numbers
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-14
6.4
Tips for Games and Simulations
Random Numbers
– Function that returns a fractional number between 0
and 1
– Function can be added by altering the more editing
tag
• minimum: specifies a minimum value for the random
number
• maximum: specifies a minimum value for the random
number
• integerOnly: if the value is true, the function returns only
whole numbers
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-15
6.4
Tips for Games and Simulations
Playing Audio
– Several classes include sounds
– Sounds appear in the class’s thumbnail in the
gallery
– An object’s properties’
tab also allows for
sound to be imported
and played
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-16
6.4
Tips for Games and Simulations
Importing Sounds
– .wav or .mp3 sounds can be imported to an object
Recording Sounds
– With a mic attached to your
computer, sounds can be recorded
to the computer and then imported
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-17
6.4
Tips for Games and Simulations
Character Builders
– The people class provides many
characters to choose from
– Tool for building own
characters is also included
• hebuilder
• shebuilder
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-18
6.4
Tips for Games and Simulations
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-19
6.4
Tips for Games and Simulations
Hebuilder and
Shebuilder have
custom methods
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-20
6.4
Tips for Games and Simulations
Debugging with print
– Diagnostic messages are statements that are inserted into a
program to display information that can be used in
debugging a program that isn’t working properly
– Addition of the print instruction causes a menu to appear
– Menu shows all the objects that appear in the world
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-21
6.4
Tips for Games and Simulations
Debugging with print
– Diagnostic messages are statements that are inserted into a
program to display information that can be used in
debugging a program that isn’t working properly
– Addition of the print instruction causes a menu to appear
– Menu shows all the objects that appear in the world
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
6-22