2014-02-25 24 views
0

我一直在为这个网站上的整洁avoider游戏的教程工作:http://gamedev.michaeljameswilliams.com/2009/02/03/avoider-game-tutorial-5/。至于第5部分,直到那时,我完全遵循了代码(在运行游戏中的最后一个分数之前,我停下了脚步),并且事件禁用了自动内核和使用的设备字体,并嵌入了文本。Avoider游戏教程:得分不起作用

除了当我运行游戏,不管多少敌人怎么露面,我的分数不从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. 
      } 

     } 

} 

回答

0

由于您尚未创建班级分数的实例,因此不能执行gameScore.addToValue(5)。 如果它不是静态方法,则称它为它。首先尝试初始化Score的一个实例。

试试这个:

public var gameScore:Score = new Score(); 

现在你应该可以调用addtoValue功能。

+0

好吧,好消息:我再也没有那个错误了。坏消息是:当我玩这个游戏时,分数依然为零。每当敌人展示自己时,不要给予我点数,而不会发生任何事情。有什么建议么? – user3321704

+0

因为addToValue函数不起作用。 – user3321704

+0

您不需要在Score类中声明currentValue,因为它是从Main继承的。这可能是为什么你的分数是0,它显示的是本地currentValue而不是主类的值。希望这是有道理的。 – Chris