Actionscript 3 AS3:类运行不正常

Actionscript 3 AS3:类运行不正常,actionscript-3,flash,Actionscript 3,Flash,很抱歉标题模糊,但这可能是我能描述这个问题的最好方式,我现在觉得这个问题很荒谬 我需要MC做一个简单的动作:转到某个帧。我无法让它工作,虽然我有一个完全相同的动作类型由另一个电影剪辑在同一类代码完成。我是这样做的: if (currentItem.type == "blue") { guy.gotoAndPlay("blue") } 是的,我指的“盖伊”这个类被扩展为电影剪辑。同样,完全相同的代码也适用于其他剪辑。我尝试了另一种方法:从剪辑的类中切换帧,它切换到的帧由一个由主类更改的变

很抱歉标题模糊,但这可能是我能描述这个问题的最好方式,我现在觉得这个问题很荒谬

我需要MC做一个简单的动作:转到某个帧。我无法让它工作,虽然我有一个完全相同的动作类型由另一个电影剪辑在同一类代码完成。我是这样做的:

if (currentItem.type == "blue") {
    guy.gotoAndPlay("blue")
}
是的,我指的“盖伊”这个类被扩展为电影剪辑。同样,完全相同的代码也适用于其他剪辑。我尝试了另一种方法:从剪辑的类中切换帧,它切换到的帧由一个由主类更改的变量定义。但不知何故,这也不起作用。它给了我1069错误。以下是“guy”类的代码:

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")
            }
        }

    }

}

有人对此有合理的解释吗?这可能是由bug引起的吗?

也许您在方法shaman\u armsUp中编写的代码应该移动到构造函数中

遵循我编辑的类guy版本,重命名为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;
         }
     }
}

请告诉我们哪一行抛出错误,以及实际的错误消息是什么。如果不启动联机搜索,我不知道错误1069意味着什么。ReferenceError:error 1056:无法在guy上创建属性gotoer。在Main/mainLoop的Main/throwing函数中,我尝试将变量同时声明为“publicstativar”和“publicsvar”,但都不起作用。另外,我在谷歌上搜索到一个建议,AS3设置中的“自动声明阶段实例”复选框可能未选中,但确实没有选中。谢谢你的建议,但它没有解决这个问题。问题是,由于某种原因,我无法从主类中处理“gotoer”值。它不接受直接gotoAndPlay命令,也不接受值更改。我觉得我没有说那个家伙是我做的电影唇什么的。没有整个范围很难测试。如果尝试在armLoop方法中添加字符串参数?通过框架标签?好的,在混乱了一段时间后,我解决了这个问题。很抱歉,我在AS3方面的经验还不够丰富,无法清楚地说出问题出在哪里,但我怀疑这是关于从其他地方调用goto函数的问题。谢谢你的建议,gPeart。