Hello,
This is the objective I have been trying to accomplish but having no luck. I have a simple swf named "btn.swf". It has a button and when it is clicked, it dispatches an Event to it's parent 'Main App';
I then wrote a CustomEvent Class which 'btn.swf' extends to. Btn.swf creates an instance of CustomEvent and dispatches event.
In order for me to load swf content into 'Main App', I wrote a CustomLoader which extends Sprite class. CustomLoader loads the content into 'Main App'.
I am unable to catch the dispatched event in 'Main App'. I have spent day on it and now pretty much exhausted all that I could think of. I would highly appreciate any help with this please.
Let me recap my class Structure:
BtnClass extends CustomEvent class. Btn Class dispatches an event BtnClass is then published as btn.swf.
CustomLoader class is employed by the CustomMain class to load the btn.swf and catch the dispatched event by btn.swf.
Dispatched Event is being lost in translation somewhere. I may be approaching the whole thing wrong, perhaps.
Here are the classes:
CustomEvent
class
package com.apps.comps.btn
{
import flash.events.*;
public class CustomEvent extends Event
{
public var data:*;
public static const BUBBLE_UP:String = 'BubbleUp';
public function CustomEvent(type:String, customData:Object, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
this.data = customData;
}
override public function clone():Event
{
return new CustomEvent(type, data, bubbles, cancelable);
}
override public function toString():String
{
return formatToString('CustomEvent', 'type', 'data', 'bubbles', 'cancelable');
}
}
}
BtnMain
class
package com.apps.comps.btn
{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.errors.*;
import fl.controls.*;
public class BtnMain extends Sprite
{
public var evt:CustomEvent;
public var obj:Object = {name:'JOHN', game:'RUNNING'};
public var data:*;
public function BtnMain()
{
trace("BTN MAIN INIT");
createBtn();
}
public function createBtn():void
{
var btn:Button = new Button();
btn.label = 'CLICK ME';
btn.x = btn.y = 200;
btn.addEventListener(MouseEvent.CLICK, btnHandler);
addChild(btn);
}
public function btnHandler(e:MouseEvent):void
{
evt = new CustomEvent(CustomEvent.BUBBLE_UP, obj, false, false);
this.dispatchEvent(evt);
trace('...');
}
}
}
CustomLoader
class
package com.apps.comps
{
import flash.display.*;
import flash.net.*;
import flash.events.*;
import com.apps.comps.*;
public class CustomLoader extends Sprite
{
// URLLoader already has a data property, so I used extraData
private var _path:String;
public var _ldr:Loader;
public var eData:*;
public function CustomLoader($path:String)
{
this._path = $path;
loadContent(this._path);
}
private function loadContent(e:String):void
{
this._ldr = new Loader();
this._ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
this._ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
this._ldr.load(new URLRequest(e));
}
private function progressHandler(e:ProgressEvent):void
{
var percent:uint = Math.round((e.bytesLoaded / e.bytesTotal) * 100);
trace(percent);
}
private function onCompleteHandler(e:Event)
{
e.target.dispatchEvent('BubbleUp');
this._ldr.contentLoaderInfo.addEventListener(CustomEvent.BUBBLE_UP, vHandler);
}
public function vHandler(e:CustomEvent):void
{
trace(e);
}
override public function dispatchEvent(event: Event) : Boolean
{
var customEvent: CustomEvent = new CustomEvent(event.type, extraData, event.bubbles, event.cancelable);
return super.dispatchEvent(customEvent);
}
}
}
CustomMain
class
package com.apps.comps
{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.errors.*;
import com.apps.comps.*;
public class CustomMain extends Sprite
{
var custLdr:CustomEvent;
var obj:Object = {name:'SUPERMAN', game:'FLYING'};
public function CustomMain()
{
//custLdr = new CustomLoader('movies/btn.swf');
//addChild(custLdr);
this.dispatchEvent(CustomEvent(obj));
this.addEventListener(CustomEvent.BUBBLE_UP, vHandler);
}
function vHandler(e:Event):void
{
trace('TARZAN');
}
}
}
Thanks you.