Professional Documents
Culture Documents
Event Handler and Event Listener
Event Handler and Event Listener
• Creating objects
• Event Handling
• Creating Event Listener
• Mouse Events
• Keyboards Events
Creating Objects
• Properties
• A property represents one of the pieces of data that are bundled together in
an object.
• An example song object can have properties named artist and title; the
MovieClip class has properties like rotation, x, width, and alpha. You work
with properties like individual variables. In fact, you can think of properties
as simply the “child” variables contained in an object.
• Here are some examples of ActionScript code that uses properties. This line
of code moves the MovieClip named square to the x coordinate 100 pixels:
square.x = 100;
Creating Objects
square.scaleX = 1.5;
Creating Objects
Methods
• A method is an action that an object can perform. For example, suppose you’ve
made a movie clip symbol in Flash Professional with several keyframes and
animation on its timeline. That movie clip can play, or stop, or be instructed to
move the playhead to a particular frame.
• This code instructs the MovieClip named shortFilm to start playing:
shortFilm.play();
• This code makes a MovieClip named shortFilm move its playhead to Frame 1 and
stop playing (like rewinding a video):
shortFilm.gotoAndStop(1);
Creating Objects
Events
• A computer program is a series of instructions that the computer carries
out step-by-step.
• However, ActionScript programs are designed to keep running, waiting
for user input or other things to happen. Events are the mechanism that
determines which instructions the computer carries out and when.
• In essence, events are things that happen that ActionScript is aware of
and can respond to.
• Many events are related to user interaction, such as a user clicking a
button or pressing a key on the keyboard.
Creating Objects
• There are also other types of events. For example, if you use
ActionScript to load an external image, there is an event that can
let you know when the image has finished loading.
• When an ActionScript program is running, conceptually it just sits
and waits for certain things to happen. When those things happen,
the specific ActionScript code that you’ve specified for those
events runs.
Creating a Rectangle/Square
rect.graphics.beginFill(0xFF0000);
rect.graphics.drawRect(100,100,50,100);
rect.graphics.endFill();
Creating a Rectangle/Square
• Create a RED square at coordinate(100,50) from top left corner, and width and height = 100;
Creating a Rectangle/Square
circle.graphics.beginFill(0x0000FF);
circle.graphics.drawCircle(250,150,50);
circle.graphics.endFill();
Creating a Circle
• Create a circle at
coordinate(250,150) from
top left corner, with radius
= 50 (this will give us 100 in
width, same with the width
of the rectangle).
• Change the color into BLUE
Exercise!
• The event source: Which object is the one the event is going to happen
to? For example, which button was clicked, or which Loader object is
loading the image? The event source is also known as the event target.
It has this name because it’s the object where the computer targets the
event (that is, where the event actually happens).
• The event: What is the thing that is going to happen, the thing that you
want to respond to? The specific event is important to identify, because
many objects trigger several events.
• The response: What steps do you want performed when the event
happens?
Event handling
• Any time you write ActionScript code to handle events, it requires these three
elements. The code follows this basic structure (elements in bold are placeholders
you’d fill in for your specific case):
Function eventResponse(eventObject:EventType):void
{
// Actions performed in response to the event go here.
}
eventSource.addEventListener(EventType.EVENT_NAME, eventResponse);
• This code does two things. First, it defines a function, which is the way to specify the actions
you want performed in response to the event. Next, it calls the addEventListener() method of
the source object.
Event handling
function eventResponse(event:MouseEvent):void
{
// Actions performed in response to the event go here.
}
myButton.addEventListener(MouseEvent.CLICK, eventResponse);
Event Listener
• Create an event listener for the square object that will call the
function rectClick.
• Everytime the object rectangle is clicked, it will print out
“Rectangle clicked”.
Event Listener
• Add another event listener for the Circle that will call the function
rectClick and print out “Circle clicked”.
• Try to click on the rectangle and the circle alternately to test the
event listener.
KeyboardEvent
• You can place an event listener on the Stage to listen for and
respond to keyboard input.
• In order to learn the used key you can use two properties that
belong the the Keyboard Event Class:
• keyCode - The key code is a numeric value representing the position of the
key of the keyboard.
• charCode - The character code is a numeric value representing the
character associated with the key.
KeyboardEvent
• In the following code, an event listener captures a key press, and the
key name and key code properties are displayed:
function myKeyDown(e:KeyboardEvent):void
{
trace(e.keyCode);
trace(e.charCode);
}
The difference between these two becomes apparent when you realize that small
and capital "A" letters have the same keyCode but different character codes.
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown)
function myKeyDown(e:KeyboardEvent):void
{
trace("You have pressed: " + String.fromCharCode(e.charCode));
trace("Char Code: " + e.charCode);
trace("---------------------------");
}
KeyboardEvent
• Though this might a good indicative way to inform you that the
keyboard is being used, you are more likely need to associate code
with a press of a certain key only and not the whole of the
keyboard.
• In order to do this you need to use a conditional to compare the
value of the keyCode with a constant representing the key you
need to use.
• The Keyboard Class has a number of constants for all the keys you
are likely to need to use.
• For example, if you want to pass a specific command to be
triggered when the space bar is pressed you can use a conditional
to trigger than command this way:
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{
if (e.keyCode == Keyboard.SPACE){
trace("Success!");
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown)
function myKeyDown(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.SPACE)
{
trace("Space bar pressed!");
}
if (e.keyCode == Keyboard.UP)
{
trace("Up arrow pressed!");
}
if (e.keyCode == Keyboard.LEFT)
{
trace("Left arrow pressed!");
}
}
KeyboardEvent
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
KeyboardEvent