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';
这是我一直试图完成的目标,但没有运气。我有一个名为“btn.swf”的简单swf。它有一个按钮,当它被点击时,它会向它的父“主应用程序”发送一个事件;
I then wrote a CustomEvent Class which 'btn.swf' extends to. Btn.swf creates an instance of CustomEvent and dispatches event.
然后我编写了一个'btn.swf'扩展到的CustomEvent类。 Btn.swf创建一个CustomEvent实例并调度事件。
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'.
为了让我将swf内容加载到'Main App'中,我编写了一个扩展Sprite类的CustomLoader。 CustomLoader将内容加载到“主应用程序”中。
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.
BtnClass扩展了CustomEvent类。 Btn类调度一个事件BtnClass然后发布为btn.swf。
CustomLoader class is employed by the CustomMain class to load the btn.swf and catch the dispatched event by btn.swf.
CustomMain类使用CustomLoader类来加载btn.swf并通过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
以下是类:CustomEvent类
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.
2 个解决方案
#1
0
It looks like you're not setting your event to bubble in BtnMain.as, so CustomLoader won't receive it. Try changing this line in btnHandler to set "bubbles" to true:
看起来您没有将事件设置为在BtnMain.as中冒泡,因此CustomLoader将不会收到它。尝试在btnHandler中更改此行以将“bubbles”设置为true:
evt = new CustomEvent(CustomEvent.BUBBLE_UP, obj, true, false);
#2
0
You are almost there, you just have to listen to the object that is dispatching. So in your main class, refer to your BtnMain, and addEventListener to it.
你几乎就在那里,你只需要听取调度的对象。所以在你的主类中,请参考你的BtnMain和addEventListener。
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()
{
var btnMain:BtnMain = new BtnMain();
addChild(btnMain);
/* This is the line you need */
btnMain.addEventListener(CustomEvent.BUBBLE_UP, vHandler);
}
function vHandler(e:Event):void
{
trace('TARZAN');
}
}
}
#1
0
It looks like you're not setting your event to bubble in BtnMain.as, so CustomLoader won't receive it. Try changing this line in btnHandler to set "bubbles" to true:
看起来您没有将事件设置为在BtnMain.as中冒泡,因此CustomLoader将不会收到它。尝试在btnHandler中更改此行以将“bubbles”设置为true:
evt = new CustomEvent(CustomEvent.BUBBLE_UP, obj, true, false);
#2
0
You are almost there, you just have to listen to the object that is dispatching. So in your main class, refer to your BtnMain, and addEventListener to it.
你几乎就在那里,你只需要听取调度的对象。所以在你的主类中,请参考你的BtnMain和addEventListener。
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()
{
var btnMain:BtnMain = new BtnMain();
addChild(btnMain);
/* This is the line you need */
btnMain.addEventListener(CustomEvent.BUBBLE_UP, vHandler);
}
function vHandler(e:Event):void
{
trace('TARZAN');
}
}
}