The focus of this article is to know about power of your own events. Considering you are familiar with events, still we will see some brief introduction about events as it is basic of your own events.
Event means action, as Mouse Clicks, Mouse Moves, Error occurs , File loads. Whatever happens means event occurs. Flash have its own events like MouseEvent, KeyboardEvent, IOErrorEvent etc.
While migrating AS 2.0 to AS 3.0 you can see you have lot of control on events. Then why to create your own event? I had that question. I always think, if I write little bit complex logic I will get output. But I got my answer. Custom event gives you power to control your own actions.
I am not theoretical guy so not able to explain you. Let’s see it in my practical way… with example…..
I created an example for loading bulk of images serially.
1) I created my own event (BulkImageLoaderEvent) who tells me that all images are loaded or one image loaded and also it passes me that loaded data.
2) I created an object for loading all images where I wrote my business logic for loading bulk images one by one. Also assigns my BulkImageLoaderEvent to this object. Means my custom event controls actions in my custom class.
3) I have some code on timeline to pass raw data.
This is my custom event. Which helps in loading bulk of images.It will tell me that all images are loaded also single image is loaded.
I imported Event Class. BulkImageLoaderEvent Class extends Event Class so that it can inherits all properties and methods from Event Class.
I import Bitmap Class because in used Bitmap data type in BulkImageLoaderEvent Class as I am working on images, I think I need to use bitmap data type.
public static const ALL_LOAD_COMPLETE:String="AllLoadComplete";
public static const SINGLE_LOAD_COMPLETE:String="SingleLoadComplete";
This defines your custom event
string constant. ie It defines type of your event.
Constructor function have some parameters, out of which $type, $bubbles, $cancelable are standard parameters of Event Class. Using super keyword we passed parameters to superclass ie Event Class.
Remaining two $paramArr ,$paramBit, we are going to use as per our use.
I put paramArr and paramBit as public so that I can access them out side this class with its instance.
clone() and toString() functions are belong to Event class and we override them with our two new variables. That’s it. Now lets attach it to our custom class to control its actions.
Here we put our business logic to load all images.[as] BulkImageLoader.as
I import all necessary classes. Also I import BulkImageLoaderEvent Class so that I can use this event. I declared all required variables. In constructor function I got Array which contain list of images path. loadImage() function loads images having path in ImgArr Array with index currImgLoaded. Imgurl Loader have EventListener to tell that image is loaded or there is file error.
On imgLoaded function I check with counter whether all images are loaded or not if not loaded I call loadImage() function again. So that I can load next image.
Now use of our custom event,
here I dispatched BulkImageLoaderEvent of type SINGLE_LOAD_COMPLETE. Where single image is loaded and passes e.target.loader.content as its paramBit variable’s data. If error occurs I passed null value.
And when all files loaded I dispatched BulkImageLoaderEvent of type ALL_LOAD_COMPLETE and passes Array of all loaded bitmaps as its paramArr variable’s data.
mainTimeLine in fla file:[as] import BulkImageLoader;
Here I imported BulkImageLoader class so that I can use it.
Here I put my business logic. I loaded XML file , parse it and while creating instance of BulkImageLoader Class I sent an Array which contain path of all files.
And finally I assign BulkImageLoaderEvent of types ALL_LOAD_COMPLETE and SINGLE_LOAD_COMPLETE.
When ALL_LOAD_COMPLETE event occurs I got all bitmaps which are loaded through e.paramArr
And current loaded bitmap through e.paramBit in SINGLE_LOAD_COMPLETE event type.
That is the power of custom event. Now I can reuse this code whenever I want.