-
1. Re: AS3: custom events help needed !!!
theErez Jun 1, 2009 5:54 AM (in response to Ziggizag)ziggi,
what you try to acomplish isn't that sophisticated, so i'll try to make it as simple as possible.
indeed, in order to solve your problem you need a custom event class. it should look something like this:
package com.events
{
import flash.events.Event;public class ZiggiCustomEvent extends Event
{public static const COMPLETE:String = "complete"; // the event type supported by this event class - you can add more of course
public var customAgrument:* // the argument to pass - cast it to whatever type you need
public function ZiggiCustomEvent(type:String, arg:*, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);customAgrument = arg;
}override public function clone():Event
{
var event:Nana10PlayerEvents = new ZiggiCustomEvent (this.type, this.customArgument, this.bubbles, this.cancelable);
return event;
}}
}
when you dispatch this event, it should look like this:
dispatchEvent(new ZiggiCustomEvent(ZiggiCustomEvent.COMPLETE, 1)); // send the parameter you need
other option is to remove you argument from the constructor's header, and add it 'later', like this:
var ziggiCustomEvent:ZiggiCustomEvent = new ZiggiCustomEvent(ZiggiCustomEvent.COMPLETE);
ziggiCustomEvent.customAgrument = 1;
dispatchEvent(ziggiCustomEvent);
any option you choose, the listener should look like this:
obj.addEventListener(ZiggiCustomEvent.COMPLETE, onComplete);
private function onComplete(event:ZiggiCustomEvent):void
{
trace(event.customArgrument); // retrieve the parameter you sent
}
hope this makes it clearer
eRez
-
2. Re: AS3: custom events help needed !!!
Ziggizag Jun 1, 2009 6:26 AM (in response to theErez)Thank you (the)Erez for your help but I need a little more. I need to understand how exatly pass the argument to event handler.
Please notice, the "normal" image loading process is as follows (timeline script only, no classes):
//--------------------------------------
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;loadImage(someURLaddress);
function loadImage(url):void{
var requesto:URLRequest=new URLRequest();
requesto.url=url;
var loader:Loader=new Loader();
addListeners(loader.contentLoaderInfo);
try{
loader.load(requesto);
}catch(e:Error){
loader.unload();
removeListeners();
}}
function addListeners(d:IEventDispatcher):void{
d.addEventListener(Event.COMPLETE, onComplete);
d.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandling);
}function onComplete(e:Event):void{
var loaderInfo:LoaderInfo = LoaderInfo(e.target);
var loader:Loader = loaderInfo.loader;
removeListeners();
var dob:DisplayObject = loaderInfo.content;
loader.unload();
processImage(dob);}
function ioErrorHandling(e:IOErrorEvent):void{
trace("Error loading image");
removeListeners();
}function removeListeners():void{
removeEventListener(Event.COMPLETE, onComplete);
removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandling);
}function processImage(img):void{
//for instance:
var placeholderMovieClip = getChildByName("placeholder");
placeholderMovieClip.addChild(img);}
//--------------------------------------
So, as you may see - there is a complete process shown how to initiate image loading and how to send loaded image to processing.
Now, I would really like to see that when the initial command is not:
loadImage(someURLaddress);
but:
loadImage(someURLaddress, myArgument);
and final function is not:
function processImage(img):void{
}
but:
function processImage(img, arg):void{
}
I would really appreciate that!
Regards,Ziggi
-
3. Re: AS3: custom events help needed !!!
theErez Jun 1, 2009 6:39 AM (in response to Ziggizag)first - if you want to use a custom event, you'll have to use classes, timeline script only won't do.
and second - if i were you i'd create a 'ImageLoader' class, with a public function 'loadImage' which expects both url and 'myArgument'', and eventually will dispatch the custom event you prepared with the 'myArgumanet' u sent. that way, you'll have an instance of the 'ImageLoader' class, which you'll call with a different argument for each image, and add a listener to that instacne which will receieve that argument.
-
4. Re: AS3: custom events help needed !!!
Ziggizag Jun 1, 2009 6:52 AM (in response to theErez)Erez(the) - please, of course I know that using classes is necessary for custom events. Sure. But what is tricky moment is actually "dispatching".
Can you please write here a short example of this LoadImage class, so we can all see how to pass extra argument through?
Please!
-
5. Re: AS3: custom events help needed !!!
theErez Jun 1, 2009 7:06 AM (in response to Ziggizag)package com
{
import flash.events.EventDispatcher
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;public myParameter:*;
public class ImageLoader extends EventDispatcher
{
public function ImageLoader() {}
public function loadImage(url:String, arg:*):void{
myParameter = arg;
var requesto:URLRequest=new URLRequest();
requesto.url=url;
var loader:Loader=new Loader();
addListeners(loader.contentLoaderInfo);
try{
loader.load(requesto);
}catch(e:Error){
loader.unload();
removeListeners();
}}
private function addListeners(d:IEventDispatcher):void{
d.addEventListener(Event.COMPLETE, onComplete);
d.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandling);
}private function onComplete(e:Event):void{
var loaderInfo:LoaderInfo = LoaderInfo(e.target);
var loader:Loader = loaderInfo.loader;
removeListeners();
var dob:DisplayObject = loaderInfo.content;
loader.unload();var customEvent:CustomEvent = new CustomEvent(CustromEvent.COMPLETE);
customEvent.img = dob;
customEvent.customArgument = myParameter;
dispatchEvent(customEvent)
//processImage(dob);}
privtate function ioErrorHandling(e:IOErrorEvent):void{
trace("Error loading image");
removeListeners();
}private function removeListeners():void{
removeEventListener(Event.COMPLETE, onComplete);
removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandling);
}/*private function processImage(img):void{
//for instance:
var placeholderMovieClip = getChildByName("placeholder");
placeholderMovieClip.addChild(img);}*/
}
}
********************************
var imageLoader:ImageLoader = new ImageLoaer();
imageLoader.loadImage(pathToImage, param);
imageLoader.addEventListener(CustomEvnet.COMPLETE, processImage);
function processImage(event:CustomEvent):void
{
//for instance:
var placeholderMovieClip = getChildByName("placeholder");
placeholderMovieClip.addChild(event.img);trace(event.customArgument)
}
-
6. Re: AS3: custom events help needed !!!
Ziggizag Jun 1, 2009 7:19 AM (in response to theErez)1000 times THANK YOU !!!

