00:00
00:00

AS3: Events 2007-05-04 20:47:27


A$3: Main

/*In this tutorial on events, I'm going to explain how to use events in AS3 to
manipulate objects and stuff when certain things (called events) happen.

First Up: Import Events
*/
import flash.events.MouseEvent;
// includes events such as click, mousedown, mouseup, roll over, etc.
import flash.events.Event;
// the basic frame events
import flash.events.KeyboardEvent;
// keyboard events, such as key down.
// import flash.events.* will import all types of events.
// you dont actually need to import events unless you are writing a class.
//
// Adding events to the stage:
stage.addEventListener(Event.ENTER_FRAME,ente rFrameFunction);
// this adds an enterframe event, meaning updated everyframe, to the stage,
// linked to a function called enterFrameFunction, which is below
// a function is a piece of code that performs a certain task when there are
// variables in the task that differ each time the function needs to be called.
// or, in the case of event functions, they are just used to set aside a certain
// amount of code to be executed at certain times.
// A listener is an object that listens in the .swf file for certain events, and
// calls its function when it hears the events.
function enterFrameFunction(event:Event):void {
// code that will happen every frame
// the :void means that the function returns nothing. It will always be void
// on an event function because there is no place to return something to because
// there is not way to call an event function. That is the job of the event
// listener.
}
stage.addEventListener(MouseEvent.CLICK,click Function);
// adds a click event, meaning press+release over the same object
function clickFunction(event:MouseEvent):void {
// code that will happen on click
}
// from now on, I'm not going to explain the code so much, since it should be fairly
// straightforward
stage.addEventListener(MouseEvent.MOUSE_DOWN,
mouseDownFunction);
function mouseDownFunction(event:MouseEvent):void {
}
stage.addEventListener(MouseEvent.MOUSE_UP,mo useUpFunction);
function mouseUpFunction(event:MouseEvent):void {
}
// A list of mouse events:
/* MOUSE_MOVE
MOUSE_OUT
MOUSE_OVER
MOUSE_WHEEL
DOUBLE_CLICK may give you some problems
ROLL_OVER
ROLL_OUT */
// And some frame events:
/* UNLOAD
FULLSCREEN
ADDED
REMOVED
ACTIVATE
INIT
MOUSE_LEAVE */

//Keyboard events are just KEY_DOWN and KEY_UP
stage.addEventListener(KeyboardEvent.KEY_DOWN ,keyDownFunction);
function keyDownFunction(event:KeyboardEvent):void {
trace(event.keyCode)
// the event specifies the type of event that was called, and the keyCode is
// a property of the KeyboardEvent which tells what key was pressed.
}
//
//Adding Events to objects
//
import flash.display.*;
var circle:Sprite = new Sprite()
with (circle.graphics) {
lineStyle(1)
beginFill(0xFF0000,1)
drawCircle(10,10,10)
}
addChild(circle)
// put the mc on the stage.
circle.addEventListener(Event.ENTER_FRAME,cir cleFrame);
// the circle in front of addEventListener specifies that the listener is being
// add to the circle rather than the stage.
function circleFrame(event:Event):void {
//code here
//use event.target to change the properties of the circle
event.target.x++;
}
circle.addEventListener(MouseEvent.MOUSE_DOWN ,circleDown);
function circleDown(event:MouseEvent):void {
trace("you pressed the circle");
}
//
//removing listeners
//
circle.removeEventListener(Event.ENTER_FRAME,
circleFrame)
// the parameters must be the same as those specified in the addEventListener function

thanks for reading, I hope you understand how to use events in AS3 now.

Response to AS3: Events 2007-05-04 21:14:16


Fair play to you.

Yeah, Events seem to be the backbone of AS3 - things that we all took for granted in AS2 & 1 (eg onMouseDown, onKeyPress, onEnterFrame, onRelease, etc), have all pretty much been replaced with events and event listeners. While listeners were an 'optional extra' in previous versions (I used Key Listeners occasionally, that was about as far as I went into Listener territory), anyone wishing to take the step forward into the next generation of actionscript NEEDS to get to grips with them, and how they work. I think this is one of the most major changes, and one of the most important to comprehend


- - Flash - Music - Images - -

BBS Signature

Response to AS3: Events 2007-05-04 21:19:04


An alright tutorial. What I didn't like was that whenever you were typing normal text you were quoting it out. That was a little distracting. I think you should have included in it that you must have the event parameter in the function for it to work. When I first started AS3 I didn't know that and I spent a while trying to figure it out. I gave up soon after and delayed learning it til flash 9 came out.


========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

BBS Signature

Response to AS3: Events 2007-05-04 22:25:24


What I would love to learn is how to have a parameter inside the function that you add to the event....

ie-
addEventListener(Event.ENTER_FRAME, blah(10, false, "pie"));

but noo, it cant be that easy now can it?

If anyone knows how to do this PLEASE LET ME KNOW

Response to AS3: Events 2007-05-04 22:32:34


At 5/4/07 10:25 PM, Pyromaniac wrote: What I would love to learn is how to have a parameter inside the function that you add to the event....

ie-
addEventListener(Event.ENTER_FRAME, blah(10, false, "pie"));

but noo, it cant be that easy now can it?

If anyone knows how to do this PLEASE LET ME KNOW

Just set some variables to the values you need and use those.

Response to AS3: Events 2007-05-04 22:46:15


At 5/4/07 10:25 PM, Pyromaniac wrote: What I would love to learn is how to have a parameter inside the function that you add to the event....

ie-
addEventListener(Event.ENTER_FRAME, blah(10, false, "pie"));

but noo, it cant be that easy now can it?

If anyone knows how to do this PLEASE LET ME KNOW

you cannot do it ever. anyways why would you want to?

Response to AS3: Events 2007-06-04 12:27:45


Just thought I'd mention that all of the events (eg Event.ENTER_FRAME) are just string variables that store the name of the event, and it is much easier to type the string in yourself eg:

addEventListener("enterFrame", blah);
addEventListener("mouseDown", blaah);

etc.


SCGMD4 is on the way! @scgmd4

If a picture is worth a thousand words, a game is worth a play.

BBS Signature

Response to AS3: Events 2007-07-22 21:36:53


At 5/4/07 10:46 PM, LesPaulPlayer wrote:
At 5/4/07 10:25 PM, Pyromaniac wrote: What I would love to learn is how to have a parameter inside the function that you add to the event....

ie-
addEventListener(Event.ENTER_FRAME, blah(10, false, "pie"));

but noo, it cant be that easy now can it?

If anyone knows how to do this PLEASE LET ME KNOW
you cannot do it ever. anyways why would you want to?

If you were making a large duplication loop that had events for each object. I would definitely like to know how this is done. Ergo, bump.

Response to AS3: Events 2008-10-14 13:41:16


At 5/4/07 10:25 PM, Pyromaniac wrote: What I would love to learn is how to have a parameter inside the function that you add to the event....

ie-
addEventListener(Event.ENTER_FRAME, blah(10, false, "pie"));

but noo, it cant be that easy now can it?

If anyone knows how to do this PLEASE LET ME KNOW

The way that I found to do this is by using a return function;

thingy.addEventListener(Event.EVENT, thingyEvent(foo))
function thingyEvent(bar:Variable){
return function (e:Event){
trace(bar)
}
}

a little messy, but really nice when looping through things and sending variables along with event calls

Response to AS3: Events 2008-10-14 15:54:58


If you want to pass additional information in an Event you have to extend the Event class and dispatch the new Event subclass. I wrote about such things right here.

Response to AS3: Events 2008-10-16 03:29:05


No, I do not think that the callback is just a string, it is a Function object.

The caller choses the arguments, not the guy who tells the caller what to call. However, methods are clingy, no matter how hard you pull, the object that they belong to will not get lost. If you need any listener specific data, use a method and store that data in the object the method belongs to. But you do not need to do that in some cases, since the listener does get one argument at all times, the event object. And the event object got a few nifty properties, like a reference to the object that the event was fired for and the object you actually attached the listener to. Yes, this means that they can differ, and that's good. You can listen for clicks on a button even if the user clicks on a MC that is part of the button.


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.