addListener.....Ilyas?

Heya.

I saw that Ilyas has a tutorial that mentions he will be posting another tutorial on addListener. I was wondering if anybody can point me to a tutorial for it, or if anybody can explain it for me, with an example?

TIA.

-Al

[size=1]takes out gun
puts silencer
shoots Al[/size]

You were saying? :beam:

That was bloody fast…and extremely helpful…

Seriously though, just a quick rundown on how you would use add Listener. I know the syntax, but I don’t really see why it would be used.

takes off bullet proof vest and tosses it on the ground

And no more gunplay, k? That hurt.

OK, OK, so here we go, listeners.

In flash, with the whole new event model, certain objects broadcast messages. Those objects are Mouse, Key, Stage, and Selection. For instance, if you move the mouse, press a key, resize the movie, or select something, a special message will be sent by those objects.

And sometimes you want certain objects to listen to the messages. So you will add them to the list of the listeners of that object. As simple as that. Now if you can show me that piece of code where I used addListener, I can explain a little bit more.

pom :slight_smile:

I forgot to say: movie clips (and possibly buttons, I can’t remember) are naturally listening to those messages.


// _root "listens" to keyboard events
Key.addListener(_root);

// Now that _root. listens, we can define the function
// executed when we press a key :
_root.onKeyDown = function(){

// What was the last key pressed ?
switch(Key.getCode()){
	case 65: trace("A"); break;
	case 66: trace("B");break;
	case 67: trace("C");break;
	case 68: trace ("D");break;
	default: trace ("Not A, B, C nor D");
	}
}

There’s your code. Why was adding the listener required here?

Because there’s no reason why the _root should listen to the Key messages :slight_smile:

But to be very honnest, I rarely use listeners (only when I have to). I’ve seen it used in the last components, because it seems to be a very good way to optimize files. :-\