ActionScript 3.0和'TypeError:错误#1034:类型强制失败'

问题描述 投票:1回答:1

我目前正在关注一个教程,我看了6-7次,但出于某种原因我不断得到:

TypeError:错误#1034:类型强制失败。

我正在尝试为学校作业制作匹配游戏,我现在有这个:

package {

    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.Event;
    import flash.events.MouseEvent;


    public class MatchingGame extends MovieClip {

        var fClip:Logo
        var sClip:Logo
        var myTimer:Timer
        var frames:Array = new Array(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10);

        public function MatchingGame() {
            // Constructor code

            for(var i:Number=1; i<=5; i++) {
                for(var j:Number=1; j<=4; j++) {
                    var myLogo:Logo = new Logo();
                    var index = Math.floor(Math.random() * frames.length)

                    myLogo.frameNo = frames[index];
                    frames.splice(index, 1);

                    addChild(myLogo);
                    myLogo.x = j*100;
                    myLogo.y = i*100;

                    myLogo.gotoAndStop(11);
                    myLogo.addEventListener(MouseEvent.CLICK, openLogo);
                }
            }
        }

        private function openLogo(e:MouseEvent) {
            var clickObj:Logo = Logo(e.target);

            if(fClip == null) {
                    fClip = clickObj;
                    fClip.gotoAndStop(fClip.frameNo);
            }
            else if(sClip == null && fClip != clickObj) {

                sClip = clickObj;
                sClip.gotoAndStop(sClip.frameNo);

                if(fClip.frameNo == sClip.frameNo) {
                    myTimer = new Timer(1000, 1);
                    myTimer.start();
                    myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, removeLogos);
                }
                else {
                    myTimer = new Timer(1000, 1);
                    myTimer.start();
                    myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, resetLogos);
                }
            }
        }

        private function removeLogos(e:TimerEvent) {
            removeChild(fClip);
            removeChild(sClip);
            myTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, removeLogos);
            fClip = null;
            sClip = null;
        }

        private function resetLogos(e:TimerEvent) {
            fClip.gotoAndStop(11);
            sClip.gotoAndStop(11);
            myTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, resetLogos);
            fClip = null;
            sClip = null;
        }
    }
}

第38行弹出错误,当我尝试调试它时告诉我clickObj未定义。我该如何解决这个问题?

这是整个错误消息:

TypeError:错误#1034:类型强制失败:无法将flash.display :: MovieClip @ a3e4a61转换为徽标。 在MatchingGame / openLogo()[H:\ Informatica \ Matching game \ MatchingGame.as:39]

actionscript adobe
1个回答
1
投票

MovieClipLogo的演员看起来不起作用。在该行之前留下痕迹,看看event.target是什么。

根据display list structure and event bubbling,您可能会获得与您期望的不同的元素。

尝试var clickObj:Logo = Logo(e.currentTarget);作为快速测试。请务必阅读Trevor McCauley的文章,以便更好地了解事件冒泡。

as3 event phase

© www.soinside.com 2019 - 2024. All rights reserved.