好的,在这段代码中:
package GameFiles.SourceCode.Classes
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class User extends MovieClip
{
private var numStars:int = 65;
public static var enemyList:Array = new Array();
private var ourShip:Ship;
private var scoreHUD:ScoreHUD;
public function User() : void
{
var ourShip:Ship = new Ship(stage);
stage.addChild(ourShip);
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
ourShip.addEventListener("hit", shipHit, false, 0, true);
scoreHUD = new ScoreHUD(stage);
stage.addChild(scoreHUD);
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), stage.getChildIndex(ourShip));
}
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e:Event) : void
{
if (Math.floor(Math.random() * 90) <= scoreHUD.s_score / 100)
{
var enemy:Astroid = new Astroid(stage, ourShip);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemyList.push(enemy);
enemy.addEventListener("Passed", enemyPassed, false, 0, true);
stage.addChild(enemy);
}
}
public function shipHit(e:Event) : void
{
scoreHUD.updateHits(e.currentTarget.points);
ourShip.updateHitCount(1);
}
public function enemyPassed(e:Event) : void
{
scoreHUD.updateScore(10);
}
private function removeEnemy(e:Event)
{
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
}
}
}
该变量ourShip
被定义为对象船(我可以对大部分代码进行跟踪),但是当我到达该shipHit
函数并尝试引用ourShip
以调整我的船类中的变量时,它说它为空并且我得到一个错误。我不明白为什么它变成了 null,而我认为代码的特定部分是更新变量的最佳位置。我已经在更新一个类似的变量scoreHUD
。