Hello everyone,
I am just starting working with AS3 after many years working with AS1 and
AS2.
I am trying to rebuild a Game/App development framework I had built in AS2
into AS3
and I want to make sure I get it right from scratch.
All these years I had been working with Delegates and specifically a custom
Delegate class I had writen
which allowed the user to pass extra parameters.
Now for my new framework, I want to use the new extended event model of AS3,
with addEventListener
and subclassed Event objects. The problem is that I cannot find a solution
that does not uses a Delegate object.
I know that in AS3 callbacks are now executed with the proper scope, so this
does not require a delegate as before,
but I still haven't figured out how to pass parameters.
Specifically, I cannot do this in situations like the following:
(please note that a lot of things were deleted in this example, so that you
don't have to read a longer text)
----------------------------------------------------
package com.zefxis.solarwind2 {
public class ImgLoader() {
/*
Various lines of code that initialize the class
and load an external .xml file (images.xml), which triggers "xmlLoaded"
when
the loading is complete.
images.xml contains information for every image that needs to be loaded
*/
private function xmlLoaded(e:Event):void {
// parse xml data
// for every image, get its id, the file path
// and a bunch of other information which is not shown here
// loop through each node and load the corresponding image
var id:String; // = blah blah
var path:String; // = blah blah
loadImage(id, path);
// end loop
}
private function loadImage(id:String, file:String):void {
// 1st Option
Solution using just a method reference. No parameters can be passed
loader.contentLoaderInfo.addEventListener(Event.INIT, imageLoaded);
/* 2nd option
The only solution I know of that passes parameters, but it includes a
Delegate object
loader.contentLoaderInfo.addEventListener(Event.INIT,
Delegate.create(imageLoaded, id));
*/
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,
ioErrorHandler);
loader.load(new URLRequest(file));
}
private function imageLoaded(event:Event, id:String):void {
var loader:Loader = LoaderInfo(event.target).loader;
var bitmapData:BitmapData = Bitmap(loader.content).bitmapData;
// Store the loaded bitmapData in an object
// I need to have its id, so that I know which image I am storing, how to
manipulate it (e.g. slice it)
// and be able to use it later in my application by calling it with its
id.
bitmaps[id].bitmap = bitmapData;
loadedImages++;
if (loadedImages == totalImages)
dispatchEvent(new Event(Event.COMPLETE));
}
}
}
----------------------------------------------------
Well, that's it.
I am puzzled. I tried to find help online but couldn't find anything useful.
I am somehow convinced that I can achieve what I'm after without using a
Delegate,
(which is not the cleanest thing to do as I understand), and that the new
Event model in AS3
provides all the tools I need.
Am I right?
Thank you,
Dimitrios
---------------------------------
Dimitrios Bendilas
Game Designer/Lead Developer
Total Eclipse Games
www.totaleclipsegames.com
Web Consultant
ZEFXIS
www.zefxis.gr
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com