问题
I've been working on the tutorial for a neat avoider game over at this site: http://gamedev.michaeljameswilliams.com/2009/02/03/avoider-game-tutorial-5/ . I got as far as part 5, and up till then, I followed the code exactly (I stopped before running the final score at game over), and I event disabled auto-kernel and used device fonts, and embedded the text.
Except when I run the game, no matter how many enemies show up, my score doesn't change from 0.
Apparently, I keep getting the 1009 error, connected to the "onTick" function. I've concluded that it's connected to the "gameScore.addToValue( 5 );" line. But I don't know how to fix that. Can anyone help me?
Here's a sample of the code I've put in the classes in question, if anyone can spot something that I forgot to add.
--==-- Shooter_II class: --==--
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 class: --==--
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 class: --==--`
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:
You can't do gameScore.addToValue( 5 )
as you have not created an instance of the Class Score.
You are calling it as if it is a static method when it is not. First try initializing an instance of Score.
Try this:
public var gameScore:Score = new Score();
now you should be able to call the addtoValue
function.
来源:https://stackoverflow.com/questions/22001841/avoider-game-tutorial-score-not-working