首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >避免游戏教程:得分不起作用

避免游戏教程:得分不起作用
EN

Stack Overflow用户
提问于 2014-02-25 08:02:44
回答 1查看 130关注 0票数 0

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

除了当我运行游戏时,无论有多少敌人出现,我的分数都不会从0开始变化。

显然,我一直收到1009错误,连接到"onTick“函数。我的结论是它连接到"gameScore.addToValue( 5 );“行。但我不知道怎么解决这个问题。有谁可以帮我?

这是我放在有问题的类中的代码示例,如果有人能发现我忘记添加的内容的话。

--==-- Shooter_II类:--==--

代码语言:javascript
复制
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类:--==--

代码语言:javascript
复制
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
      {

      }
  }
}

--==-- Score类:--==--`

代码语言:javascript
复制
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.
            }

        }

}
EN

回答 1

Stack Overflow用户

发布于 2014-02-25 08:29:55

您不能执行gameScore.addToValue( 5 ),因为您尚未创建Class Score的实例。当它不是静态方法时,您可以像调用静态方法一样调用它。首先尝试初始化Score的实例。

试试这个:

代码语言:javascript
复制
public var gameScore:Score = new Score();

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22001841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档