Skip to main content
Participant
April 22, 2012
Answered

Problem removing child at end game screen

  • April 22, 2012
  • 1 reply
  • 1462 views

Hi everyone

I am creating an android app for my university corusework and it is based on shooting alien spaceships as they fly in from the right side of the screen. When my gun is shot enough times the game should go to the end screen, the problem is I need to remove the alien bullets as the game ends. However I keep getting an error of an undefined property. Here is the code for my main timeline:

import flash.events.MouseEvent;

import flash.events.Event;

import flash.display.MovieClip;

//gun control

var upButtonIsDown:Boolean;

var downButtonIsDown:Boolean;

var livesLost:Number = 0;

stop();

btn_up.addEventListener(MouseEvent.MOUSE_DOWN,upDown);

btn_down.addEventListener(MouseEvent.MOUSE_DOWN,downDown);

btn_up.addEventListener(MouseEvent.MOUSE_UP,upUp);

btn_down.addEventListener(MouseEvent.MOUSE_UP,downUp);

//bg

var bgButtonIsDown:Boolean;

bg.addEventListener(MouseEvent.MOUSE_UP,upUp);

bg.addEventListener (MouseEvent.MOUSE_UP,downUp);

stage.addEventListener(Event.ENTER_FRAME,pulse);

function pulse(evt:Event)

{

          if (currentFrame!=2) return;

          if (upButtonIsDown) mc_gun.y-=5;

          if (downButtonIsDown) mc_gun.y+=5;

          checkforHits();

}

function checkforHits()

{

          for (var i:Number=0;i<numChildren;i++)

{

                               if (getChildAt is alienbullet)

                               {

                                         var ab:alienbullet = alienbullet (getChildAt (i));

                                         if (mc_gun.hitTestObject(ab) && ab.currentFrame==1)

                                         {

                                                   mc_gun.gotoAndStop(mc_gun.currentFrame+1);

                                                   ab.gotoAndStop(2);

                                                   livesLost++;

                                                   if (livesLost>3) endGame();

                                                   return;

                                         }

                               }

                     }

}

function endGame()

{

          for (var i:Number=numChildren-1;i>=0;i--)

          {

                    if (getChildAt(i) is alienbullet)

                    {

                              var ab:alienbullet = alienbullet(getChildAt(i));

                              ab.killMe();

                    }

          }

          gotoAndStop(21);

}

function upDown (evt:MouseEvent)

{

          upButtonIsDown=true;

}

function downDown (evt:MouseEvent)

{

          downButtonIsDown=true;

}

function upUp (evt:MouseEvent)

{

          upButtonIsDown=false;

}

function downUp (evt:MouseEvent)

{

          downButtonIsDown=false;

}

//shoot code

mc_gun.addEventListener(MouseEvent.MOUSE_DOWN,shoot);

stage,addEventListener(Event.ENTER_FRAME,alienhit);

var bulletArray:Array = new Array();

bulletArray.push(mc_bullet1);

bulletArray.push(mc_bullet2);

bulletArray.push(mc_bullet3);

bulletArray.push(mc_bullet4);

bulletArray.push(mc_bullet5);

bulletArray.push(mc_bullet6);

var bulletCounter:Number = 0;

function alienhit(evt:Event)

{

          for (var i:Number=0;i<bulletArray.length;i++)

                     {

                              if (mc_alienA1.hitTestObject(bulletArray)) trace("alienA1 has been hit" +i);

                              if (mc_alienA2.hitTestObject(bulletArray)) trace("alienA2 has been hit" +i);

                              if (mc_alienA3.hitTestObject(bulletArray)) trace("alienA3 has been hit" +i);

                     }

}

function shoot(evt:MouseEvent)

{

          if (bulletCounter<5) bulletCounter++;

          else bulletCounter=0;

          shootmc(bulletArray[bulletCounter]);

}

function shootmc(mc:MovieClip)

{

          mc.visible=true;

          mc.x = mc_gun.x;

          mc.y = mc_gun.y;

          mc.gotoAndPlay(2);

}

Now the endGame function is supposed to call on this code which is in the package file (script for aliens)

public function killMe()

                              {

                                        this.removeEventListener(Event.ENTER_FRAME,pulse);

                                        parent.removeChild(this);

                              }

full script:

package

{

          import flash.display.MovieClip;

          import flash.events.Event;

          public class alien1 extends MovieClip

          {

                    var yMin = 0;

                    var yMax = stage.stageHeight;

                    var yInc = 10;

                    var maxPulses= Math.round(yMax/yInc);

                    var changeDirectionAt = Math.round(Math.random() * maxPulses);

                    var pulseCount = 0;

                    var fireCount=0;

                    var fireAt = Math.round(Math.random() * maxPulses);

                    public function alien1()

                    {

                              this.addEventListener(Event.ENTER_FRAME,apulse);

                    }

                    function apulse(evt:Event)

                    {

                              pulseCount++;

                              fireCount++;

                              if (pulseCount>=changeDirectionAt)

                              {

                                        yInc *=  -1;

                                        pulseCount = 0;

                                        changeDirectionAt = Math.round(Math.random() * maxPulses);

                                        return;

                              }

                              if (fireCount>=fireAt)

                              {

                                        var bullet:alienbullet = new alienbullet();

                                        bullet.x = this.x;

                                        bullet.y = this.y;

                                        parent.addChild(bullet);

                                        fireCount=0;

                                        fireAt = Math.round(Math.random() * maxPulses);

                              }

                              this.y +=  yInc;

                              if (this.y <= yMin)

                              {

                                        yInc *=  -1;

                              }

                              if (this.y >= yMax)

                              {

                                        yInc *=  -1;

                              }

                              public function killMe()

                              {

                                        this.removeEventListener(Event.ENTER_FRAME,pulse);

                                        parent.removeChild(this);

                              }

                    }

          }

}

Would really appreciate any help! Thanks

This topic has been closed for replies.
Correct answer kglad

you're not removing the alien bullets in killMe().  you're removing the alien1 instance.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 22, 2012

you're not removing the alien bullets in killMe().  you're removing the alien1 instance.

Participant
April 22, 2012

you're right, I dont know how missed that tbh!

but now that I have moved that public function to the alienbullet class, it doesnt come up with any errors, but now when the game is supposed to end, the adobe air debug launcher crashes!

edit: it now shows up with this error

TypeError: Error #1009: Cannot access a property or method of a null object reference.

          at cw_fla::MainTimeline/alienhit()[cw_fla.MainTimeline::frame2:103]

kglad
Community Expert
Community Expert
April 22, 2012

what's frame2, line 103?