5 Replies Latest reply: Apr 7, 2012 10:52 AM by Ned Murphy RSS

    removin bullets from stage after game over

    mr.shumi

      in my game enemy Planes shoot at my plane and everything is working fine

      enemy bullets are inside an array

      the problem is when my plane gets destroyed by enemy Planes ,the last bullets shot by the enemyPlanes still shows on the screen

      eventhough i called a removelistener function and added a game over screen

      enemy planes use timer which i have a listener for it

      how can i remove the last bullets that still shows on the screen?

        • 1. Re: removin bullets from stage after game over
          Ned Murphy MVP

          If the bullets are stored in an array, then use removeChild to remove them and set any references to them as null.  Empty the array when you are done.

          • 2. Re: removin bullets from stage after game over
            mr.shumi Community Member

            // responsible for making an enemy plane shoot when the timer start

            public function e1ShootBullet(event:TimerEvent):void

                    {

                        //Create a bullet and add it to the stage

                        enemyBullet1 = new Bullets(getAngle2(enemyPlane1));

                        stage.addChild(enemyBullet1);

             

                        //Set the bullet's starting position

                        var radius:int = 30;

                        var angle:Number = enemyPlane1.rotation * Math.PI / 180;

                        enemyBullet1.x = enemyPlane1.x + radius * Math.cos(angle);

                        enemyBullet1.y = enemyPlane1.y + radius * Math.sin(angle);

             

                        //Set the bullet's velocity based

                        //on the angle

                        enemyBullet1.vx = Math.cos(angle) * 5;

                        enemyBullet1.vy = Math.sin(angle) * 5;

             

                        //Push the bullet into the _bullets array

                        _eBullet.push(enemyBullet1);

             

                        //Find a random start time for the next bullet

                        var randomFireTime:Number = Math.round(Math.random() * 1000) + 200;

                        //_timer1.delay = randomFireTime;

                    }

             

            // responsible for moving the enemy Planes bullets called from an enter frame function

            private function moveEnemyBullet( enemyPlane :MovieClip)

                    {

                        if (enemyPlane == enemyPlane1)

                        {

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

                            {

                                enemyBullet1 = _eBullet[i];

                                enemyBullet1.x +=  enemyBullet1.vx;

                                enemyBullet1.y +=  enemyBullet1.vy;

             

                                if (enemyBullet1.y < 0 || enemyBullet1.x < 0 || enemyBullet1.x > stage.stageWidth || enemyBullet1.y > 609)

                                {

                                    //Remove the bullet from the stage

                                    stage.removeChild(enemyBullet1);

                                    bullethit.play();

                                    //Remove the bullet from the _bullets;

                                    //array

                                    _eBullet.splice(i,1);

                                    enemyBullet1 = null;

                                    //Reduce the loop counter

                                    //by one to compensate

                                    //for the removed bullet

             

                                }

            }

            }

            // end Game function

             

            private function endGame( endMessage:String):void

                    {

             

                        endScreen = new winLoose();

                        endScreen.end_txt.text = endMessage;

                        //endScreen.score_txt.text = String(_PlaneScoreDisplay);

                        addChild(endScreen);

                        endScreen.x = 0;

                        endScreen.y = 0;

                        _timer1.removeEventListener(TimerEvent.TIMER, e1ShootBullet);

                        _timer2.removeEventListener(TimerEvent.TIMER, e2ShootBullet);

                        _timer1.stop();

                        _timer2.stop();

                       

                        _PlaneScoreDisplay.text = "";

                        bombsLeftDisplay.text = "";

                        endScreen.playAgain_btn.addEventListener(MouseEvent.CLICK, startGame);

             

             

                    }

            • 3. Re: removin bullets from stage after game over
              Ned Murphy MVP

              I am not sure why you are showing the code without any explanation, but I do not see where you are removing any of the bullets in your endGame function, which is where you say you wanted them to go away... you only do it in your moveEnemyBullet function

              • 4. Re: removin bullets from stage after game over
                mr.shumi Community Member

                damn i missed that  ^^ damn i just get lost in my own code

                i should do the same

                stage.removeChild(enemyBullet1);

                          enemyBullet1 = null;
                • 5. Re: removin bullets from stage after game over
                  Ned Murphy MVP

                  yep, and clear the array