【问题标题】:Custom Events with Singleton EventDispatcher, using getDefinition from child带有 Singleton EventDispatcher 的自定义事件,使用来自 child 的 getDefinition
【发布时间】:2011-06-11 02:07:07
【问题描述】:

编辑:无论出于何种原因,它都可以在浏览器中运行,但在 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 {}

静音图标.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);
    }

}

}

【问题讨论】:

    标签: actionscript-3 events singleton


    【解决方案1】:

    getDefinitionByName(className) 方法只有在 className 在你的代码中被提及时才有效。您可以将className 类导入您要调用getDefinitionByName(className) 的文件中。这应该会有所帮助!

    【讨论】:

    • 迟到的评论,但确实有帮助!谢谢!
    猜你喜欢
    • 2014-08-30
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    相关资源
    最近更新 更多