So a couple of weeks ago a friend and I were talking about the now hardship of as3 and retrieving class properties when building app.
Ok, lets say in Class A I load an image.
Class B – I am calling two copies of Class A to make two loaded images. make Objects in Class B. If I try to space them equally with classAobj2.x = classAobj1.width + 5,
I get “5″. That is a problem.
(“But Josh, you shouldn’t have a ton of loader objects.” I know I should make a iterator or something but I’m not there yet)
devonair on gotoandlearn.com forums told me about how to pass that var buy creating a custom event. It works but it’s a waste of time to do that for every time you need it. The same friend I was talking to gave me the great “catch all” for passing var through certain events.
See here:
[as]package events
{
import flash.events.Event;
public class GenericEvent extends Event {
public var _data:*;
function GenericEvent (evt:String) {
super(evt);
}
}
}[/as]
So I tested it and it’s nice I can pass a crap load of stuff because it’s an Object i.e.: generic._data = ({width: load.width, height: load.height});
So let’s do that example:
We are going to make three classes, build it like this:
- /root
- -customEventExample.as
- /events
- -GenericEvent.as
- /display
- Loading.as
Let’s start w/ Loading.as because we already have the GenericEvent.
[as]
package display
{
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
import events.GenericEvent;
public class Loading extends Sprite
{
private var load:Loader;
public function Loading()
{
load = new Loader();
load.load(new URLRequest(“http://www.kornitaly.com/Discografia/Korn.jpg”));
addChild(load);
load.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandler);
}
private function loadHandler(event:Event):void
{
var generic:GenericEvent = new GenericEvent(Event.COMPLETE);
generic._data = ({width: load.width, height: load.height});
dispatchEvent(generic);
}
}
}[/as]
Now the main class file customEventExample:
[as]
package {
import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.display.Shape;
import flash.events.MouseEvent;
import events.GenericEvent;
import display.Loading
import flash.events.Event;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
[SWF(width="450", height="450")]
public class customEventExample extends Sprite
{
private var _width:int;
private var image:Loading;
private var image2:Loading;
public function customEventExample()
{
image = new Loading();
image2 = new Loading();
addChild(image);
addChild(image2);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.EXACT_FIT;
image.addEventListener(Event.COMPLETE, clickHandler);
image.addEventListener(Event.COMPLETE, clickHandler);
}
public function clickHandler(event:GenericEvent):void
{
_width = event._data.width
trace(_width);
if(image2)
{
image2.x = _width + 5;
}
}
}
}
[/as]
dowload zip
I think this is called a proxy of some sort. I’m still learning Design Patterns. Also I’ve learned you can achieve this w/ the DataEvent.
hey, thanks for this tutorial. After 3 or 4 days trying to figure out how to do this I was about to give up. Your tips and example were a life saver. I love how easy is to pass any sort of data with this class. Amazing.