在CustomEvent类上调度事件时丢失了

时间:2009-11-15 18:15:24

标签: flash actionscript-3 events

这是我一直在努力实现但没有运气的目标。我有一个名为“btn.swf”的简单swf。它有一个按钮,当它被点击时,它会将一个事件发送给它的父母'Main App';

然后我编写了一个'btn.swf'扩展到的CustomEvent类。 Btn.swf创建一个CustomEvent实例并调度事件。

为了让我将swf内容加载到'Main App'中,我编写了一个扩展Sprite类的CustomLoader。 CustomLoader将内容加载到“主应用程序”中。

我无法在“主应用程序”中捕获已发送的事件。我已经花了一天时间,而且现在已经完全耗尽了我所能想到的一切。我非常感谢任何帮助。

让我回顾一下我的课程结构:

BtnClass扩展了CustomEvent类。 Btn Class调度一个事件 BtnClass随后发布为btn.swf。

CustomMain类使用CustomLoader类来加载btn.swf并通过btn.swf捕获调度的事件。

Dispatched Event在某处翻译时丢失了。或许,我可能正在接近整个事情。

以下是课程:
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上课

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上课

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上课

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');
        }

    }
}

谢谢你。

2 个答案:

答案 0 :(得分:0)

看起来您没有将事件设置为在BtnMain.as中冒泡,因此CustomLoader将不会收到它。尝试在btnHandler中更改此行以将“bubbles”设置为true:

evt = new CustomEvent(CustomEvent.BUBBLE_UP, obj, true, false);

答案 1 :(得分:0)

你几乎就在那里,你只需要听取调度的对象。 所以在你的主类中,请参考你的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');
    }

}
}