Jan 23 2009

AS3 Creating Custom Event Listeners

Category: ClassesHadi Tavakoli @ 8:50 pm

Maybe you have heard a lot about creating custom listeners, well, doing this is very easy in AS3 now! At least it will look simple when you figure out how! But from my personal experience, I don't think it would be a good idea on some occasions. first let's see how the custom listeners will be created using dispatchEvent Here is what I was going to do initially (I changed my mind later!). I was trying to build a class for loading external files into our movie so it will make the job easier without forcing me to deal with the Loader and URLRequest classes each time. I was hoping on creating a class where I can simply load something like this:

var myImageLoader:ImageLoader = new ImageLoader("00.jpg", this); 

so I wrote the class like below considering all the different listeners that you may want to deal with when loading an external file.


		  
		  
		  
		  
		  
		  
		

As you see, we get the input as one url which will be the address of the file to be loaded and a movieclip target where we want to place the loaded content to. I have used the good dispatchEvent event to create custom listeners with a new name so it enables me to create new listeners. and I could use the new listeners like below


		  
		  
		  
		  
		  
		  
		

Very nice and clean idea you might think, yeah? OK, I used to agree with this too, but nope, forget about it! :( let me get you to the point quickly, Your custom ProgressEvent will NOT work! I'm not sure if it's right to say this for sure, but I tested this in different ways for a thousand times, but it simply won't work and will just send it's properties, bytesTotal and bytesLoaded always zero "0"! which makes it impossible to build any preloader using your custom listener! I also tried digging in the web but I didn't manage to find any article or anything talking about this little issue. Maybe it's a possible bug in AS3 and how dispatchEvent works? I really don't know. I hope someone can give some idea about this. but anyway, in my case, I don't have time to spend more time on this! So I decided to create a function for this reason and pass parameters of whatever I wish to load into the function directly and use the listeners directly. Oh, don't take this wrong, other custom listeners will work just fine and the problem seems to be with the ProgressEvent only. so if you are not going to use the progress event, you will still be able to create your own custom listeners in a separated class. But I personally rather create a function now as it will help me out of the headache at least. if anyone has a better experience with this, please do let me know. I'm going to get back to my Box class and clean it up with an almost stand alone function for taking care of the loading process of external files. hmm, I don't feel satisfied with having to do it with a function in every class I may need something to load... but I have to do this until I find a work around the problem.  

Problem Solved!

January 25th, 2009
ok, I need to edit my post and correct the ImageLoader class we had built before. Below will be the correct class as in my last post I didn't sent the necessary information via dispatchEvent! So there's NO bug with ProgressEvent :) I was just an idiot not to fining the right answer.


		  
		  
		  
		  
		  
		  
		

The only unsolved question in my mind are bubbles and cancelable which I see them a lot of places but can't understand what they mean exactly yet. I will post an article when I did. Anyhow, I think it's a good idea now to use the ImageLoader class because soon we need to create some extended classes from this ImageLoader to load images in Bitmaps... Regards, Hadi

post updated on http://www.emstris.com/2009/02/custom-event-listener-final/

Tags: , , , , , , ,

14 Responses to “AS3 Creating Custom Event Listeners”

  1. ron says:

    I was delighted when I found this but I can’t get it to work. Calling it as you do at the begining of your article in cs4 throws up confusing errors such as ‘call to undefined method addFrameScript’?? Strangly no errors occur when compiled with mxmlc but its just a blank swf. I would be grateful for any help.

  2. Hadi Tavakoli says:

    Hi Ron, make sure you are importing all the necessary classes, but rather than that, the current class we have created above is actually not doing very much. I just created that to get myself ready for doing some other things around like creating bitmaps from the loaded JPG, PNG files… In my next post I am going to talk about more about eventListeners as I can see a few direct topics can be found around the net and this has made the understanding of what eventListneres do exactly, a very hard thing… Check back soon though.

  3. AS3 custom event litener final « My Flash Lab! says:

    [...] 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 [...]

  4. wow power leveling says:

    Great article, again. These informations are especially useful …

  5. wow power leveling says:

    A wonderful article…. In my life, I have never seen a man be so selfless in helping others around him to get along and get working. I feel good that there are people like you too. Thanks for this great weblog of yours. Its surely going to get me to go to higher places!

  6. Hadi Tavakoli says:

    @wow power leveling
    I’m glad you find my posts helpful :) that’s all I meant. to share information to have a better world to live in. I have learnt a lot of things from reading other’s posts so why not share my own expiriences too…

  7. aion power leveling says:

    Great article, again. These informations are especially useful

  8. aion power leveling says:

    listener and I did mention how easy it was… but

  9. aion gold says:

    from the loaded JPG, PNG files… In my next post I am going to talk about more about eventListeners as I can see a few direct topics can be found around the net and this has made the understanding of what eventListneres do exactly, a very hard thing… Check

  10. aion gold says:

    what eventListneres do exactly

  11. Hadi Tavakoli says:

    @aion gold well, in as far as I can see the main and the best tool for connecting classes are how a class listens for an event and only when it receives that event will react and does something in reply… now imagine you are building some of your own codes and certainly you will see yourself in a position that some new event will occure rather than those preset listeners in flash like mouseEvent… so, you need to create your own listeners using dispatchEvent ( new Event( “YourCustomListenerName” ) ;

  12. flyff gold says:

    Good article – plenty of food for thought.

  13. wow gold says:

    This is great! Now I want to see your ways for us readers to become more involved! Expect an email later today.

  14. Hadi Tavakoli says:

    @wow gold

    if you have any new idea or some new creations or something, feel free to drop a line here and I will contact you if needed. Thanks.

Leave a Reply

*