Search code examples
actionscriptadobe

ActionScript 3.0 and 'TypeError: Error#1034: type Coercion failed'


I'm currently following a tutorial, and I watched it like 6-7 times over, but for some reason I keep getting:

TypeError: Error#1034: type Coercion failed.

I'm trying to make a matching game for a school assignment, and I currently have this:

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;
        }
    }
}

The error pops up at line 38 and when I try debugging it tell me that clickObj is undefined. How can I fix this problem?

This is the entire error message:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::MovieClip@a3e4a61 to Logo.
at MatchingGame/openLogo()[H:\Informatica\Matching game\MatchingGame.as:39]


Solution

  • It looks like the cast from MovieClip to Logo isn't working. put a trace before that line to see what event.target is.

    Depending on the display list structure and event bubbling, you might be getting a different element that what you expect.

    Try var clickObj:Logo = Logo(e.currentTarget); as a quick test. Be sure to go through Trevor McCauley's article to get a better understanding of event bubbling.

    as3 event phase