Actionscript 3.0:TypeError:错误#1006:addToValue不是函数

时间:2013-07-18 22:51:52

标签: actionscript-3 typeerror

我确定隐藏着一些愚蠢的错误,但我已经在这里呆了2个多小时,似乎无法找到出错的地方。有人请帮我确定我做错了什么,错误在标题中,以下是我的相关代码。

进一步有用的细节:

确切错误如下:

TypeError:错误#1006:addToValue不是函数。     在SimpleMenuMain / onTick()     在flash.utils :: Timer / _timerDispatch()     在flash.utils :: Timer / tick()

游戏运行,但不计算得分,我希望得分计算。我已经三次检查我的所有实例名称都是正确的,我真的没有看到我在那里做了什么。可能我不知何故可能错误地引用它们,但代码中使用的所有名称都在我的符号和文本内的对象......等等。

非常感谢你的时间,这真的让我很伤心。

P.S .---我正在关注一个教程,所以如果我做了一些不必要的事情,不要怪我!虽然其他评论除了帮助我回答这个难题外,欢迎=)。

文件类

package 
{
    import flash.display.MovieClip;

    public class SMGDocClass extends MovieClip 
    {
        public var playScreen:SimpleMenuMain;
        public var titleScreen:TitleScreen;
        public var gameOver:GameOver;

        public function SMGDocClass() 
        {
            titleScreen = new TitleScreen();
            titleScreen.addEventListener(NavigationEvent.START,onRequestStart,false,0,true);
            titleScreen.x = 0;
            titleScreen.y = 0;
            addChild(titleScreen);

        }

        public function onStickman1Death(stickman1Event:Stickman1Event):void
        {
            var finalScore:Number = playScreen.getFinalScore();
            var finalClockTime:Number = playScreen.getFinalClockTime();

            gameOver = new GameOver();
            gameOver.addEventListener(NavigationEvent.RESTART,onRequestRestart,false,0,true);
            gameOver.addEventListener(NavigationEvent.MAINMENU,onRequestMainMenu,false,0,true);
            gameOver.x = 5;
            gameOver.y = 6;
            gameOver.setFinalScore( finalScore );
            gameOver.setFinalClockTime( finalClockTime );
            addChild(gameOver);

            playScreen = null;

        }

        public function onRequestStart( navigationEvent:NavigationEvent ):void
        {
        playScreen = new SimpleMenuMain();
        playScreen.addEventListener( Stickman1Event.DEAD, onStickman1Death,false,0,true );
        playScreen.x = 0;
        playScreen.y = 0;
        addChild( playScreen );

        titleScreen = null;
        }

        public function restartGame():void
        {
            playScreen = new SimpleMenuMain;
            playScreen.addEventListener(Stickman1Event.DEAD, onStickman1Death,false,0,true);
            playScreen.x = 0;
            playScreen.y = 0;
            addChild(playScreen);

            gameOver = null;
        }

        public function onRequestRestart(navigationEvent:NavigationEvent):void
        {
            restartGame();
        }

        public function onRequestMainMenu( navigationEvent:NavigationEvent):void
        {
            titleScreen = new TitleScreen();
            titleScreen.addEventListener(NavigationEvent.START,onRequestStart,false,0,true);
            titleScreen.x = 0;
            titleScreen.y = 0;
            addChild(titleScreen);

            removeChild(gameOver);
            gameOver = null;
        }

    }
}

playScreen CLASS

package  {

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

    public class SimpleMenuMain extends MovieClip {

        public var army1:Array;
        public var enemy1:Enemy1;
        public var gameTimer:Timer;
        public var stickman1:Stickman1;

        public function SimpleMenuMain() {

            army1 = new Array();
            var newEnemy1 = new Enemy1( 100, -15 );
            army1.push(newEnemy1);
            addChild(newEnemy1);

            stickman1 = new Stickman1();
            addChild(stickman1);
            stickman1.x = mouseX;
            stickman1.y = mouseY;

            gameTimer = new Timer(25);
            gameTimer.addEventListener(TimerEvent.TIMER, onTick, false, 0, true);
            gameTimer.start();

        }

        public function onTick(timerEvent:TimerEvent):void
        {
            gameClock.addToValue( 25 );
            if ( Math.random() < 0.1 )
            {
                var randomX:Number = Math.random() * 400;
                var newEnemy1:Enemy1 = new Enemy1( randomX, -15 );
                army1.push( newEnemy1 );
                addChild( newEnemy1 );
                gameScore.addToValue( 10 );
            }

            stickman1.x = mouseX;
            stickman1.y = mouseY;

            for each (var enemy1:Enemy1 in army1)
            {
                enemy1.moveDown();

                if (stickman1.hitTestObject(enemy1))
                {
                    gameTimer.stop();
                    dispatchEvent(new Stickman1Event(Stickman1Event.DEAD));
                }
            }

        }

        public function getFinalScore():Number
        {
            return gameScore.currentValue;
        }

        public function getFinalClockTime():Number
        {
            return gameClock.currentValue;
        }

    }

}

计数器CLASS

package
{
    import flash.display.MovieClip;
    public class Counter extends MovieClip
    {
        public var currentValue:Number;

        public function Counter()
        {
            reset();
        }

        public function addToValue( amountToAdd:Number ):void
        {
            currentValue = currentValue + amountToAdd;
            updateDisplay();
        }

        public function reset():void
        {
            currentValue = 0;
            updateDisplay();
        }

        public function updateDisplay():void
        {

        }
    }
}

Stickman1Event CLASS

package  
{
    import flash.events.Event;
    public class Stickman1Event extends Event 
    {
        public static const DEAD:String = "dead";

        public function Stickman1Event( type:String, bubbles:Boolean = false, cancelable:Boolean = false ) 
        { 
            super( type, bubbles, cancelable );

        } 

        public override function clone():Event 
        { 
            return new Stickman1Event( type, bubbles, cancelable );
        } 

        public override function toString():String 
        { 
            return formatToString( "Stickman1Event", "type", "bubbles", "cancelable", "eventPhase" ); 
        }
    }
}

NavigationEvent CLASS

package  
{
    import flash.events.Event;
    public class NavigationEvent extends Event 
    {
        public static const RESTART:String = "restart";
        public static const START:String = "start";
        public static const MAINMENU:String = "mainmenu";

        public function NavigationEvent( type:String, bubbles:Boolean = false, cancelable:Boolean = false ) 
        { 
            super( type, bubbles, cancelable );

        } 

        public override function clone():Event 
        { 
            return new NavigationEvent( type, bubbles, cancelable );
        }

        public override function toString():String 
        { 
            return formatToString( "NavigationEvent", "type", "bubbles", "cancelable", "eventPhase" ); 
        }
    }
}

分数等级

package
{
    import flash.text.TextField;
    public class Score extends Counter
    {
        public function Score()
        {
            super();
        }

        override public function updateDisplay():void
        {
            super.updateDisplay();
            scoreDisplay.text = currentValue.toString();
        }

    }
}

1 个答案:

答案 0 :(得分:1)

正如Josh已经指出的那样,不能保证gameScore和gameClock对象实际上是Counter的实例。

我们需要查看实例化它的代码/位置。

一些可能的选择;

  • gameScore和gameClock不是实际的Counter实例
  • 您将计数器实例化为MovieClip,因此您已将其添加到舞台上,但您可能没有正确地将Library资产和Counter类连接在一起。

您可以将Counter构造函数更改为此并运行它;

public function Counter()
{
    trace("i am a counter");
    reset();
}

...您应该看到两次跟踪输出(对于gameScore和gameClock),如果您没有看到跟踪输出,则不会构建Counter,这意味着gameScore和gameClock只是普通的MovieClip实例。