Forum Topic: AS3: Events

(1,903 views • 10 replies)

This topic is 1 page long.

<< < > >>
None

LesPaulPlayer

Reply To Post Reply & Quote

Posted at: 5/4/07 08:47 PM

LesPaulPlayer DARK LEVEL 12

Sign-Up: 05/18/06

Posts: 673

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.


None

Denvish

Reply To Post Reply & Quote

Posted at: 5/4/07 09:14 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Vengeance

Reply To Post Reply & Quote

Posted at: 5/4/07 09:19 PM

Vengeance EVIL LEVEL 28

Sign-Up: 03/18/05

Posts: 5,035

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

None

Pyromaniac

Reply To Post Reply & Quote

Posted at: 5/4/07 10:25 PM

Pyromaniac EVIL LEVEL 18

Sign-Up: 01/14/05

Posts: 2,976

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


None

ShooterMG

Reply To Post Reply & Quote

Posted at: 5/4/07 10:32 PM

ShooterMG EVIL LEVEL 03

Sign-Up: 11/10/06

Posts: 266

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.


None

LesPaulPlayer

Reply To Post Reply & Quote

Posted at: 5/4/07 10:46 PM

LesPaulPlayer DARK LEVEL 12

Sign-Up: 05/18/06

Posts: 673

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?


None

Shinki

Reply To Post Reply & Quote

Posted at: 6/4/07 12:27 PM

Shinki DARK LEVEL 10

Sign-Up: 02/14/05

Posts: 1,571

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.

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

BBS Signature

None

CaptinChu

Reply To Post Reply & Quote

Posted at: 7/22/07 09:36 PM

CaptinChu DARK LEVEL 15

Sign-Up: 09/11/05

Posts: 3,219

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.

All programming problems can be solved with Arrays!

BBS Signature

None

akiersky

Reply To Post Reply & Quote

Posted at: 10/14/08 01:41 PM

akiersky NEUTRAL LEVEL 01

Sign-Up: 10/14/08

Posts: 5

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


None

Ben-Fox

Reply To Post Reply & Quote

Posted at: 10/14/08 03:54 PM

Ben-Fox LIGHT LEVEL 07

Sign-Up: 12/27/03

Posts: 571

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.


None

henke37

Reply To Post Reply & Quote

Posted at: 10/16/08 03:29 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,592

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.


All times are Eastern Standard Time (GMT -5) | Current Time: 07:17 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!