Events (Listeners/Handlers: Mouse, keyboard, timer) Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer

Slides:



Advertisements
Similar presentations
Adding User Interactions actionscript 3.0. Common event-handling tasks Writing code to respond to events Stopping code from responding to events Working.
Advertisements

Introduction to Macromedia Director 8.5 – Lingo
AS3: Events & Listeners eventListeners in ActionScript 3.
SM1205 Interactivity Topic 04: Properties and Events Spring 2010SCM-CityU1.
Event Handling Events and Listeners Timers and Animation.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 5 Interactive Programs.
Tutorial 15 Working with the Event Model. XP Objectives Compare the IE and W3C event models Study how events propagate under both event models Write a.
1 Flash Actionscript Event Handling. 2 Event Handling Right now we know all about variables lets go back to our text input/output example: Suppose we.
Flash Workshop Flash Workshop :: Agenda  Introductions  Look at a few Flash Examples  Flash Web Sites  Flash Web Applications  Flash Games.
SM1205 Interactivity Topic 05: Properties and Events Part II Spring 2011SCM-CityU1.
Kapi’olani Community College Art 258 Interface Programming II In-class Presentation Week 5A.
1 Flash Programming Introduction Script Assist. 2 Course Description This course concentrates on the teaching of Actionscript, the programming language.
SM1205 Interactivity Topic 06: Iteration and Multiple Objects Spring 2010SCM-CityU1.
Scripts  Director’s scripting language is called Lingo  Lingo can be used to offer your users navigational control of the order of the scenes of your.
MovieClips & Properties Flash ActionScript Introduction to Thomas Lövgren
Animations Flash ActionScript Introduction to Thomas Lövgren
XP Tutorial 5 Buttons, Behaviors, and Sounds. XP New Perspectives on Macromedia Flash MX Buttons Interactive means that the user has some level.
Welcome to CIS 083 ! Events CIS 068.
London April 2005 London April 2005 Creating Eyeblaster Ads The Rich Media Platform The Rich Media Platform Eyeblaster.
Chapter 5 © 2003 by Addison-Wesley, Inc. 1 Chapter 5 JavaScript and HTML Documents.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
JavaScript, Part 3 Instructor: Charles Moen CSCI/CINF 4230.
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
Getting a handle on ActionScript A basic primer for non-programmers.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
Lab 6: event & input intro User Interface Lab: GUI Lab Oct. 2 nd, 2013.
Introduction to Flash MX 2004: Action Scripting Lloyd Rieber.
CSC 361/661 Digital Media Spring 2010 Professor Burg.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
Flash AS3 Event Stiff problem - demo PowerPoint – slide 1 This PowerPoint file is created to demonstrate the problem of intermittent jamming of flash AS3.
Mouse Events & Keyboard Inputs Flash ActionScript Introduction to Thomas Lövgren
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Roy McElmurry. More:
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
© 2011 Delmar, Cengage Learning Chapter 10 Using ActionScript to Enhance User Experience.
1 Actionscript for Flash by Dr SC Li. 2 Understanding more about instances Symbolsgraphics buttons Movie clips Instances (without names) No interaction.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
SCRIPT PROGRAMMING WITH FLASH Introductory Level 1.
Review of Previous Class Declaring variables var myVariableName:DataType = variableValue;
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
Basic ActionScript and PHP Cis 126. Getting Started set up a basic folder structure so we can keep our files organized. Mirror this structure on your.
Host Objects: Browsers and the DOM
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
CISC 110 Day 6 Introduction to Events. Outline Event-Driven Programming Event Classes Hierarchy –Event Class –Mouse Events –Keyboard Events Registering.
Copyright © 2003 Pearson Education, Inc. Chapter 4 – Slide 1 by Michael Kay The Web Wizard’s Guide to Flash.
CISC 110 Day 7 “The Outliers, Part1” hitTest(), Text Input, Frame and Timer Loops, Publishing Flash Content.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
Integrating Components and Dynamic Text Boxes with the Animated Map– Lesson 101 Integrating Components and Dynamic Text Boxes with the Animated Map Lesson.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
Flash Session 3: Events Shiny Yang special thanks to Alex
Topic 02: Introduction to ActionScript 3.0
ActionScript Basics 2016 (2.0 – 3.0)
Introduction to JavaScript Events
Flash Demonstration – Unit 5 – ActionScript 2.0
CS106A, Stanford University
JavaScript and Events.
Event Driven Programming
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Events.
More programming with "Processing"
CHAPTER 6 EVENTS. CHAPTER 6 EVENTS WHAT IS AN EVENT?
Flash Demonstration – Unit 5
Presentation transcript:

Events (Listeners/Handlers: Mouse, keyboard, timer) Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer

Event and common tasks Why events? And why is it so important? Why events? And why is it so important? The answer is simple: We can’t have an interactive application without detecting user inputs! We need events for many different tasks, here’s a few examples: We need events for many different tasks, here’s a few examples: Buttons: Press, release, rollOver, rollOut etc Buttons: Press, release, rollOver, rollOut etc Keyboard: Keypress, keyRelease etc Keyboard: Keypress, keyRelease etc TextInput: Change, TextInput, Enter TextInput: Change, TextInput, Enter Timer: Start and stop timer events Timer: Start and stop timer events Loading data (Event complete) Loading data (Event complete) Animations (Frame Event) Animations (Frame Event)

Event handling For Event Handling in AS3, there are tree important steps we want to identify: For Event Handling in AS3, there are tree important steps we want to identify: 1. The event source: Which object is the event is going to happen to? For instance, which button will be clicked etc 1. The event source: Which object is the event is going to happen to? For instance, which button will be clicked etc 2. The event: What’s going to happen, the thing that we want to respond to? 2. The event: What’s going to happen, the thing that we want to respond to? 3. The response: What step(s) should be performed when the event happens?

EventDispatcher The EventDispatcher class is the base class for all classes that dispatch events The EventDispatcher class is the base class for all classes that dispatch events Event Dispatchers are objects that dispatch events to objects that are registered as listeners Event Dispatchers are objects that dispatch events to objects that are registered as listeners An event system consists of 3 main entities: Event Dispatchers, Listeners, and Event Objects An event system consists of 3 main entities: Event Dispatchers, Listeners, and Event Objects EventDispatcher class methods EventDispatcher class methods addEventListener(); addEventListener(); removeEventListener(); removeEventListener(); dispatchEvent(); dispatchEvent(); hasEventListener(); hasEventListener(); willTrigger(); willTrigger();

Event Objects Let's recall that when an event happens, Flash will create an object that will be sent to the registered function Let's recall that when an event happens, Flash will create an object that will be sent to the registered function This object contains at least the following information: This object contains at least the following information: Type : A string indicating the type of event Type : A string indicating the type of event Target : The instance that sent the event (i.e. a reference to it) Since target refers to an object you then can also extract information about the target, e.g. its label (if it has one) Target : The instance that sent the event (i.e. a reference to it) Since target refers to an object you then can also extract information about the target, e.g. its label (if it has one)  To get this working properly we need to set up our Listener and Handler/Function…

Event Listener and Handler For each event (user action or something else), we need to register a an event listener and an event handler/function For each event (user action or something else), we need to register a an event listener and an event handler/function Let’s look at the basic syntax of this: Let’s look at the basic syntax of this: //add eventlistener to object, specify the event, call the function eventTarget.addEventListener(EventClass.EVENT_NAME, eventHandler); //the event handler/function function eventHandler(eventObject:EventType){ //actions performed in response to the event go here }

MouseEvent The MouseEvent class is useful for activities like: Mouse clicks, mouse navigation, drag and drop, roll over/roll out, drawing by mouse etc The MouseEvent class is useful for activities like: Mouse clicks, mouse navigation, drag and drop, roll over/roll out, drawing by mouse etc In this example we use an event called CLICK for detection of a mouse click In this example we use an event called CLICK for detection of a mouse click //add eventlistener to object, specify the event, call the function my_button.addEventListener(MouseEvent.CLICK, clickHandler); //handler/function that proceeds the action function clickHandler(event:MouseEvent){ trace(”Button click: Testing Listener and Handler!”); } Note! The argument in the handler/function contains information about the event Note! The argument in the handler/function contains information about the event

Event Listener Parameters The addEventListener() method can have 5 parameters, the first two are compulsory: The addEventListener() method can have 5 parameters, the first two are compulsory: //params: eventName, function, capturePhase, priority, useWeakReference eventTarget.addEventListener(EventClass.EVENT_NAME, eventHandler, false, 0, true); eventName: EventClass and Name of event ( compulsory) eventName: EventClass and Name of event ( compulsory) function: Function/event handler call ( compulsory) function: Function/event handler call ( compulsory) useCapture (boolean): When this parameter is set to true, we'll listen to the event in the capture phase. By default this parameter is set to false useCapture (boolean): When this parameter is set to true, we'll listen to the event in the capture phase. By default this parameter is set to false priority (int): This is to make sure that a certain event listener has a higher priority than a different event listener priority (int): This is to make sure that a certain event listener has a higher priority than a different event listener useWeakReference (boolean): When this parameter is set to true it will create a weak link between the object and event listener. A weak reference is not counted by the Garbage Collector (reference will be removed next GC sweep). Recommended that we use true useWeakReference (boolean): When this parameter is set to true it will create a weak link between the object and event listener. A weak reference is not counted by the Garbage Collector (reference will be removed next GC sweep). Recommended that we use true

KeyboardEvent The KeyboardEvent class is useful for activities like: Keyboard navigation, keyPress, tabbing etc The KeyboardEvent class is useful for activities like: Keyboard navigation, keyPress, tabbing etc In this example we use the an event called KEY_DOWN for detection of a keyPress In this example we use the an event called KEY_DOWN for detection of a keyPress //add eventlistener to object, specify the event, call the function stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); //handler/function that proceeds the action function keyDownHandler(event:KeyboardEvent){ trace("You just pressed a key!"); trace("You just pressed a key!");}

Common Events Some of the most common events for Mouse and Keyboard (with a migration guide between AS2 and AS3): Some of the most common events for Mouse and Keyboard (with a migration guide between AS2 and AS3):

removeEventListener It’s a good idea to remove listeners that we know will no longer be needed. This could easily be done by the removeEventListener method It’s a good idea to remove listeners that we know will no longer be needed. This could easily be done by the removeEventListener method The removeEventListener method requires two parameters: The event and the function specified when the listener was created The removeEventListener method requires two parameters: The event and the function specified when the listener was created For example like this: For example like this: //remove the listener from button my_button.removeEventListener(MouseEvent.CLICK, clickHandler);

Event Arguments (Multiple Events, One Listener Function) Every handler function has one argument: The event argument. This argument contains data about the event and the event dispatcher Every handler function has one argument: The event argument. This argument contains data about the event and the event dispatcher The most common event arguments are: type, target & currentTarget The most common event arguments are: type, target & currentTarget In this example we can use event.type to catch the mouse events In this example we can use event.type to catch the mouse events //add listener to stage for mouse event stage.addEventListener(MouseEvent.MOUSE_DOWN, handler); stage.addEventListener(MouseEvent.MOUSE_UP, handler); //event function/handler function handler(event:MouseEvent){ if(event.type == "mouseDown"){ if(event.type == "mouseDown"){ trace("Down"); trace("Down"); }else{ }else{ trace("Up"); trace("Up"); }}

TextEvent Let’s look at an example were we use the TextEvent class for detecting typing in a text field: entry_txt is an input text field, and output_txt is a dynamic text field Let’s look at an example were we use the TextEvent class for detecting typing in a text field: entry_txt is an input text field, and output_txt is a dynamic text field //add listerner to the input textfield entry_txt.addEventListener(TextEvent.TEXT_INPUT, updateOutput); //function that picks out the pressed keys function updateOutput(event:TextEvent):void{ var pressedKey:String = event.text; var pressedKey:String = event.text; output_txt.text = "You typed: " + pressedKey; output_txt.text = "You typed: " + pressedKey;}

MouseEvent (Random number) This example generates a random number between on every mouse click, the numbers will appear in a dynamic text field This example generates a random number between on every mouse click, the numbers will appear in a dynamic text field //add eventlistener to object, specify the event, call the function rnd_button.addEventListener(MouseEvent.MOUSE_DOWN, randomNumber); //handler/function that proceeds the action function randomNumber(event:MouseEvent){ rnd = Math.random() * 100; //generate a random number between random_txt.text = String(rnd); //cast number to string, output }

Timer Event The Timer class is very useful if we want to fire an event within an interval (with a specific delay) The Timer class is very useful if we want to fire an event within an interval (with a specific delay) The Timer Object can have two parameters (delay, repeatCount) The Timer Object can have two parameters (delay, repeatCount) delay is compulsory The start method starts the timer (there’s also a stop method) The start method starts the timer (there’s also a stop method) //example of using the timer, delay and repeatCount var timer:Timer = new Timer(1000, 5); //1000 miliseconds = 1 sec //add listener and call the onTimer function/handler timer.addEventListener(TimerEvent.TIMER, onTimer); timer.start(); //start timer function onTimer(event:TimerEvent):void{ //function/handler trace(”Timer ticking!”); //output }

Frame Event The Frame event is very useful for all kinds of animations, it’s basically a frame loop - the event occurs at the rate of the frame rate The Frame event is very useful for all kinds of animations, it’s basically a frame loop - the event occurs at the rate of the frame rate Let’s say out movie has a frame rate of 30 fps, then the event will be called 30 times a second Let’s say out movie has a frame rate of 30 fps, then the event will be called 30 times a second //add the listener to the movieClip, event class, function call my_mc.addEventListener(Event.ENTER_FRAME, enterFrameHandler); //function/handler that animates a movieClip function enterFrameHandler(event:Event){ my_mc.x += 5; //move the clip 5 pixels right every enterframe my_mc.x += 5; //move the clip 5 pixels right every enterframe} Note! More of this in the Animation/Motion lecture… Note! More of this in the Animation/Motion lecture…