我可以通过接口从运行时共享库​​中实例化一个类吗?

时间:2011-12-03 02:01:38

标签: actionscript

在我的Runtime Shred Library SWF中,我有一个名为BackButton的类,它扩展了MovieClip并连接了IGameButton:

package com.game.button
{
    import com.interfaces.IGameButton;

    import flash.text.TextField;

    public class BackButton extends MovieClip implements IGameButton
    {
        public var labelTxt:TextField;

        public function BackButton()
        {
            super();
        }

        public function get label():String
        {
            return labelTxt.text;
        }

        public function set label(value:String):void
        {
            labelTxt.text = value;
        }

    }
}

当我通过执行以下操作将RSL加载到主SWF中时:

var backButtonClassName:String = "com.game.button.BackButton";
var BackButtonClass:Class = getDefinitionByName(backButtonClassName) as Class;
var backButton:IGameButton = new BackButtonClass();

我在运行时遇到以下错误:

  

类型强制失败:无法将com.game.button :: BackButton @ bb12af1转换为com.interfaces.IGameButton。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的getDefinition来电需要引用必须在加载程序上下文中设置的ApplicationDomain

public function loadHandler(e:Event) : void {
   var loaderInfo:LoaderInfo = e.target as LoaderInfo;
   var BackButtonClass : Class = loaderInfo.applicationDomain.getDefinition( "com.game.button.BackButton" );
}

var loader : Loader = new Loader();
var context : LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
loader.load( new URLRequest("backbutton.swf"), context );