Avoider游戏教程:分数不起作用

时间:2014-02-25 00:02:44

标签: string actionscript-3 flash-cs5 super

我一直在为这个网站制作一个简洁的避免游戏教程:http://gamedev.michaeljameswilliams.com/2009/02/03/avoider-game-tutorial-5/。我得到了第5部分,直到那时,我完全遵循了代码(我在游戏结束时运行最终得分之前停止了),并且我禁用了自动内核并使用了设备字体,并嵌入了文本。

除非我运行游戏,否则无论出现多少敌人,我的分数都不会从0变化。

显然,我一直收到1009错误,连接到“onTick”功能。我已经得出结论,它与“gameScore.addToValue(5);”相关联。线。但我不知道如何解决这个问题。任何人都可以帮助我吗?

以下是我在相关课程中添加的代码示例,如果有人能发现我忘记添加的内容。

- == - Shooter_II类: - == -

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

  public class SpaceShooter_II extends MovieClip //The public class extends the class to a movie clip.
  { 
    public var army:Array; //the Enemies will be part of this array.
    public var gameScore:Score;
    public var playerShip:PlayerShip; //This establishes a variable connected to the PlayerShip AS.
    public var onScreen:GameScreen; //This establishes a variable that's connected to the GameScreen AS.
    public var gameTimer:Timer; //This establishes a new variable known as gameTimer, connected to the timer utility.

    //This function contains the bulk of the game's components.
    public function SpaceShooter_II() 
    {
      //This initiates the GameScreen.
      onScreen = new GameScreen;
      addChild ( onScreen );

      //This sets up the enemy army.
      army = new Array(); //sets the "army" as a NEW instance of array.
      var newEnemy = new Enemy( 100, -15); //This will create new enemies. There's new var newEnemy statement, hence we call THIS a var.
      army.push ( newEnemy ); //the new enemy is added to the army.
      addChild( newEnemy ); //the new enemy is added to the game.

      //This sets up the player's avatar, a spaceship.
      playerShip = new PlayerShip(); //This invokes a new instance of the PlayerShip... 
      addChild( playerShip ); //...And this adds it to the game.
      playerShip.x = mouseX; //These two variables place the "playerShip" on-screen...
      playerShip.y = mouseY; //...at the position of the mouse.


      //This sets up the gameTimer, where a lot of the action takes place.
      gameTimer = new Timer( 25 );
      gameTimer.addEventListener( TimerEvent.TIMER, onTick );
      gameTimer.start();
    }

      //This function contains the things that happen during the game (player movement, enemy swarms, etc.)
    public function onTick( timerEvent:TimerEvent ):void
    {
      //This "if" statement is where the array that contains the enemy ships is initialized.
      if ( Math.random() < 0.05 ) //This sets the number of ships showing up at once.
      {
        var randomX:Number = Math.random() * 800 //Generates a random number between 0 & 1.
        var newEnemy:Enemy = new Enemy ( randomX, -15 ); //This shows where the enemy starts out--at a random position on the X plane, but at a certain points on the Y plane.
        army.push( newEnemy ); //This adds the new enemy to the "army" Array.
    addChild( newEnemy ); //This makes the new enemy part of the game.

    //This piece of code is providing a 1009 error message that I don't know how to fix.
    gameScore.addToValue( 5 );//This adds a few points every time an enemy appears on-screen.
      }

      //This "for" statement sends the enemies downward on the screen.
      for each (var enemy:Enemy in army) //Every time an enemy is added to the "army" array, it's sent downward.
      {
        enemy.moveDown(); //This is the part that sends the enemy downward.

        //And now for the collision part--the part that establishes what happens if the enemy hits the player's spaceship:
        if ( playerShip.hitTestObject ( enemy ) ) //If the playerShip makes contact with the enemy...
        {
          gameTimer.stop(); //This stops the game.
          dispatchEvent( new PlayerEvent(PlayerEvent.BOOM) ); //This triggers the game over screen in the PlayerEvent AS

        }
      }

      //This, incidentally, is the player's movement controls:
      playerShip.x = mouseX;
      playerShip.y = mouseY;
    }

  } 
}

- == - MainCounter类: - == -

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

    public function MainCounter()
      {
        resetValue(); //This triggers the resetValue function.
      }

      //This runs on every "tick," or every time that the player does something worthy of earning points.
      public function addToValue( addedValue:Number ):void
      {
        currentValue = currentValue + addedValue; //This takes the current value/score and updates it by adding an extra amount to it.
        updateDisplay(); //This triggers the updateDisplay function.
      }

      //This resets the time and score to the original value (zero). This is set off when the score/timer is created in the first place, and potentially if the player grabs a certain power-up or hits an enemy.
      public function resetValue():void
      {
        currentValue = 0; //this resets the current value/score/etc. to zero.
        updateDisplay(); //This triggers the updateDisplay function.
      }

      //This function shows the current score/time/whatever, and thus triggers every time the value changes.
      public function updateDisplay():void
      {

      }
  }
}

- == - 分数类: - == - `

package  
{
    import flash.text.TextField;

    //The "extends" part of this class allows this class to "inherit" every public variable and function from the "MainCounter" class.
    public class Score extends MainCounter 
    {   
        public var scoreDisplay:TextField;

        public function Score() 
        {
            super(); //"super()" allows a class to access the functions of the class that it's "extending" to.
        }

            //This function is given an "override" because otherwise, we'd have two "updateDisplay" functions thanks to the "extends MainCounter."
            override public function updateDisplay():void
            {
                super.updateDisplay(); //Any code that's in the updateDisplay function of MainCounter will run here, too.
            scoreDisplay.text = currentValue.toString();
            //Fun fact: any sequence of letters and numbers is called a "string" because it's a string of characters. Case in point,
            //the text properties of this Score. Now, currentValue is defined as a Number, but all Numbers have a function called toString()
            //that returns the number in the form of a string.
            }

        }

}

1 个答案:

答案 0 :(得分:0)

由于您尚未创建班级分数实例,因此无法执行gameScore.addToValue( 5 )。 你正在调用它,就好像它是一个静态方法,当它不是。首先尝试初始化Score的实例。

试试这个:

public var gameScore:Score = new Score();

现在您应该能够调用addtoValue函数。