【问题标题】:How do I create a instance of a class using a variable?如何使用变量创建类的实例?
【发布时间】:2012-02-21 18:13:19
【问题描述】:

我正在尝试将变量传递给我的一个类中的方法,以便我可以使用它来创建正确的movieClip(图像)

我的班级代码如下所示:

package  {
    import flash.display.MovieClip;
    import flash.display.Sprite;


    public class SlideShow extends MovieClip{

        public function SlideShow() 
            {

                             //does something on start

        }

          //This function should take the string and use it as the class name below.
          public function addImages(BackGround:String):void
             {
            trace(BackGround);

            var main_bg:BackGround = new BackGround();
            addChild(main_bg);
                 }
         }
    }

当我从我的主时间线调用该方法时,它看起来像这样:

var shoeSlide:SlideShow = new SlideShow(); 
shoeSlide.addImages("menPant"); 

SO "menPant" 实际上是我分配给其中包含一些图像的影片剪辑类的名称。

我收到以下错误:

SlideShow.as, Line 30   1046: Type was not found or was not a compile-time constant: BackGround.

【问题讨论】:

    标签: actionscript-3 class variables methods


    【解决方案1】:

    如果 flash 没有自动为您执行此操作,请确保您在课程代码的顶部导入 getDefinitionByName。这应该可以。

    public function addImages(BackGround:String):void
    {
        var symbol_class:Class = getDefinitionByName(BackGround);
        //EDIT: removed data type :BackGround -- this will give an error.
        var main_bg = new symbol_class();
        addChild(main_bg);
    }
    

    【讨论】:

    • 太棒了,这就是我想要的! +1 选中
    猜你喜欢
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多