AS3: Events & Listeners eventListeners in ActionScript 3.

Slides:



Advertisements
Similar presentations
Flex Component Life-cycle. What is it? Sequence of steps that occur when you create a component object from a component class. As part of this process,
Advertisements

Adding User Interactions actionscript 3.0. Common event-handling tasks Writing code to respond to events Stopping code from responding to events Working.
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Introduction to Macromedia Director 8.5 – Lingo
1What is the Stage. 2How do you open a panel in Flash
Microsoft® Small Basic
Thomas Lövgren, Flash developer
 2004 Prentice Hall, Inc. All rights reserved. Chapter 18 – Macromedia Flash MX 2004: Building an Interactive Game Outline 18.1 Introduction 18.2 Object-Oriented.
Macromedia Flash MX 2004 – Design Professional and Interactivity WORKING WITH SYMBOLS.
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.
SM1205 Interactivity Topic 05: Properties and Events Part II Spring 2011SCM-CityU1.
1 Flash Actionscript Animation. 2 Introduction to Sprites We will now look at implementing Sprites in Flash. We should know enough after this to create.
Work with symbols and instances Work with Libraries Create buttons Assign actions to buttons Unit Lessons.
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
1 Flash Actionscript Adding Interactive Actions Variables.
SM1205 Interactivity Topic 03: Flow Control Spring 2010SCM-CityU1.
1 Flash Programming Introduction Script Assist. 2 Course Description This course concentrates on the teaching of Actionscript, the programming language.
Events (Listeners/Handlers: Mouse, keyboard, timer) Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer
MovieClips & Properties Flash ActionScript Introduction to Thomas Lövgren
© 2011 Delmar, Cengage Learning Chapter 8 Building Complex Animations.
Fish Chomp. The screen where you can see what happens when you play your game is called the STAGE. The SCRIPT BANK is where the types of instructions.
 2008 Pearson Education, Inc. All rights reserved Adobe ® Flash ® CS3: Building an Interactive Game.
PROG Object Oriented Programming II PROG Object Oriented Programming II Window Events Multi-screen Applications.
Getting a handle on ActionScript A basic primer for non-programmers.
Lab 6: event & input intro User Interface Lab: GUI Lab Oct. 2 nd, 2013.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Introduction to Flash MX 2004: Action Scripting Lloyd Rieber.
CISC 110 Day 5 OOP Part 2. 2 Outline The Display List Display List Classes Adding and Removing Objects Adding Symbol Instances Managing Object Depths.
Fish Chomp. The screen where you can see what happens when you play your game is called the STAGE. The SCRIPT BANK is where the types of instructions.
UID – Event Handling and Listeners Boriana Koleva
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Roy McElmurry. More:
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Macromedia Flash Design Professional And Interactivity WORKING WITH SYMBOLS.
© 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.
Programming Games Show your rock-paper-scissors. Demonstrate bouncing ball. Demonstrate and examine Bo the dog. Homework: Modify Bo to make your own.
SCRIPT PROGRAMMING WITH FLASH Introductory Level 1.
Tutorial 7 Creating Animations. XP Objectives Learn about animation Create a timeline Add AP divs and graphics to a timeline Move and resize animation.
Video in Macromedia Flash (Optional) – Lesson 121 Video in Macromedia Flash (Optional) Lesson 12.
Review of Previous Class Declaring variables var myVariableName:DataType = variableValue;
Open a new Flash File Action Script 2.0. Create a button like you did last lesson and name it Click to Play.
Cloning in Scratch Computer App Session 6. Cloning in Scratch Learning Objectives: I will clone sprites in Scratch to create copies of an existing sprite.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
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.
DM 11- Flash –Unit C and Interactivity WORKING WITH SYMBOLS.
Lives and Scoring Games Programming in Scratch. Games Programming in Scratch L2 Lives and Scoring Learning Objectives Define a variable Understand 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.
Macromedia Flash MX Design Professional And Interactivity WORKING WITH SYMBOLS.
 Onpress()  Videos loaded  Mouse click  Something that runs when a certain event occurs.
Introducing Scratch Learning resources for the implementation of the scenario
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Chapter 12 Event-Driven Programming
Introduction to Event-Driven Programming
How to work with your sprite
ActionScript Basics 2016 (2.0 – 3.0)
Introduction to JavaScript Events
Events in Action script
CS106A, Stanford University
JavaScript and Events.
Actionscript Session 2 Roy McElmurry.
Event Driven Programming
Week 6: Time and triggers!
Tonga Institute of Higher Education
Programming games Demonstrate cannonball
Group Boxes, Radio buttons and Checked List Boxes
Exercise : Animated Navigation Structure in Animate (= Flash)
Presentation transcript:

AS3: Events & Listeners eventListeners in ActionScript 3

What are events & listeners? An event is something that happens while your movie runs, like:  key press, mouse click, mouse movement, new frame, timer tick, loading an asset… Some events are initiated by the user. Some events happen as part of the program running. They can be used to make your program interactive.

Registering an event listener To respond to an event we must:  Choose which object is going to respond or ‘listen’ to the event and attach a listener to it.  Add the event listener  Indicate which event is being listened to  Give the name of a function that will be called when the event occurs. objName.addEventListener( ); EventName, eventListenerFunction This is a blank text box because it’s the only way I know of hiding the text underneath it. Surely there must be another way.

Registering an event listener: example To create a listener for a click event, attached to a button called ‘btnStart’ that will call a function called ‘startGame’ btnStart.addEventListener(MouseEvent.CLICK, startGame); Function startGame( evt:MouseEvent ) :void { trace(evt.target.label, “ was clicked”); trace(btnStart.label, “ was clicked”); } Using evt.target is more flexible than using the name of the button.

Event listener: example 2 stage.addEventListener(KeyboardEvent.KEY_DOWN, moveShip); To listen for a key press that will call a function called ‘moveShip’ we might attach the event to the stage Function moveShip( evt:KeyboardEvent ) :void { trace(evt.keyCode, “ was clicked”); } remember if you’re needing to check what the keyCode is you should not check the keyCode number (if (keyCode == 40)) but use the constants, like (if (keyCode == Keyboard.DOWN)) instead. You’ll need to import flash.ui.Keyboard to have access to them

Listeners on container affecting children Say you have multiple animated objects and when you click on one you want to make it disappear. Rather than add a listener to each ‘child’ add one to the parent, like this: var container:Sprite = new Sprite(); container.addChild(obj1); container.addChild(obj2); etc… stage.addChild(container); container.addEventListener(MouseEvent.CLICK, hideObject); private function hideObject( evt: MouseEvent ):void { evt.target.visible = false; }

Making the children reappear Perhaps we can make a startButton make the children visible again var btnStart:Button = new Button(); btnStart.label = “Start”; stage.addChild(btnStart); btnStart.addEventListener(MouseEvent.CLICK, startGame); private function startGame( evt: MouseEvent ):void { var countChld:uint = this.container.numChildren; for (var nChild:uint=0; nChild < countChld; nChild++) { this.container.getChildAt(nChild).visible = true; } }

Removing listeners If you add a listener to an object that you later destroy you MUST remove any listeners BEFORE destroying the object. private function killObject( evt: MouseEvent ):void { evt.target.removeEventListener(MouseEvent.CLICK, killObject); evt.target = null; }