Question

I have made a game on Flash CS6 using AS3. The game has a spaceship on the right hand of the screen and it shoots bullets at aliens that randomly appear on the right. The game is all working perfectly but every now and then when I play it i get this error.

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at alien3/pulse()[/Users/Matt/Documents/DES B Assignment/Prototype/alien3.as:43]

Now here is the AS linkage for alien 3

package {

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


public class alien3 extends MovieClip {

    var yMove:Number = 15;
    var changeDirectionAfterYMoves:Number = 0;
    var moveCount:Number = 0;

    var shootCount:Number = 0;
    var shootMissilesAfterYMoves:Number = 0;

    public function alien3() {
        addEventListener(Event.ENTER_FRAME,pulse);
    }

    public function stopListening() 
    {
        removeEventListener(Event.ENTER_FRAME,pulse);
    }

    public function pulse(evt:Event)
    {
        if (currentFrame!=1) return;
        if(changeDirectionAfterYMoves == moveCount)
        {
            yMove=yMove*-1;
            var maxMoves:Number;
            if (yMove>0)maxMoves = Math.round (400-this.y)/yMove;
            else maxMoves = Math.round((this.y)/Math.abs(yMove));
            changeDirectionAfterYMoves = 1 + Math.round (Math.random () *maxMoves);
            moveCount = 0;  
        }

        if (shootCount==shootMissilesAfterYMoves)
        {
            var ao:alienMissile3 = new alienMissile3 ();
            ao.x = this.x + 50;
            ao.y = this.y;
            parent.addChild(ao);
            shootCount=0;
            shootMissilesAfterYMoves = 1 + Math.round (Math.random() * 25)
        }

        this.y+=yMove;
        moveCount = moveCount+1;
        shootCount = shootCount+1;

        }

    }
}

So line 43 responds to the alien missile movie clip in the library parent.addChild(ao);

Which is strange because it adds the alien missiles without a problem and they work fine The alienMissile3 is a movie clip which was converted to a movie clip from a png which is named something different.

I have no idea what is causing this error.

The code in the main timeline is as follows.

    import flash.display.MovieClip;
import flash.events.Event;
import flash.media.SoundChannel;
import flash.net.dns.AAAARecord;


var frameCount:Number = 0;
var alienCount:Number = 0;
var alienInterval:Number = 100;
var score:Number=0;
var gameOn:Boolean = false
var my_sound:laserGun = new laserGun();
var my_channel:SoundChannel = new SoundChannel();
var my_sound3:QueenAmidalaandTheNabooPalace = new QueenAmidalaandTheNabooPalace();
var my_channel3:SoundChannel = new SoundChannel();
var spaceshipMovement:Number = 0;


mcGameOverScreen.visible=false;

var scoresArray:Array = new Array();

var SO:SharedObject = SharedObject.getLocal("scores5");

if (SO.size!=0)
{
    scoresArray = SO.data.scoresArray;
}

function startGame()
{
    addEventListener(Event.ENTER_FRAME, pulse);
    gameOn=true;
    mySpaceship.livesLeft=3;
    score=0;
}


function pulse(event:Event):void
{   

    if (mySpaceship.livesLeft<1) noLivesLeft();
    addNewAlienIfNecessary();
    /*addNewAlien2IfNecessary();
    addNewAlien3IfNecessary();*/

    checkForMissileOnAlien();
    checkForMissileOnAlien2();
    checkForMissileOnAlien3();

    checkForMissileOnAlienMissile();
    checkForMissileOnAlienMissile2();
    checkForMissileOnAlienMissile3();

    tidyUp();

    checkForAlienMissileOnSpaceship();
    checkForAlienMissile2OnSpaceship();
    checkForAlienMissile3OnSpaceship();

    tbScore.text = String(score);
    tbLivesLeft.text = String (mySpaceship.livesLeft);

    var bg:background = new background();
    bg.y = 0;
    bg.x = 0;
    addChild(bg);
    gameOn=true;

}

function addNewAlienIfNecessary()
{
    if (score<=100 && alienCount<3 && frameCount%60==0)
    {
        var a:alien = new alien();
        a.x = Math.random()*380;
        a.y = Math.random()*50;
        addChild(a);
        alienCount=alienCount+1;

    }
    //frameCount++;


    if (score >100 && score<500 && alienCount<3 && frameCount%80==0)
    {
        var aa:alien2 = new alien2();
        aa.x = Math.random()*380;
        aa.y = Math.random()*50;
        addChild(aa);
        alienCount=alienCount+1;

    }
    //frameCount++;

    if (alienCount<3 && score>=500 && frameCount%100==0)
    {
        var ab:alien3 = new alien3();
        ab.x = Math.random()*380;
        ab.y = Math.random()*50;
        addChild(ab);
        alienCount=alienCount+1;
    }
    frameCount++;

}

/*function addNewAlien2IfNecessary()
{
    if (score>100 && score<500 && alienCount<3 && frameCount%60==0)
    {
        var aa:alien2 = new alien2();
        aa.x = Math.random()*440;
        aa.y = Math.random()*50;
        addChild(aa);
        alienCount=alienCount+1;
    }
    frameCount++;
}

function addNewAlien3IfNecessary()
{
    if (score>500 && alienCount<3 && frameCount%60==0)
    {
        var ab:alien3 = new alien3();
        ab.x = Math.random()*440;
        ab.y = Math.random()*50;
        addChild(ab);
        alienCount=alienCount+1;
    }
    frameCount++;
}*/


function checkForMissileOnAlien()
{
    var missilez:Array = new Array;
    var alienz:Array = new Array;

    for (var i=0;i<numChildren;i++)
    {
        if (getChildAt(i) is alien) {alienz.push(getChildAt(i) as MovieClip)};
        if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip)};
    }

    for (var j=0;j<alienz.length;j++)
    {
        for (var k=0;k<missilez.length;k++)
            {
                if (alienz[j].hitTestObject(missilez[k]))
                    {
                        alienz[j].gotoAndStop (2);
                        //alienz[j].stopListening();
                        missilez[k].gotoAndStop(2);
                        missilez[k].stopListening();

                        var e:explosion = new explosion();
                        e.x = alienz[j].x;
                        e.y = alienz[j].y;
                        addChild(e);

                        alienCount = alienCount-1;
                        score += 20;
                    }
            }
    }

}

function checkForMissileOnAlien2()
{
    var missilez:Array = new Array;
    var alienz2:Array = new Array;

    for (var i=0;i<numChildren;i++)
    {
        if (getChildAt(i) is alien2) {alienz2.push(getChildAt(i) as MovieClip)};
        if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip)};
    }

    for (var j=0;j<alienz2.length;j++)
    {
        for (var k=0;k<missilez.length;k++)
            {
                if (alienz2[j].hitTestObject(missilez[k]))
                    {
                        alienz2[j].gotoAndStop (2);
                        //alienz2[j].stopListening();
                        missilez[k].gotoAndStop(2);
                        missilez[k].stopListening();

                        var e:explosion = new explosion();
                        e.x = alienz2[j].x;
                        e.y = alienz2[j].y;
                        addChild(e);

                        alienCount = alienCount-1;
                        score += 50;
                    }
            }
    }

}

function checkForMissileOnAlien3()
{
    var missilez:Array = new Array;
    var alienz3:Array = new Array;

    for (var i=0;i<numChildren;i++)
    {
        if (getChildAt(i) is alien3) {alienz3.push(getChildAt(i) as MovieClip)};
        if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip)};
    }

    for (var j=0;j<alienz3.length;j++)
    {
        for (var k=0;k<missilez.length;k++)
            {
                if (alienz3[j].hitTestObject(missilez[k]))
                    {
                        alienz3[j].gotoAndStop (2);
                        //alienz3[j].stopListening();
                        missilez[k].gotoAndStop(2);
                        missilez[k].stopListening();

                        var e:explosion = new explosion();
                        e.x = alienz3[j].x;
                        e.y = alienz3[j].y;
                        addChild(e);

                        alienCount = alienCount-1;
                        score += 100;
                    }
            }
    }

}

function checkForMissileOnAlienMissile()
{
    var alienMissilez:Array = new Array();
    var missilez:Array = new Array();

    for (var i=0;i<numChildren;i++)
    {
        if (getChildAt(i) is alienMissile) {alienMissilez.push(getChildAt(i) as MovieClip);}
        if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip);}
    }

    trace("alienMissilez" + alienMissilez.length);
    trace("missilez" + missilez.length);

    for (var j=0;j<alienMissilez.length;j++)
        {
            for (var k=0;k<missilez.length;k++)
            {
                if (alienMissilez[j].hitTestObject(missilez[k]))
                    {
                        alienMissilez[j].gotoAndPlay(2);
                        alienMissilez[j].stopListening();

                        missilez[k].gotoAndPlay(2);
                        missilez[k].stopListening();

                        var e:explosion = new explosion();
                        e.x = alienMissilez[j].x;
                        e.y = alienMissilez[j].y;
                        addChild(e);
                        score += 10;

                    }
            }
        }

}

function checkForMissileOnAlienMissile2()
{
    var alienMissilez2:Array = new Array();
    var missilez:Array = new Array();

    for (var i=0;i<numChildren;i++)
    {
        if (getChildAt(i) is alienMissile2) {alienMissilez2.push(getChildAt(i) as MovieClip);}
        if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip);}
    }

    trace("alienMissilez2" + alienMissilez2.length);
    trace("missilez" + missilez.length);

    for (var j=0;j<alienMissilez2.length;j++)
        {
            for (var k=0;k<missilez.length;k++)
            {
                if (alienMissilez2[j].hitTestObject(missilez[k]))
                    {
                        alienMissilez2[j].gotoAndPlay(2);
                        alienMissilez2[j].stopListening();

                        missilez[k].gotoAndPlay(2);
                        missilez[k].stopListening();

                        var e:explosion = new explosion();
                        e.x = alienMissilez2[j].x;
                        e.y = alienMissilez2[j].y;
                        addChild(e);
                        score += 15;

                    }
            }
        }

}

function checkForMissileOnAlienMissile3()
{
    var alienMissilez3:Array = new Array();
    var missilez:Array = new Array();

    for (var i=0;i<numChildren;i++)
    {
        if (getChildAt(i) is alienMissile3) {alienMissilez3.push(getChildAt(i) as MovieClip);}
        if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip);}
    }

    trace("alienMissilez3" + alienMissilez3.length);
    trace("missilez" + missilez.length);

    for (var j=0;j<alienMissilez3.length;j++)
        {
            for (var k=0;k<missilez.length;k++)
            {
                if (alienMissilez3[j].hitTestObject(missilez[k]))
                    {
                        alienMissilez3[j].gotoAndPlay(2);
                        alienMissilez3[j].stopListening();

                        missilez[k].gotoAndPlay(2);
                        missilez[k].stopListening();

                        var e:explosion = new explosion();
                        e.x = alienMissilez3[j].x;
                        e.y = alienMissilez3[j].y;
                        addChild(e);
                        score += 20;

                    }
            }
        }

}



/*function tidyUp()
{
    for (var j=numChildren-1;j>=0;j--)
    {
        if (getChildAt(j) is TextField) continue; //This added to help text box work
        var mc:MovieClip = getChildAt(j) as MovieClip;
        if (getChildAt(j) is explosion) 
        {
            if (mc.currentFrame==15) removeChildAt(j);
            continue;
        }

        //if (mc.x>800 ||  mc.x<0 || mc.currentFrame!=1  ) removeChildAt(j);
    }
}
*/

function tidyUp()
{
    for (var j=numChildren-1;j>=0;j--)
    {
        if (getChildAt(j) is TextField) continue;
        if (getChildAt(j) is SimpleButton) continue;
        var mc:MovieClip = getChildAt(j) as MovieClip;

        if (getChildAt(j) is explosion) 
        {
            if (mc.currentFrame==10) removeChildAt(j);
            //continue;
        }

        if (getChildAt(j) is alienMissile)
        {
            if (mc.x>800||(mc.currentFrame==10)) removeChildAt(j);
            //continue;
        }

        if (getChildAt(j) is alienMissile2)
        {
            if (mc.x>800||(mc.currentFrame==10)) removeChildAt(j);
            //continue;
        }

        if (getChildAt(j) is alienMissile3)
        {
            if (mc.x>800||(mc.currentFrame==10)) removeChildAt(j);
            //continue;
        }

        if(getChildAt(j) is missile)
        {
            if(mc.x<0||(mc.currentFrame==2)) removeChildAt(j);
            //continue;
        }

        if(getChildAt(j) is alien)
        {
            if(mc.currentFrame!=1) removeChildAt(j);
            //continue;
        }

        if(getChildAt(j) is alien2)
        {
            if(mc.currentFrame!=1) removeChildAt(j);
            //continue;
        }

        if(getChildAt(j) is alien3)
        {
            if(mc.currentFrame!=1) removeChildAt(j);
            //continue;
        }

    }
}

function checkForAlienMissileOnSpaceship()
{
    for (var i=0;i<numChildren;i++)
    {
        if (getChildAt(i) is alienMissile)
        {
            if (getChildAt(i).hitTestObject(mySpaceship))
            {
                var mc:MovieClip = getChildAt(i) as MovieClip;
                if (mc.currentFrame==1) 
                {
                    mc.gotoAndPlay(2);
                    mc.stopListening();
                    var e:explosion = new explosion();
                    e.x = mc.x;
                    e.y = mc.y;
                    addChild(e);
                    mySpaceship.isHit();
                }
            }
        }
    }
}   

function checkForAlienMissile2OnSpaceship()
{
    for (var i=0;i<numChildren;i++)
    {
        if (getChildAt(i) is alienMissile2)
        {
            if (getChildAt(i).hitTestObject(mySpaceship))
            {
                var mc:MovieClip = getChildAt(i) as MovieClip;
                if (mc.currentFrame==1) 
                {
                    mc.gotoAndPlay(2);
                    mc.stopListening();
                    var e:explosion = new explosion();
                    e.x = mc.x;
                    e.y = mc.y;
                    addChild(e);
                    mySpaceship.isHit();
                }
            }
        }
    }
}   

function checkForAlienMissile3OnSpaceship()
{
    for (var i=0;i<numChildren;i++)
    {
        if (getChildAt(i) is alienMissile3)
        {
            if (getChildAt(i).hitTestObject(mySpaceship))
            {
                var mc:MovieClip = getChildAt(i) as MovieClip;
                if (mc.currentFrame==1) 
                {
                    mc.gotoAndPlay(2);
                    mc.stopListening();
                    var e:explosion = new explosion();
                    e.x = mc.x;
                    e.y = mc.y;
                    addChild(e);
                    mySpaceship.isHit();
                }
            }
        }
    }
}   

stage.addEventListener (KeyboardEvent.KEY_DOWN, spaceshipControls);

function spaceshipControls (event:KeyboardEvent):void
    {
        if (event.keyCode==38 && mySpaceship.y>+62 && stage.frameRate==24) {mySpaceship.y-=20;}
        if (event.keyCode==40 && mySpaceship.y<480-mySpaceship.height && stage.frameRate==24) {mySpaceship.y+=20;}

        if (event.keyCode==32 && gameOn==true && stage.frameRate==24) 
        {
            var m:missile = new missile();
            m.y = mySpaceship.y;
            m.x = mySpaceship.x - 80;
            addChild(m);
            my_channel = my_sound.play();
        }

    }

btn_up.addEventListener(MouseEvent.MOUSE_DOWN, goUp);
btn_up.addEventListener(MouseEvent.MOUSE_UP, stopMoving);

function goUp(evt:MouseEvent)
{
    if (mySpaceship.y>+62 && stage.frameRate==24) 
        {
            mySpaceship.y-=20;
        } 
    else 
        {
            spaceshipMovement=0;
        }

}

function stopMoving (evt:MouseEvent)
{                    
    spaceshipMovement=0;
}


btn_down.addEventListener(MouseEvent.MOUSE_DOWN, goDown);
btn_down.addEventListener(MouseEvent.MOUSE_UP, stopMoving2);

function goDown(evt:MouseEvent)
{
    if (mySpaceship.y<480-mySpaceship.height && stage.frameRate==24) 
    {
    mySpaceship.y+=20;
    } 
    else 
    {
    spaceshipMovement=0;
    }

}

function stopMoving2 (evt:MouseEvent)
{                    
    spaceshipMovement=0;     
}

btn_fire.addEventListener(MouseEvent.MOUSE_DOWN, fire);

function fire(evt:MouseEvent)
{
    var m:missile = new missile();
    m.y = mySpaceship.y;
    m.x = mySpaceship.x - 80;
    addChild(m);
    my_channel = my_sound.play();
}

function noLivesLeft()
{
    stage.removeEventListener(KeyboardEvent.KEY_DOWN, spaceshipControls);
    btn_fire.removeEventListener(MouseEvent.MOUSE_DOWN, fire);
    flash.media.SoundMixer.stopAll();
    my_channel3 = my_sound3.play();
    removeEventListener(Event.ENTER_FRAME,pulse);
    for (var i=0;i<numChildren;i++)
    {

        //trace(i);
        if (getChildAt(i) is TextField) continue;
        if (getChildAt(i) is SimpleButton) continue;
        var mc:MovieClip = getChildAt(i) as MovieClip;
        if (mc.hasEventListener(Event.ENTER_FRAME)) mc.stopListening();
    }
    gameOn = false;
    mcGameOverScreen.visible = true;
    mcGameOverScreen.tb_score.text =  String (score);
    stage.removeEventListener(KeyboardEvent.KEY_DOWN, spaceshipControls);
    btn_fire.removeEventListener(MouseEvent.MOUSE_DOWN, fire);

    for (var c=numChildren-1;c>=0;c--)
    {
        var d = getChildAt(c);
        if (d is alien || d is alienMissile|| d is explosion || d is missile || d is  alien2 || d is alien3 ||d is alienMissile2 ||d is alienMissile3 || d is background)    removeChildAt(c);
    }


     }


    btnResume.addEventListener(MouseEvent.MOUSE_DOWN, resumeGame);

    function resumeGame(e:MouseEvent):void
    {
     stage.frameRate = 24
     btnPause.visible=true;
     btnResume.visible=false;
     }

     btnPause.addEventListener(MouseEvent.MOUSE_DOWN, pauseGame);

     function pauseGame(e:MouseEvent):void
     {
     stage.frameRate = 0
     btnPause.visible=false;
     btnResume.visible=true;
     }


    function setMute(vol)
    {
    var sTransform:SoundTransform = new SoundTransform(1,0);
    sTransform.volume = vol;
    SoundMixer.soundTransform = sTransform;
    }
    var isMuted:Boolean = false;
     muteBtn.addEventListener(MouseEvent.CLICK,toggleMuteBtn);
     function toggleMuteBtn(event:Event){
      if(isMuted)
     {
          isMuted = false;
          setMute(1);
     }
     else
     {
          isMuted = true;
          setMute(0);
     }
     }
Was it helpful?

Solution

You are getting the error because you are checking for Alien3's parent before it has been added to the display list.
Add a trace before the parent.addChild(ao) line, ie:

trace("Alien3 parent = "+parent);

I'm betting the output will be null sometimes.
The solution is to replace the ENTER_FRAME event listener (in the Alien3 constructor function) with an ADDED_TO_STAGE event listener. Try this:

public function alien3() {
    addEventListener(Event.ADDED_TO_STAGE, onStage);
}

private function onStage(e:Event):void {
    removeEventListener(Event.ADDED_TO_STAGE, onStage);
    addEventListener(Event.ENTER_FRAME, pulse); 
}

Now, when pulse is called, 'parent' will not be null because the instance is on the display list and has access to 'parent'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top