错误#1009,ActionScript 3,Bullet为空

时间:2012-03-08 18:42:30

标签: actionscript-3

TypeError:错误#1009:无法访问空对象引用的属性或方法。 在Main_fla :: MainTimeline / BulletFire()[Main_fla.MainTimeline :: frame32:68]。正在发生,我不知道为什么......请帮助,我一直在排除故障,我完全迷失了。还要感谢,因为某些原因,当我发射子弹时,它仅在45度和225度处...谢谢你们

    //Create an array to hold multiple sprites
var mySpriteHolder:Array = [];
//Create a counter to keep track of the number of sprites
var lbCounter:int = 0;
//Maximum number of sprites on the canvas
var maxLB:int = 1;
//Keypress Code
stage.addEventListener(MouseEvent.CLICK, dropBullet);
//Function for the mouse event to fire bullet
function dropBullet(evt:MouseEvent):void{

     var bcos:Number = Math.cos((Turret.rotation) * Math.PI / 180);
     var bsin:Number = Math.sin((Turret.rotation) * Math.PI / 180);

     //starting x and y
     var startx:Number = Turret.x + (15 * bcos);
     var starty:Number = Turret.y + (15 * bsin);

    //calculates where the bullet needs to go by aiming in front of the gun
    var endx:Number = Turret.x + (50 * bcos);
    var endy:Number = Turret.y + (50 * bsin);

    var Bullet:MovieClip = new bullet();

    Bullet.x = startx;
    Bullet.y = starty;

    Bullet.xspeed = (endx - startx)/5;
    Bullet.yspeed =  (endx - startx)/5;

    mySpriteHolder.push(Bullet);
    stage.addChild(Bullet);

    //this calls the move down function
    stage.addEventListener(Event.ENTER_FRAME,BulletFire);
}
//Function to shoot bullet
function BulletFire(evt:Event):void{
    var Bullet:MovieClip;
    //Use a for loop to move the Bullets
    for(var i:int=0; i<=mySpriteHolder.length; i++){
        Bullet = mySpriteHolder[i];
    //Bounds Collision
    if(Bullet.hitTestObject(Up)){
        Bullet.yspeed*=-1;
    }
    if(Bullet.hitTestObject(Lower)){
        Bullet.yspeed*=-1;
    }
    if(Bullet.hitTestObject(Left)){
        Bullet.xspeed*=-1;
    }
    if(Bullet.hitTestObject(Right)){
        Bullet.xspeed*=-1;
    }
    //Blockade Collision
    for(var t in myBlockadeHolder){
            if(Bullet.hitTestObject(myBlockadeHolder[t])){
                trace("test");
            }
        }
    //Target Collision
        for(var c in mytargetHolder){
            if(Bullet.hitTestObject(mytargetHolder[c])){
            stage.removeChild(Bullet);
            mySpriteHolder.splice(i,1);
            lbCounter --;
            mytargetHolder[c].y = Math.random()*390 + 10;
            mytargetHolder[c].x = Math.random()*390 + 10;
                while(mytargetHolder[c].hitTestObject(Turret)){
                    mytargetHolder[c].y = Math.random()*390 + 10;
                    mytargetHolder[c].x = Math.random()*390 + 10;
                }
            }
            for(var a in mytargetHolder){
                for(var s in mytargetHolder){
                    while(mytargetHolder[a].hitTestObject(mytargetHolder[s])&& a!=s){
                        mytargetHolder[a].y = Math.random()*390 + 10;
                        mytargetHolder[a].x = Math.random()*390 + 10;
                    }
                }
                for(var g in myBlockadeHolder){
                    while(mytargetHolder[a].hitTestObject(myBlockadeHolder[g])&& a!=g){
                        mytargetHolder[a].y = Math.random()*390 + 10;
                        mytargetHolder[a].x = Math.random()*390 + 10;
                    }
                }
            }
        }
        Bullet.y += Bullet.yspeed;
        Bullet.x += Bullet.xspeed;
    }
}

2 个答案:

答案 0 :(得分:1)

首先:

for (var i:int = 0; i < mySpriteHolder.length; i++) {
...

即。将<=更改为<。如果i变为mySpriteHolder.length,那么您将获得越界异常。

下一步:

stage.removeChild(Bullet);
mySpriteHolder.splice(i--, 1);

即。从数组中删除当前元素时,还会减少索引i。它将在下一次迭代中再次增加,因此您将处于相同的索引。例如,如果删除第5个元素,则需要在下一次迭代中查看新的第5个元素(之前是第6个元素),而不是新的第6个元素(之前是第7个元素)。在您当前的代码中,您无意中跳过了一个 - 更不用说它本身可能会给您一个越界异常。

“越界”是指Bulletundefined

答案 1 :(得分:1)

mySpriteHolder.splice(i,1);

这条线可能会导致一些意想不到的结果(可能还有你的错误),因为你正在从你的循环逐渐增加的数组中拼接对象。重要的是要理解当您在AS3中拼接对象时,您实际上正在删除该元素,并且后面的所有索引都会向下移动拼接量。尝试在循环中向后工作:

 for(var i:int=mySpriteHolder.length-1; i>=0; i--){

此外,当您每次放置子弹时添加多个ENTER_FRAME事件时,此行应移出MouseClick事件。就像现在一样,每当你点击鼠标时,它会再做一次bulletFire()传递:

stage.addEventListener(Event.ENTER_FRAME,BulletFire);