如何使用变量创建类的实例?

时间:2012-02-21 18:13:19

标签: actionscript-3 class variables methods

我正在尝试将变量传递给我的一个类中的方法,以便我可以用它来创建正确的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);
                 }
         }
    }

当我从maintimeline调用方法时,它看起来像这样:

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.

1 个答案:

答案 0 :(得分:2)

如果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);
}