2014-02-06 64 views
0

我正在研究一个大学项目,在AS3中创建一个自上而下的射击游戏,我对AS3知之甚少,但仍然必须生产完成的游戏。本场比赛的规定和运行,但是当我拍的敌人我得到这个错误AS3 - Flash Pro CS6 - 无法访问空对象引用的属性或方法

bullet hit baddie 2 < ---这是一种微量元素
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Enemy/removeSelf()
at Bullet/loop()
at Main/loop()

任何帮助的非常感谢,非常感谢。我会在下面发布我的代码,以便可以引用它。


Main.as

package 
{ 
import flash.display.Stage; 
import flash.display.MovieClip; 
import flash.events.Event; 
import flash.events.MouseEvent; 

public class Main extends MovieClip 
{ 
    public var player:Player; 
    public var enemy:Enemy; 

    public var bulletList:Array = []; 

    public var mousePressed:Boolean = false; //keeps track of whether the mouse is currently pressed down 
    public var delayCounter:int = 0; //this adds delay between the shots 
    public var delayMax:int = 7; //change this number to shoot more or less rapidly 

    public var enemies:Array = []; 

    public function Main():void 
    { 
     player = new Player(stage, 320, 240); 
     stage.addChild(player); 

     //stage.addEventListener(MouseEvent.CLICK, shootBullet, false, 0, true); //remove this 
     stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, false, 0, true); 
     stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, 0, true); 

     stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true); 
     for(var numBaddies=0; numBaddies<6;numBaddies++){ 
      var enemy:Enemy = new Enemy(/*null*/); 
      enemy.x = numBaddies*50; 
      enemy.y = numBaddies*50 
      stage.addChild(enemy); 
      enemies.push(enemy); 
     } 
    } 

    public function loop(e:Event):void 
    { 
     if(mousePressed) // as long as the mouse is pressed... 
     { 
      delayCounter++; //increase the delayCounter by 1 
      if(delayCounter == delayMax) //if it reaches the max... 
      { 
       shootBullet(); //shoot a bullet 
       delayCounter = 0; //reset the delay counter so there is a pause between bullets 

      } 
     } 

     if(bulletList.length > 0) 
     { 
      for(var i:int = bulletList.length-1; i >= 0; i--) 
      { 
       bulletList[i].loop(); 
      } 
     } 

     /*for(var h = 0; h<bulletList.length; ++h) 
     { 
      if(bulletList[h].hitTestObject(this)){ 
       trace("player hit by baddie " + h); 
       } 
     }*/ 

     for(var u:int=0; u<enemies.length; u++) { 
     Enemy(enemies[u]).moveTowards(player.x, player.y); 
     } 
    } 


    public function mouseDownHandler(e:MouseEvent):void //add this function 
    { 
     mousePressed = true; //set mousePressed to true 
    } 

    public function mouseUpHandler(e:MouseEvent):void //add this function 
    { 
     mousePressed = false; //reset this to false 
    } 

    public function shootBullet():void //delete the "e:MouseEvent" parameter 
    { 
     var bullet:Bullet = new Bullet(stage, player.x, player.y, player.rotation, enemies); 
     bullet.addEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved, false, 0, true); 
     bulletList.push(bullet); 
     stage.addChild(bullet); 
    } 

    public function bulletRemoved(e:Event):void 
    { 
     e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved); 
     bulletList.splice(bulletList.indexOf(e.currentTarget),1); 
    } 
} 
} 



Enemy.as

package { 

import flash.display.Stage; 
import flash.display.MovieClip; 
import flash.events.Event; 

public class Enemy extends MovieClip { 

    public var bullets:Array; 

    public var stageRef:Stage; 

    private var enemyPositionX, enemyPositionY, xDistance, yDistance, myRotation:int; 
    public function Enemy(/*stageRef:Stage, bulletList:Array*/) { 
     // constructor code 

     /*bullets = bulletList; 
     this.stageRef = stageRef;*/ 
    } 

    public function moveTowards(playerX:int, playerY:int){ 
     xDistance = this.x - playerX; 
     yDistance = this.y - playerY; 

     myRotation = Math.atan2(yDistance, xDistance); 

     this.x -= 3 * Math.cos(myRotation); 
     this.y -= 3 * Math.sin(myRotation); 


    } 

    public function removeSelf():void 
    { 
     //removeEventListener(Event.ENTER_FRAME, loop); 
     if (stageRef.contains(this)) 
     stageRef.removeChild(this); 
    } 
} 

} 



Bullet.as

package 
{ 
import flash.display.Stage; 
import flash.display.MovieClip; 
import flash.events.Event; 

public class Bullet extends MovieClip 
{ 
    private var stageRef:Stage; //checks if the bullet leaves the screen borders 
    private var speed:Number = 10; //speed that the bullet will travel at 
    private var xVel:Number = 0; //current x velocity 
    private var yVel:Number = 0; //current y velocity 
    private var rotationInRadians = 0; //convenient to store our rotation in radians instead of degrees 

    public var allBaddies:Array; 

    //our constructor requires: the stage, the position of the bullet, and the direction the bullet should be facing 
    public function Bullet(stageRef:Stage, X:int, Y:int, rotationInDegrees:Number, enemies:Array):void 
    { 
     this.stageRef = stageRef; 
     this.x = X; 
     this.y = Y; 
     this.rotation = rotationInDegrees; 
     this.rotationInRadians = rotationInDegrees * Math.PI/180; //convert degrees to radians, for trigonometry 
     allBaddies = enemies; 
    } 

    public function loop():void //we don't need to include the "e:Event" because we aren't using an EventListener 
    { 
     for(var b=0; b<allBaddies.length; ++b) 
     { 
      if(allBaddies[b].hitTestObject(this)){ 
       trace("bullet hit baddie " + b); 
       removeSelf(); 
       allBaddies[b].removeSelf(); 
       } 
     } 

     xVel = Math.cos(rotationInRadians) * speed; //uses the cosine to get the xVel from the speed and rotation 
     yVel = Math.sin(rotationInRadians) * speed; //uses the sine to get the yVel 

     x += xVel; //updates the position 
     y += yVel; 

     //if the bullet goes off the edge of the screen... 
     if(x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0) 
     { 
      this.parent.removeChild(this); //remove the bullet 
     } 
    } 

    private function removeSelf():void 
    { 
     removeEventListener(Event.ENTER_FRAME, loop); 
     if (stageRef.contains(this)) 
     stageRef.removeChild(this); 
    } 
} 
} 

回答

1

stageRefEnemy类变量是永远不会设置。 您正在将它传递给Bullet类构造函数,但您在Enemy类中做的不是这样。

+0

啊,我真是无聊,谢谢。我已经通过了StageRef,现在敌方的剪辑在与子弹movieclip碰撞时视觉上被移除,但是它们仍然存在,因为子弹在它们碰撞时被移除,并且他们按照他们应该做的那样跟随玩家,但是当他们应该做时被从舞台上删除...这里的任何想法? – cm125192

+0

你能否让最后的评论更清楚一点? –

+0

++以上评论。不知道什么是实际上被删除,什么不是。 – 3vilguy

相关问题