为什么我在这里收到错误1118?

时间:2014-04-02 07:45:40

标签: actionscript-3 compiler-errors

在下面的代码中,我有几个都是TheBeetle()的MovieClip。它们位于另一个名为gamelevel的MovieClip中,并且还被推入一个名为bArray的数组中。之前我已经在gamelevel中索引了它们但是在调用事件监听器之后我不能再将它们编入索引并收到错误" 1118:使用静态类型Object将值隐式强制转换为可能不相关的类型flash.display:DisplayObject。 &#34 ;.当用户点击它们时,它们会死亡(并改变框架)并且尸体会进入其他活着的身体,这就是我需要在它们死亡时将它们索引为1的原因。我理解错误说的是什么,但我怎么能做我需要做的事情? 代码工作正常,但它不会在我提到的两行中提到,所以请看一下:

public function clicked (event:MouseEvent)
    {
        if (event.target is TheBeetle && event.target.currentFrame <= 2)
        {
            var mc:Object = event.target

            // TheBeetle is actually a MovieClip but i cannot write: var mc:MovieClip = event.target, if i do i receive 1118


                if (mc.currentFrame == 1)
                {
                    mc.gotoAndStop (Math.floor(Math.random() * 3 + 4));
                }
                else
                {
                    mc.gotoAndStop (3);
                }
                mc.filters = null;

                // Here i need to index the TheBeetle as i did before like gamelevel.setChildIndex(mc,1) but i'd receive 1118!

                bArray.splice (bArray.indexOf(mc),1);

                if (bArray.length == 0)
                {
                    removeEventListener (Event.ENTER_FRAME,frameHandler);
                    waveTimer.removeEventListener (TimerEvent.TIMER_COMPLETE, changeLocation);
                }
        }
    }

2 个答案:

答案 0 :(得分:2)

您需要将目标明确地转换为MovieClip类:

var mc:MovieClip = MovieClip(event.target);

您可能需要在检查目标的currentFrame的行之前执行此操作,因为'Object'没有currentFrame方法。

答案 1 :(得分:1)

我建议您在处理事件和目标时使用软铸造。通过软铸造,如果你抓住了错误的目标,你将不会遇到问题 - 施法过程只返回null

public function clicked (e:MouseEvent){
    var beetle: TheBeetle = e.target as TheBeetle;
    if(beetle != null && beetle.currentFrame <= 2){
        //Work with beetle as you want
    }
}
相关问题