How to use Event listeners/Handlers in Flash CS3 using Actionscript 3
If you want to create applications or interactivity for your users then you will definitely need to get to grips with Events it took me a bit of time to fully understand the concept of how to use Event Listeners/handlers in AS3. I could use them OK but when it came to understanding what the code actually meant i got stuck. With Actionscript 3 i prefer much more to learn the inner workings of whats going on with the code and behind the scenes to fully understand the power of AS3 id recommend all readers to do this also.
This tutorial will run you through a simple drag and drop application made in Flash CS3 using ActionScript 3 written as a Class. I have set this up this way to help myself and you get used to writing and using classes as its now becoming a standard for Flash, Flex and Actionscript Developers. Your files are also more organised and your code runs much faster not to mention its a step up towards Object Orientated Programming so its a win win situation.
Fisrt off open a new .fla file and name it (for this tutorial) EventTute otherwise you can name your .fla files as and what you like but it is best practice to atleast relate the name to the project.
(optional) write out Event tutorial as show in image.
.jpg)
Then open a new .as file and name it EvenDemo without any spaces this is where you will place all of your actionscript.
We then enter in the following code (i will explain the code along with it)
fisrt off we set up the package
package {
import the classes we will need to use in the EventDemo class
import flash.display.Sprite;
import flash.events.MouseEvent;
extends the Sprite class so you can use all the properties of a Sprite in the EventDemo class
public class EventDemo extends Sprite {
This is where we set variable called eventSprite making sure its a Sprite
private var eventSprite:Sprite;
The public function is set to Holder because thats what it acts as a holder for the private function this is due to common practice that the publick function should be left well organised and preferably as small as possible.
public function EventDemo() {
Holder();
}
this private function adds a new instance of the eventSprite and then draws it out in AS3 code onto the stage. Note! always make sure that you addChild(); this will add your instance to the display list.
private function Holder():void {
eventSprite = new Sprite();
addChild(eventSprite);
eventSprite.graphics.beginFill(0xff0000);
eventSprite.graphics.drawCircle(260, 320, 100);
eventSprite.graphics.endFill();
These following 2 are event listeners 1 for when the mouse is pressed and the other for when the mouse is up. They are added directly onto the earlier declared variable eventSprite this is becuase we set the functionality of this varible to inherit all the of the Sprite class.
eventSprite.addEventListener(MouseEvent.MOUSE_DOWN,
onMouseDown);
eventSprite.addEventListener(MouseEvent.MOUSE_UP,
onMouseUp);
}
What we have here are 2 functions to handle what is going to happen as soon as the event listeners call out that the event they were looking out for has occured. Note! these event handlers use the same name wirtten at the end of the event listeners. This is so they can communicate with each other when passing information about events.
within these functions there are start and stop drag methods set to move with the mouse when pressed and stop when mouse is up. Thats 1 way of checking this code works, another way that im sure your very fammiiliar with is using the trace function
private function onMouseDown(event:MouseEvent):void {
startDrag();
trace("the mouse is down")
}
private function onMouseUp(event:MouseEvent):void {
stopDrag();
trace("the mouse is up")
}
}
}
.jpg)
Now one thing you may or may not have come across is the Document class text input field in the properties pannel this is in Flash CS3 only. The Document class ensures any class specified within the input field is run as soon as the swf starts. You could use a document class for that purpose and for use with running your other classes via the Document class.
.jpg)
The trace function will display this in your out put window to identify that the code is working.

Hope you enjoyed this tutorial please leave a comment
|
Compare Life Insurance
We search 300 life insurance plans www.protected.co.uk |
If you enjoyed this post, make sure you subscribe to my RSS feed!
Comments
7 Responses to “How to use Event listeners/Handlers in Flash CS3 using Actionscript 3”
Leave a Reply




.jpg)
Is there anyway of making this display in other than the out put window say for example as to make the circle smaller on mouse down to see visually thanks and great tute
Yes abid all you would need to do is in the onMouseDown function set the cicle size less than it already is. so it would be something like
eventSprite.radius (150);
Fantastic tutorial, I now understand why event listeners are used so much. Plus your code was simple and explained in depth. Thank you.
[…] How to use Event listeners/Handlers in Flash CS3 using Actionscript 3 - This tutorial will run you through a simple drag and drop application made in Flash CS3 using […]
[…] Actionscript3 Event listeners in Flash CS3- This tutorial will run you through a simple drag and drop application made in Flash CS3 using […]
[…] task at all. In this article you will learn how to load and process XML files via actionscript. 29) Actionscript3 Event listeners in Flash CS3- This tutorial will run you through a simple drag and drop application made in Flash CS3 using […]
Hi ;o)
Good post, also a great blog layout. Keep it up