我是第一次遇到问题的海报。我的队友和我正在做一个游戏制作项目。它是一个简单的捕捉游戏,只有在失败之后才有效:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at catcher_fla::MainTimeline/gameLoop()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
我不是我们团队中的脚本编写者,但是当我查找它时我看到了这一点,据我所知,我们在gameLoop中有一个问题,使用timerDispatch()函数和tick ()函数。
currentSceneName = "GamePlay";
character.gotoAndPlay("idle")
import flash.events.KeyboardEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
//VAR
var speed:Number = 2;
var score:Number = 0;
var life:int = 3;
updateLifeBar();
var isWalkingAnimation:Boolean=false;
var isRight:Boolean = false;
var isLeft:Boolean = false;
var isUp:Boolean = false;
var isDown:Boolean = false;
var isZ:Boolean = false;
var isC:Boolean = false;
var gameState:String = "Off";
playButton.addEventListener(MouseEvent.CLICK, clickPlay);
function clickPlay(event:MouseEvent):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);
stage.addEventListener(KeyboardEvent.KEY_UP, liftKey);
playButton.removeEventListener(MouseEvent.CLICK, clickPlay);
removeChild(playButton);
gameState = "On";
spawnTimer.start();
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);
//PRESSKEY
function pressKey(event:KeyboardEvent):void {
if (isWalkingAnimation == false){
isWalkingAnimation = true;
character.gotoAndPlay("walk")
}
if(event.keyCode == Keyboard.RIGHT){
isRight = true;
}
if(event.keyCode == Keyboard.LEFT){
isLeft = true;
}
if(event.keyCode == Keyboard.UP){
isUp = true;
}
if(event.keyCode == Keyboard.DOWN){
isDown = true;
}
if(event.keyCode == Keyboard.Z){
isZ = true;
}
if(event.keyCode == Keyboard.C){
isC = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, liftKey);
//LIFTKEY
function liftKey(event:KeyboardEvent):void {
if (isWalkingAnimation == true){
isWalkingAnimation = false;
character.gotoAndPlay("idle")
}
if(event.keyCode == Keyboard.RIGHT){
isRight = false;
}
if(event.keyCode == Keyboard.LEFT){
isLeft = false;
}
if(event.keyCode == Keyboard.UP){
isUp = false;
}
if(event.keyCode == Keyboard.DOWN){
isDown = false;
}
if(event.keyCode == Keyboard.Z){
isZ = false;
}
if(event.keyCode == Keyboard.C){
isC = false;
}
}
var joshsTimer:Timer = new Timer(16);
joshsTimer.addEventListener(TimerEvent.TIMER, gameLoop);
joshsTimer.start();
//GAMELOOP
function gameLoop(event:TimerEvent):void {
if(isLeft == true && character.x > (0 + character.width * .55)){
character.scaleX = -.80;
character.x -= speed;
}
if(isRight == true && character.x < (stage.stageWidth - character.width * .58)){
character.scaleX = .80;
character.x += speed;
}
if(isUp == true && character.y > (0 + character.height * 2)){
character.y -= speed;
}
if(isDown == true && character.y < (stage.stageHeight - character.height * .25)){
character.y += speed;
}
if(isZ == true && character.x >(0 + character.width * .55)){
character.scaleX = -.80;
character.x -= speed;
}
if(isC == true && character.x < (stage.stageWidth - character.width * .58)){
character.scaleX = .80;
character.x += speed;
}
///////////////Collides with Players Basket//////////////////
for each(var joshBanana:Banana in bananaArray){
joshBanana.y = joshBanana.y + (2.5);
if(joshBanana.hitTestObject(character.activePoint)){
score += 100;
var bananaToBeRemoved:Banana = joshBanana;
bananaArray.splice(joshBanana, 1);
removeChild(bananaToBeRemoved);
doGoodThings();
}
}
for each(var joshCandy:Candy in candyArray){
joshCandy.y = joshCandy.y + (2.5);
if(joshCandy.hitTestObject(character.activePoint)){
var candyToBeRemoved:Candy = joshCandy;
candyArray.splice(joshCandy, 1);
removeChild(candyToBeRemoved);
doBadThings();
}
}
for each(var joshApple:Apple in appleArray){
joshApple.y = joshApple.y + (2.5);
if(joshApple.hitTestObject(character.activePoint)){
score += 100;
var appleToBeRemoved:Apple = joshApple;
appleArray.splice(joshApple, 1);
removeChild(appleToBeRemoved);
doGoodThings();
}
}
for each(var joshChocolate:Chocolate in chocolateArray){
joshChocolate.y = joshChocolate.y + (2.5);
if(joshChocolate.hitTestObject(character.activePoint)){
var chocolateToBeRemoved:Chocolate = joshChocolate;
chocolateArray.splice(joshChocolate, 1);
removeChild(chocolateToBeRemoved);
doBadThings();
}
}
////////////////////////If falls below Stage/////////////////////
for(var i:uint = 0; i < bananaArray.length;i++){
if(bananaArray[i].y > stage.stageHeight){
var fallBananaToBeRemoved:DisplayObject = bananaArray[i];
bananaArray.splice(i, 1);
removeChild(fallBananaToBeRemoved);
}
}
for(i = 0; i < candyArray.length;i++){
if(candyArray[i].y > stage.stageHeight){
var fallCandyToBeRemoved:DisplayObject = candyArray[i];
candyArray.splice(i, 1);
removeChild(fallCandyToBeRemoved);
}
}
for(i = 0; i < appleArray.length;i++){
if(appleArray[i].y > stage.stageHeight){
var fallAppleToBeRemoved:DisplayObject = appleArray[i];
appleArray.splice(i, 1);
removeChild(fallAppleToBeRemoved);
}
}
for(i = 0; i < chocolateArray.length;i++){
if(chocolateArray[i].y > stage.stageHeight){
var fallChocolateToBeRemoved:DisplayObject = chocolateArray[i];
chocolateArray.splice(i, 1);
removeChild(fallChocolateToBeRemoved);
}
}
}
var bananaArray:Array = new Array();
var candyArray:Array = new Array();
var appleArray:Array = new Array();
var chocolateArray:Array = new Array();
var spawnTimer:Timer = new Timer(1000);
spawnTimer.addEventListener(TimerEvent.TIMER, spawnBanana);
spawnTimer.stop();
var numOfFruitToSpawn:Number = 1;
function spawnBanana(event:TimerEvent):void {
for(var i:uint = 0; i < numOfFruitToSpawn; i++){
var randomFruit:Number = randomNumber(1,4);
if(randomFruit == 1){
var newBanana:Banana = new Banana();
addChild(newBanana);
bananaArray.push(newBanana);
newBanana.x = randomNumber(0, stage.stageWidth - newBanana.width)
newBanana.y = 0 - newBanana.height;
}
else if(randomFruit == 2){
var newCandy:Candy = new Candy();
addChild(newCandy);
candyArray.push(newCandy);
newCandy.x = randomNumber(0, stage.stageWidth - newCandy.width)
newCandy.y = 0 - newCandy.height;
}
else if(randomFruit == 3){
var newChocolate:Chocolate = new Chocolate();
addChild(newChocolate);
chocolateArray.push(newChocolate);
newChocolate.x = randomNumber(0, stage.stageWidth - newChocolate.width)
newChocolate.y = 0 - newChocolate.height;
}
else if(randomFruit == 4){
var newApple:Apple = new Apple();
addChild(newApple);
appleArray.push(newApple);
newApple.x = randomNumber(0, stage.stageWidth - newApple.width)
newApple.y = 0 - newApple.height;
}
}
}
function doGoodThings():void {
trace("GOT SOME GOOD!");
joshScore.text = "SCORE: " + String(score);
}
function doBadThings():void {
life--;
trace("Bad JuJu");
updateLifeBar();
if(life == 0){
endGame();
gotoAndStop("1", "MainMenu");
}
}
function endGame():void {
stage.removeEventListener(KeyboardEvent.KEY_DOWN, pressKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, liftKey);
//playButton.removeEventListener(MouseEvent.CLICK, clickPlay);
//Altered line 307 to playButton.removeEventLitener from addEventListener
//addChild(playButton); readded this line
gameState = "Off";
spawnTimer.stop();
}
function updateLifeBar():void {
if(life > 0){
lifeBar.gotoAndStop(life);
} else if(life <= 0){
lifeBar.gotoAndStop(4);
}
}
function randomNumber(min:Number, max:Number):Number {
return Math.floor(Math.random() * (1 + max - min) + min);
}