我一直在教程整齐avoider游戏在这个网站的 http://gamedev.michaeljameswilliams.com/2009/02/03/avoider-game-tutorial-5/ 。我得到尽可能部分5,最多到那时,我跟着code完全(我在游戏上运行的最后得分前停止),和我的事件禁用自动内核并用设备字体和嵌入式文本
除了当我运行游戏,不管多的敌人如何展现出来,我的分数不会从0变化。
显然,我不断收到1009错误,连接到onTick功能。我的结论是,它连接到gameScore.addToValue(5);线。但我不知道如何解决这个问题。谁能帮我?
下面是的code,我把在类问题的样本,如果任何人能发现的东西,我忘了补充。
- == - Shooter_II类: - == -
包
{
进口的flash.display.MovieClip;
进口flash.utils.Timer;
进口flash.events.TimerEvent;
进口flash.events.MouseEvent;
进口对象类型:flash.events.Event;
公共类SpaceShooter_II扩展影片剪辑//公共类扩展类影片剪辑。
{
公共变种军:阵列; //的敌人将是这一阵列的一部分。
公共变种gameScore:分数;
公共变种playerShip:PlayerShip; //这建立连接到PlayerShip AS的变量。
公共变种屏幕:GameScreen; //这个规定,在连接到该GameScreen作为一个变量。
公共变种gameTimer:定时器; //这建立称为gameTimer一个新的变量,连接到计时器效用。
//这个函数包含大量游戏的组件。
公共职能SpaceShooter_II()
{
//这将启动GameScreen。
屏幕=新GameScreen;
的addChild(屏幕);
//此设置了敌军。
军=新的Array(); //设置军为阵列的新实例。
VAR newEnemy =新敌人(100,-15); //这将创建一个新的敌人。有新的变种newEnemy声明,因此我们称之为无功。
army.push(newEnemy); //新的敌人加入军队。
的addChild(newEnemy); //新的敌方加入到游戏中。
//这将设置玩家的头像,一个飞船。
playerShip =新PlayerShip(); //这将调用PlayerShip的新实例...
的addChild(playerShip); //...And这增加了它的游戏。
playerShip.x = mouseX; //这两个变量放置在屏幕上的playerShip...
playerShip.y = mouseY的; //...at鼠标的位置。
//这将设置gameTimer,那里有很多的动作发生。
gameTimer =新的定时器(25);
gameTimer.addEventListener(TimerEvent.TIMER,onTick);
gameTimer.start();
}
//这个函数包含游戏(玩家的移动,敌人成群等)过程中所发生的事情
公共职能onTick(timerEvent:TimerEvent):无效
{
//这个if语句就是一个包含了敌舰的数组初始化。
如果(的Math.random()&所述; 0.05)//这设置于示出在一次向上的数目。
{
变种randomX:总数=的Math.random()* 800 //生成之间0安培的随机数; 1。
VAR newEnemy:敌人=新敌人(randomX,-15); //这表明在敌人开始时 - 在对X平面的随机位置,但是在在Y平面上的某些点。
army.push(newEnemy); //这增加了新的敌人来了军阵。
的addChild(newEnemy); //这使得游戏的新敌人的一部分。
//这片code被提供,我不知道如何解决一个1009错误消息。
gameScore.addToValue(5); //这一个敌人出现在屏幕上每一次增加了几个百分点。
}
//这个为语句将敌人向下在屏幕上。
每个(VAR的敌人:敌人的军队)//每一个敌人被添加到军阵时,它向下发送。
{
enemy.moveDown(); //这是发送敌人向下的部分。
//现在对于冲突的一部分 - 制定如果敌人击中玩家的飞船会发生什么的一部分:
如果(playerShip.hitTestObject(敌人))//如果playerShip使得与敌人接触...
{
gameTimer.stop(); //这将停止比赛。
则dispatchEvent(新PlayerEvent(PlayerEvent.BOOM)); //这会触发游戏结束画面中PlayerEvent AS
}
}
//这,顺便说一句,是玩家的运动控制:
playerShip.x = mouseX;
playerShip.y = mouseY的;
}
}
}
- == - MainCounter类: - == -
包
{
进口的flash.display.MovieClip;
公共类MainCounter扩展影片剪辑
{
公共变种到currentValue:数字;
公共变种addedValue:数字;
公共职能MainCounter()
{
resetValue(); //这会触发resetValue功能。
}
//这个运行在每一个滴答,或者每一个玩家做一些事情值得收入的时间点。
公共职能addToValue(addedValue:编号):无效
{
到currentValue =到currentValue + addedValue; //这需要电流值/得分,并通过添加额外的量,它更新了。
updateDisplay(); //这触发执行UpdateDisplay功能。
}
//这将重置时间和得分为初始值(零)。这是出征时将比分/定时器首先创建的,潜在的,如果球员抓住一定的权力,或击中敌人。
公共职能resetValue():无效
{
到currentValue = 0; //这个复位电流值/分/等。到零。
updateDisplay(); //这触发执行UpdateDisplay功能。
}
//此功能可显示当前得分/时间/不管,从而触发每次值发生变化。
公共职能updateDisplay():无效
{
}
}
}
- == - 评分等级: - == - `
包
{
进口API元素flash.text.TextField;
//将扩展这一类的一部分,使这个类从MainCounter类继承每个公共变量和函数。
公共类分数扩展MainCounter
{
公共变种scoreDisplay:文本字段;
公共功能评分()
{
超(); //超()允许一个类来访问类的,它的扩展的功能。
}
//这个功能给予覆盖,否则,我们就会有两个updateDisplay功能得益于扩展MainCounter。
重写公共职能updateDisplay():无效
{
super.updateDisplay(); //任何code这是在MainCounter的执行UpdateDisplay功能将运行也在这里。
scoreDisplay.text = currentValue.toString();
//有趣的事实:字母和数字序列称为串,因为它是个字符的字符串。典型的例子,
//这个分数的文本属性。现在,到currentValue被定义为一个数字,但所有数字有一个函数调用的toString()
//返回在一个字符串的形式的数目。
}
}
}
解决方案
您不能做 gameScore.addToValue(5)
如您尚未创建的一个实例类分数。
你在呼唤它,仿佛它是一个静态方法时,它不是。第一次尝试初始化分数的一个实例。
试试这个:
公共变种gameScore:得分=新的分数();
现在你应该可以调用 addtoValue
的功能。
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.
}
}
}
解决方案
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.