0

我确定有一些愚蠢的错误对我隐藏,但我已经做了两个多小时,似乎无法找到问题所在。有人请帮我确定我做错了什么,错误在标题中,以下是我的相关代码。

更多有用的细节:

确切的错误如下:

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

游戏运行,但不计分,我要计分。我已经三次检查了我所有的实例名称是否正确,我真的看不出我在那里所做的有什么问题。可能我以某种方式错误地引用了它们,但是代码中使用的所有名称都在我的符号和文本内的对象上......等等。

非常感谢您的时间,这真的让我很生气。

PS---我正在学习教程,所以如果我做了一些不必要的事情,请不要怪我!尽管除了帮助我回答这个难题之外的其他评论也是受欢迎的=)。

文档类

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

    }
}

播放屏幕类

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

    }

}

柜台类

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

导航事件类

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

    }
}
4

1 回答 1

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 实例。

于 2013-07-19T00:07:07.003 回答