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:
-
package events
-
{
-
import flash.events.Event;
-
-
public class GenericEvent extends Event {
-
public var _data:*;
-
-
function GenericEvent (evt:String) {
-
super(evt);
-
}
-
-
}
-
}
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.
-
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);
-
}
-
}
-
}
Now the main class file customEventExample:
-
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;
-
}
-
-
}
-
}
-
}
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.
Last 5 posts in as3
- Flash Player 10 Pure Flash Keyboard using SampleDataEvent - September 3rd, 2008
- Using Flash Player 10 to produce Dynamic Musical Notes - May 20th, 2008
- Flash Player 10 Dynamic Sound Generation - May 20th, 2008
- How to load Pixel Bender in Flash Player 10 - May 19th, 2008
- How to compile and examples for Flash Player 10 or ASTRO BETA - May 16th, 2008
Josh Weatherspoon, A self-proclaimed millionaire who for some reason has to work as a Flash Developer, at Travelocity in Dallas(Southlake), TX.
1 person has left a comment
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.