使用Singleton EventDispatcher的自定义事件,使用来自child的getDefinition

时间:2011-06-11 02:07:08

标签: actionscript-3 events singleton

编辑:无论出于何种原因,它在浏览器中有效,但在IDE中编译/调试时无效。

我无法让我的外部SWF接收来自我的单件事件管理器(EventDispatcher)的调度。以下是详细信息:

  • 我使用getDefinition方法将外部SWF中的子项添加到我的主SWF。
  • 我正在使用负责监听和调度的单例EventDispatcher。
  • 使用自定义事件类。

在此代码中,我试图获取一个静音按钮,告诉主SWF已单击静音图标(SoundClipEvent.MUTE_CLICK)。声音被静音后,它应该调度事件(SoundClipEvent.STATE)并向muteIcon确认状态。目前,静音图标成功调度MUTE_CLICK事件,主SWF文档类可以拾取它。 MuteIcon(儿童SWF MC)听不到单身人士的任何消息。

非常感谢您对此问题的帮助!

SoundClipManager.as:

import flash.events.Event;
import flash.events.EventDispatcher;

public dynamic class SoundClipManager extends EventDispatcher {
    private static var isMuted:Boolean;

    public function SoundClipManager(blocker:SingletonBlocker):void {
        super();
        //
        if (blocker == null) {
            throw new Error("Error: Instantiation failed; Use SoundClipManager.getInstance()");
        }
    }

    public static function get muted():Boolean {
        return SoundClipManager.isMuted;
    }

    public static function set muted(value:Boolean) {
        SoundClipManager.isMuted = value;
        //
        SoundClipManager.getInstance().dispatchEvent(new SoundClipEvent(SoundClipEvent.STATE,SoundClipManager.muted));
    }

    public static function getInstance():SoundClipManager {
        if (instance == null) {
            instance = new SoundClipManager(new SingletonBlocker());
        }
        return instance;
    }

    public override function dispatchEvent(evt:Event):Boolean {
        return super.dispatchEvent(evt);
    }

    private static function stateChanged(evt:*) {
        trace('state changed!');
    }        
}

internal class SingletonBlocker {}

MuteIcon.as

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
//
public dynamic class IconMute extends MovieClip {
    public function IconMute() {
        this.addEventListener(Event.ADDED_TO_STAGE,this.addedToStage);
        //
        SoundClipManager.getInstance().addEventListener(SoundClipEvent.STATE,this.soundClipManagerStateChanged);
    }
    //
    //  Methods, Private
    //

    //
    //  Events
    //
    private function muteClick(evt:MouseEvent) {
        SoundClipManager.getInstance().dispatchEvent(new SoundClipEvent(SoundClipEvent.MUTE_CLICK));
    }
    //
    private function addedToStage(evt:Event) {
        this.addEventListener(MouseEvent.CLICK,this.muteClick);
    }
    //
    private function soundClipManagerStateChanged(evt:*) {
                    trace("state changed!");
    }
}

SoundClipEvent.as

package  {
//
import flash.events.Event;
//
public class SoundClipEvent extends Event {

    public static const MUTE_CLICK:String = "muteClick";
    public static const STATE:String = "state";
    //
    public var muted:Boolean;

    public function SoundClipEvent(type:String,muted:Boolean = false) {
        if(muted) this.muted = muted;
        //
        super(type,true,false);
    }

}

}

1 个答案:

答案 0 :(得分:1)

getDefinitionByName(className)方法只有在您的代码中某处className被广泛使用时才有效。您可以在要调用className的文件中导入getDefinitionByName(className)类。那应该有帮助!