PDA

View Full Version : A few questions about Events and Listeners!


MadBeat
09-29-2008, 11:31 AM
Hi All,
For a while now ive been using actionscript3 to achieve some good results when doing my projects without truly understanding whats happening with eventlisteners, by making use of examples on the internet and feel its about time i actually knew the in's and out's of it all. So im hoping someone could enlighten me by answering a few questions i have::confused:

1. When should i use the removeEventListener to optimize my programs? For example:

playAll.addEventListener(MouseEvent.CLICK, onPlayClick, false, 0,
true);

Does this create a new instance of the event each time my button is pressed which should be removed or does it just retrigger the one function each time it is pressed.

2. What is the (e) all about when compared to (evt) e.g:

function onPlayClick(evt:Event):void or function onComplete(e:Event):void

3. One more! If i want to add an EventListener when another event is complete what is the code. I understand the concept of events linked to buttons etc, but I want to simply create an event from another event e.g.

function LayerArrow2(evt:Event):void{
LayerSelection = LayerSelection+1
if(LayerSelection >= 3){
LayerSelection = 3}


if(LayerSelection == 3){
whatVelLayer = "HiHatCL";
whatLayer = "HiHatCL";
layerText.text = "HiHatClosed";
}
if(LayerSelection == 2){
whatVelLayer = "Snare";
whatLayer = "Snare";
layerText.text = "Snare";
}
addEventListener(event.Event, myEvent) // create another event here.
}


Hope this is not too much to ask, any insight would be great. Thx.:)

rawmantick
09-29-2008, 11:48 AM
1. When should i use the removeEventListener to optimize my programs?
You should remove event listener when you don't need it anymore.

2. What is the (e) all about when compared to (evt) e.g:
just an argument (variable) name. You can use absolutely any. Like
function onEvent(mySuperPuperMegaHyperEventArgument:Event): void


3. One more! If i want to add an EventListener when another event is complete what is the code. I understand the concept of events linked to buttons etc, but I want to simply create an event from another event e.g.
I haven't understood a thing ...

MadBeat
09-29-2008, 12:22 PM
LOL! Thx for the help Romantique once again. What i mean in the question is if i have a function that is triggered by an event, (e.g. mouse click.) how do i then create an event that triggers another function from the mouse click event function. I understand the principle a little but cant get the code right. Im finding it hard to understand data types and listener type.

Essentially i want to chain link one event from another.

I hope this makes sense.

rawmantick
09-29-2008, 12:43 PM
addEventListener(MouseEvent.CLICK,onMouseClick);

function onMouseClick(event:MouseEvent):void
{
var myEvent:MyUIEvent = new MyUIEvent(MyUIEvent.MY_CLICK);
myEvent.someField = this.someAnotherField;
dispatchEvent(myEvent); // probably a key line for you
}

Hope it helps...

MadBeat
09-29-2008, 12:55 PM
Hell yeah! That seems like it, thanks very much! Another small question before I start implementing the custom event. If i had one button but with two mouse click eventlisteners linked to it and obviously a function for each event. How does flash does flash perform each function, both at same time or one after other dependent on order in code?

If the second i may not need a custom event! Because as long as the first function is complete, the second will then work as desired.

rawmantick
09-29-2008, 01:09 PM
Functions are performed one after another in order they are added as event listeners.

But you better check it... I'm not sure...