String 回避者游戏教程:分数不起作用
我一直在为这个站点上的一个整洁的回避游戏编写教程:。我一直到第5部分,直到那时,我完全遵循代码(在游戏结束时运行最终分数之前我停止了),我禁用了自动内核,使用了设备字体,并嵌入了文本 除非当我运行游戏时,无论有多少敌人出现,我的分数都不会从0变为0 显然,我一直收到1009错误,连接到“onTick”函数。我已经得出结论,它与“gameScore.addToValue(5);”行有关。但我不知道如何解决这个问题。有人能帮我吗 如果有人能发现我忘记添加的东西,这里有一个我放在相关类中的代码示例 --==-- 射击二级: --==-- --==-- 分数等级: --==--`String 回避者游戏教程:分数不起作用,string,actionscript-3,flash-cs5,super,String,Actionscript 3,Flash Cs5,Super,我一直在为这个站点上的一个整洁的回避游戏编写教程:。我一直到第5部分,直到那时,我完全遵循代码(在游戏结束时运行最终分数之前我停止了),我禁用了自动内核,使用了设备字体,并嵌入了文本 除非当我运行游戏时,无论有多少敌人出现,我的分数都不会从0变为0 显然,我一直收到1009错误,连接到“onTick”函数。我已经得出结论,它与“gameScore.addToValue(5);”行有关。但我不知道如何解决这个问题。有人能帮我吗 如果有人能发现我忘记添加的东西,这里有一个我放在相关类中的代码示例 -
您不能执行
gameScore.addToValue(5)
,因为您尚未创建类Score的实例。
当它不是静态方法时,调用它就好像它是静态方法一样。首先尝试初始化Score的实例
试试这个:
public var gameScore:Score = new Score();
现在您应该可以调用
addtoValue
函数了。好消息:我再也不会收到那个错误了。坏消息是:当我玩游戏时,分数仍然保持在零。敌人一露面就给我分,什么事也没发生。有什么建议吗?您不需要在分数类中声明currentValue,因为它是从Main继承的。这可能就是您的分数为0的原因,它显示的是本地currentValue,而不是主类的值。希望这是有意义的。我修改了score类(如果你查看我的示例,我会相应地编辑你看到的内容),但它仍然不起作用。你在哪里创建scoreDisplay文本字段的实例并将其添加到本示例中的显示中,我看不到它?你是什么意思?在“分数”课上。我应该在显示器的什么位置添加它?我的意思是,我将“scoreDisplay”指定给我的主记分牌符号的实例名称。然后,当我把这个符号放在游戏屏幕上时,我给了这个符号的实例一个名字“gameScore”。我应该做些不同的事情吗?
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.
}
}
}
public var gameScore:Score = new Score();