Feb 12 2009

AS3 custom event litener final

Category: ClassesHadi Tavakoli @ 2:12 am

This post is too old, read http://www.myflashlab.com/2010/02/21/custom-event-listener/ instead.

in my other post on http://www.emstris.com/2009/01/custom-event-listeners/ I talked about how you can create a custom event listener and I did mention how easy it was... but now that I have managed to learn a lot more about them, I can tell that creating these custom events are not only easy but also VERY easy and straight :)

This is what I was trying to do (I did this in my DropDownMenu class which will write about it in my next post).

I wanted my item list class to have some listeners so when ever a user selects an item from the drop down menu, it would understand that event and sets the data and closes the menu...

Thanks to ultrashock.com I found a very straight answer for this and here I try to briefly sumersize everything quickly.

Before anything, you must know that you can dispatch events using the dispatchEvent() method only if you are extending from EventDispatcher class. Don't be scared! if you're class is already extending Sprite or MovieClip then you are also good to go with the rest because Sprite and MovieClip also extend from EventDispatcher class and considering that, you will be able to use the dispatchEvent() method with no problem.

alright, now that you you know this, take a look at the below sample code and have a few seconds thinking about it:


		  
		  
		  
		  
		  
		  
		

and below will be how you may use it:


		  
		  
		  
		  
		  
		  
		

Note that you don't have to necessarily use the dispatchEvent() method in a setter function! common, be a little creative. in the above code, the main consentration is on changing the variable "name" so as soon as you change it, the listener will be notified... you can add the line

dispatchEvent( new Event( "nameChanged" ) );

to anywhere in your code that your target change is happening and that's it.

I think it would be worthy to have a look at how I have used the above code in my itemList class:

_selectedItem = {lable:e.currentTarget.lable, detail:e.currentTarget.detail, thumb:e.currentTarget.thumb, data:e.currentTarget.data, index:e.currentTarget.index};
dispatchEvent(new Event("itemChanged"));

You see, whenever a new box is selected in the item list, the object which holds the data will be called and the new data will be inserted into it and exactly the same place we have placed our dispatchEvent() method.

Now we can listen to this change! That easy. let me give you a sample... simply like this:

myList.addEventListener("itemChanged", onItemChanged);

and "onItemChanged" will be function which can do whatever you wish that event handler to do :)

Yeah, me too, me too. I couldn't beleive how easy it was to create my own listener!

The examples above will surely clear you about how you can create your own listeners but in my dropdown menu class you will see it more and we will actually do something useful with it.

It's really a shame that I spend a couple of good days searching for this! but that what makes coding beautiful, don't you think so?

Regards,
Hadi

Tags: , , ,

4 Responses to “AS3 custom event litener final”

  1. wow power leveling says:

    This is great! It really shows me where to expand my blog. I think that sometime in the future I might try to write a book to go along with my blog, but we will see…Good post with useful tips and ideas

  2. wow power leveling says:

    Great article. Thanks for the great resource.

  3. JDMulti says:

    Really usefull article, however there is an error in your code.

    The 2nd part you type:

    cls.name = “foo”;
    // “name has been changed to foo”

    cls.name = “bob”;
    // “name has been changed to bob”

    instead cls.name you need to call

    cls.setName(“foo”)
    cls.setName(“bob”)

    Just to correct for the next person trying and figuring out why it doesn’t work. ;)

  4. Hadi Tavakoli says:

    better practice on custom event classes, read my new post here: http://www.myflashlab.com/2010/02/21/custom-event-listener/

Leave a Reply