AS3:班级运作不正常

时间:2015-03-09 09:57:10

标签: actionscript-3 flash

对于一个模糊的标题感到抱歉,但这可能是我描述这个问题的最佳方式,这对我来说是荒谬的。

我需要一个MC完成的简单动作:进入某个帧。虽然我在同一个类代码中使用另一个Movie Clip完成了相同类型的操作,但我无法使其工作。以下是我的表现方式:

if (currentItem.type == "blue") {
    guy.gotoAndPlay("blue")
}

是的,I&C引用的课程(' guy')被扩展为电影片段。同样,完全相同的代码适用于其他剪辑。我尝试了另一种方法:从Clip的类中切换帧,它切换到的帧由一个由主类改变的变量定义。但不知何故,这也无济于事。它给了我1069错误。这是''的代码。类:

package 
{
    import flash.display.MovieClip;
    import flash.events.Event;

    public class guy extends MovieClip 
    {

        public static var gotoer:String = "fffuuu"

        public function shaman_armsUp() 
        {
            super();
            addEventListener(Event.ADDED_TO_STAGE, init)
        }

        public function init(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, init)
            armLoop()
        }

        public function armLoop():void {
            if (gotoer == "brown") {
                this.gotoAndPlay("brown")
            }
            if (gotoer == "red") {
                trace(gotoer)
                this.gotoAndPlay("red")
            }
        }

    }

}

有没有人对此有合理的解释?这可能是由一个错误造成的吗?

1 个答案:

答案 0 :(得分:0)

也许您在方法shaman_armsUp()中编写的代码应该移动到构造函数中?

按照我所编辑的班级版本(也改名为Guy,遵循班级名称惯例)

package
{
     import flash.display.FrameLabel;
     import flash.display.MovieClip;
     import flash.events.Event;

     // renaming the class name (guy to Guy)
     public class Guy extends MovieClip
     {
         // not sure if using an static variable for this case is the best idea, but I decided to keep because I don't know your initial idea
         public static var gotoer:String = "fffuuu";

         public function Guy()
         {
             // move the addedToStage event to the constructor
             super();
             addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
         }

         public function shaman_armsUp()
         {
         }

         public function init(e:Event):void
         {
              removeEventListener(Event.ADDED_TO_STAGE, init);
              armLoop();
         }

         public function armLoop():void
         {
              // Note: the value of gotoer is "fffuuu" and there is no conditional for it.
             // are you chaging this value before calling armLoop? because when the init method is called it has the same value

             // why not try something like:

             // checking if the frame exist and if so, calling the gotoAndPlay method (try to imagine if you have 100 frame names? the old approach will be very hard to maintain

             if (hasFrameLabel(gotoer))
             {
                  this.gotoAndPlay(gotoer);
             }
             else
             {
                  trace('frame: ', gotoer, ' not found');
             }

            /*
            if (gotoer == "brown")
            {
               this.gotoAndPlay("brown");
            }
            if (gotoer == "red")
            {
               trace(gotoer);
               this.gotoAndPlay("red");
            }
            */
         }

         // helper function to avoid calling a frame that doesn't exist
         private function hasFrameLabel(frameLabel:String):Boolean
         {
              var returnValue:Boolean;

              const obj:Object = this.currentLabels;

              for each (var i:FrameLabel in obj)
              {
                  if (i.name == frameLabel)
                  {
                      returnValue = true;
                      break;
                  }
               }

               return returnValue;
         }
     }
}