0
package 
{
    import flash.display.*;
    import flash.events.*;
    import flash.utils.Timer;
    import flash.text.TextField;
    import flash.ui.Mouse;
    import flash.geom.Point;
    import flash.media.Sound;
    import flash.net.*;
    import scripts.player.*;
    import scripts.bullets.*;
    import scripts.enemy.*;
    import scripts.enemyDead.*;
    import scripts.maskmc.torch;

    public class Main extends MovieClip
    {
        // ***********  Player Variables *************
        private var player:MovieClip;
        private var bunker_mc:MovieClip;
        private var life_mc:MovieClip;
        private var bg_mcX = 0;
        private var bg_mcY = 0;
        // ***********  Bullet var & Array ************
        private var b:MovieClip;
        private var bullets:Array;
        private var gunFire:Boolean = false;
        private var isLoaded:Boolean = true;
        private var reloadGun:Timer;
        private var reloadSpeed:Number = 250;
        private var sniper_mc:MovieClip;
        // *******  Enemies & their Dead Animation Arrays And Variables  ***********

        // *******  For Enemy type 1  *******************
        private var enemy1:MovieClip;
        private var tempEnemy1:MovieClip;
        private var dead1:MovieClip;
        private var deadArray1:Array;
        private var enemies1:Array;


        // *******   Score & other Helper var
        private var hit:int = 2;
        private var hit1:int = 3;
        private var hit2:int = 5;
        private var hit3:int = 8;
        private var hit4:int = 10;
        private var Level:int = 1;
        private var MGun:int;
        private var MGun1:int;
        private var powerPlayer:int = 1;
        private var power:MovieClip;
        private var power2_mc:MovieClip;
        private var Score:int;
        private var HighScore:int;
        private var mouseSpeed:Number = 25;
        private var PD1:MovieClip;
        private var PD2:MovieClip;
        private var PD3:MovieClip;
        private var h1:MovieClip;
        private var tym:int;
        private var eneCount:int = 0;
        private var saveData:SharedObject;

        private var GameLevelTimer:Timer;
        private var Timer1:Timer;
        private var PowerTimer:Timer;
        private var HelpTimer:Timer;
        private var lvl:MovieClip;
        private var flag:int = 0;

        public function startMain()
        {
            // ******* Sniper stuff ******************
            var sniper_mc:sniper = new sniper();

            //............. setting the X Y co ordinates of Background .......
            bg_mc.x = 400;
            bg_mc.y = 300;
            //............. Player and Bunker initialisation and display on screen...............
            bunker_mc = new bunker();
            life_mc = new life();
            bunker_mc.x = bg_mcX;
            bunker_mc.y = bg_mcY;
            life_mc.x = 339;
            life_mc.y = 590;
            stage.addChild(life_mc);
            bg_mc.addChild(bunker_mc);
            //.............. Player Power display..................................
            PD1 = new Pd1();
            PD1.x = 50;
            PD1.y = 580;

            // ............. Defining Events................
            stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpFun);
            stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownFun);
            stage.addEventListener(Event.ENTER_FRAME,updateCollission);
            stage.addEventListener(Event.ENTER_FRAME,updateStage);
            //.............. Calling Enemy spawning timer function...............
            timeStart();
            //.............. Calling Player creating function..................
            createPlayer();
            //.............. Adding Mask i.e. Night Effect for Level 8,9..............
            if (Level == 8 || Level == 9)
            {
                torch_mc = new torch(bg_mcX,bg_mcY,bg_mc);
                dark_mc = new dark();
                bg_mc.addChild(dark_mc);
                bg_mc.addChild(torch_mc);
            }
            //.............. Initialising Enemy Objects arrays.................
            bullets = new Array();
            enemies1 = new Array();
            deadArray1 = new Array();
            dustArray = new Array();

            // ............. Initialisng Score & level variables............
            Score = 0;
            tym = 60;
            eneCount = 0;
            saveData = SharedObject.getLocal("test");
            Lvl_txt.text = String(Level);
            Scr_txt.text = String(Score);
            Time_txt.text = String(tym);
        }
        //.............. Timer for Enemy Spawning ................
        private function timeStart():void
        {
            Timer1 = new Timer(1000,60);
            Timer1.addEventListener(TimerEvent.TIMER,TimerStart);
            Timer1.start();
            Timer1.addEventListener(TimerEvent.TIMER_COMPLETE,TimerEnd);
            if (Level == 3)
            {
                var powerDelay:int = Math.random() * 8;
                if (powerDelay == 0)
                {
                    powerDelay = 5;
                }
                PowerTimer = new Timer(1000,powerDelay);
                PowerTimer.addEventListener(TimerEvent.TIMER_COMPLETE,powerFun);
                PowerTimer.start();
            }
            if (Level == 5)
            {
                var powerDelay1:int = Math.random() * 8;
                if (powerDelay1 == 0)
                {
                    powerDelay1 = 5;
                }
                PowerTimer = new Timer(1000,powerDelay1);
                PowerTimer.addEventListener(TimerEvent.TIMER_COMPLETE,powerFun2);
                PowerTimer.start();
                MGun1 = 1;
            }
        }
        //............. Player Creating Function.......................
        private function createPlayer():void
        {
            if (powerPlayer == 1)
            {
                player = new Player(bg_mcX,bg_mcY,bg_mc);
                bg_mc.addChild(player);
                reloadSpeed = 250;
            }
        }
        //............ Mouse Event Handlers...........................
        private function mouseUpFun(event:MouseEvent):void
        {
            gunFire = false;
        }
        //Mouse Down Event Handler
        private function mouseDownFun(event:MouseEvent):void
        {
            gunFire = true;
        }
        private function reloadGunHandler(e:TimerEvent):void
        {
            // stop timer
            e.target.stop();

            // clear timer var
            reloadGun = null;

            reloadWeapon();
        }
        //.............. Enemy spawning Timer Function..................
        private function TimerStart(e:TimerEvent):void
        {
            eneCount += 1;
            tym -= 1;
            Time_txt.text = String(tym);
            if (Level == 1)
            {
                createEnemy(enemy1,Enemy1,enemies1);
            }
        }
        //............. Function Creating Enemy .....................
        private function createEnemy(enemy:MovieClip,enemyClass:Class, enemyArray:Array)
        {
            enemy = new enemyClass(bg_mc);
            enemyArray.push(enemy);
            bg_mc.addChild(enemy);
        }
        //............. Function Creating dead Animation................
        private function createDead(Dead:Class,DeadArray:Array,xx:Number,yy:Number,rr:Number):void
        {
            dead1 = new Dead(xx,yy,rr);
            DeadArray.push(dead1);
            bg_mc.addChild(dead1);
        }
        // .............Function that remove the initial type of Player.......
        private function modifyPlayer():void
        {
            bg_mc.removeChild(player);
        }

        private function powerFun(e:TimerEvent):void
        {
            power = new power1();
            addChild(power);
            power.x = 500;
            power.y = 100;
            stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyboardDown);
            MGun = 1;
        }
        private function powerFun2(e:TimerEvent):void
        {
            power = new power2();
            addChild(power);
            power.x = 500;
            power.y = 100;
            MGun1 = 1;

        }
        private function KeyboardDown(e:KeyboardEvent)
        {
            flag = 0;
            if (e.keyCode == 49)
            {
                powerPlayer = 1;
                modifyPlayer();
                createPlayer();
            }

        }
        //................ Function that updates Bullet and Stageon EnterFrame Event..........;
        private function updateCollission(e:Event):void
        {
            if(Level == 1)
            {
                collideEnemy(deadArray1,enemies1,0.00025);
            }

        }
        //................ Function that updates Bullet and Stageon EnterFrame Event..........;
        private function updateStage(e:Event):void
        {
            // ............. Updating Stage according mouse co-ordinates...........
            if (mouseX <= 200 || mouseX >= 600)
            {
                bg_mc.x +=(bg_mc.x - mouseX)/mouseSpeed;
                if (bg_mc.x <= 210)
                {
                    bg_mc.x = 210;
                }
                if (bg_mc.x >= 590)
                {
                    bg_mc.x = 590;
                }
            }

            if (mouseY <= 200 || mouseY >= 400)
            {
                bg_mc.y +=(bg_mc.y - mouseY)/mouseSpeed;
                if (bg_mc.y <= 150)
                {
                    bg_mc.y = 150;
                }
                if (bg_mc.y >= 440)
                {
                    bg_mc.y = 440;
                }
            }
            // ..............Calling Function for fire........
            fire();
            var i:int;
            var tempBullet:MovieClip;

            // ........... loop for _bullets array ...........
            for (i = 0; i < bullets.length; i++)
            {
                //.... save a reference to current bullet.....
                tempBullet = bullets[i];

                //.... check for collision with Enemies.......
                if (checkCollision(tempBullet))
                {
                    destroyBullet(tempBullet);
                    eneCount -= 1;
                }
            }
        }
        //........... Function Defination of Fire...............
        private function fire():void
        {
            // check if firing
            if (! gunFire)
            {
                return;
            }
            // check if reloaded
            if (! isLoaded)
            {
                return;
            }
            // create bullet function.
            fireBullet();

            // start reload timer
            reloadGun = new Timer(reloadSpeed);
            reloadGun.addEventListener(TimerEvent.TIMER, reloadGunHandler);
            reloadGun.start();

            // set reload flag to false;
            isLoaded = false;
        }
        //............. Function Creating the Bullet.........
        private function fireBullet():void
        {
            var gunSnd:deagle = new deagle();
            player.play();
            if (powerPlayer == 1)
            {
                b = new Bullet(mouseX,mouseY,player.rotation,300);
                bullets.push(b);
                bg_mc.addChild(b);
                var f1:Fire1 = new Fire1();
                f1.x = b.x;
                f1.y = b.y;
                f1.rotation = b.rotation;
                bg_mc.addChild(f1);
            }

        }
        // ......... Function that Gives Delay in Firing the Bullets............
        private function reloadWeapon():void
        {
            isLoaded = true;
        }
        // ......... Function for Checking the Collision between Bullet And Enemy...........
        private function checkCollision(mc:MovieClip):Boolean
        {
            var test:Point = mc.localToGlobal( new Point());
            for (var i = 0; i < enemies1.length; i++)
            {
                tempEnemy1 = enemies1[i];
                if (tempEnemy1.hitTestPoint(test.x,test.y,true))
                {
                    enemies1.splice(i, 1);
                    bg_mc.removeChild(tempEnemy1);
                    createDead(Dead1,deadArray1,tempEnemy1.x,tempEnemy1.y,tempEnemy1.rotation);
                    Score +=  10;
                    Scr_txt.text = String(Score);
                    return true;
                }
            }
            return false;
        }
        //............. Function that destroy the Bullet...........
        private function destroyBullet(bullet:MovieClip):void
        {
            var tempBullet:MovieClip;
            for (var i = 0; i<= bullets.length; i++)
            {
                tempBullet = bullets[i];
                if (tempBullet == bullet)
                {
                    dust = new dust_mc();
                    dust.x = bullet.x;
                    dust.y = bullet.y;
                    dust.rotation = (Math.random() * 360);
                    // remove from array
                    bullets.splice(i, 1);
                    bullet.parent.removeChild(bullet);
                    dustArray.push(dust);
                    bg_mc.addChild(dust);
                    return;
                }
            }
        }
        private function removeElement(removeList:Array):void
        {
            for (var i = 0; i < removeList.length; i++)
            {
                bg_mc.removeChild(removeList[i]);
            }
        }
        //...........Function Checking the Collission Between Bunker And Enemy..............
        private function collideEnemy(deadArray:Array,enemyArray:Array,rate:Number):void
        {
            var enemy:MovieClip;
            for (var i = 0; i < enemyArray.length; i++)
            {
                enemy = enemyArray[i];
                if (enemy.hitTestObject(bunker_mc))
                {
                    if (life_mc.scaleX <= 0.1)
                    {
                        Timer1.stop();
                        stage.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownFun);
                        stage.removeEventListener(KeyboardEvent.KEY_DOWN,KeyboardDown);
                        stage.removeEventListener(Event.ENTER_FRAME,updateCollission);
                        bg_mc.removeChild(player);
                        bg_mc.removeChild(bunker_mc);
                        stage.removeChild(life_mc);
                        EndFun();
                        gotoAndStop("end");
                        Level = 1;
                        powerPlayer = 1;
                    }
                    life_mc.scaleX -=  rate;
                }
            }
        }
        //...........function of Timer Complete Event.....................
        private function TimerEnd(e:TimerEvent):void
        {
            HelpTimer = new Timer(1000,eneCount);
            HelpTimer.addEventListener(TimerEvent.TIMER_COMPLETE,HelpFun);
            HelpTimer.start();
            function HelpFun(Event:TimerEvent)
            {
                stage.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownFun);
                stage.removeEventListener(KeyboardEvent.KEY_DOWN,KeyboardDown);
                bg_mc.removeChild(player);
                bg_mc.removeChild(bunker_mc);
                stage.removeChild(life_mc);
                EndFun();
                Score = 0;
                Level +=  1;
            }
        }
        //..................Function for ending the Game And removing the Reamining Enemies.................
        private function EndFun():void
        {
            stage.removeEventListener(Event.ENTER_FRAME,updateStage);
            removeElement(bullets);
            removeElement(dustArray);
            if (Level == 1)
            {
                removeChild(PD1);
                removeElement(enemies1);
                removeElement(deadArray1);
                gotoAndStop("level2");
            }
            HighScore +=  Score;
            if(saveData.data.savedScore == null )
            {
                saveData.data.savedScore = HighScore;
                saveData.flush();
            }
            if(saveData.data.savedScore > HighScore)
            {
                HighScr_txt.text = String(saveData.data.savedScore);
            }
            else
            {
                HighScr_txt.text = String(HighScore);
            }
        }
    }
}

ArgumentError:错误 #2025:提供的 DisplayObject 必须是调用者的子对象。在 flash.display::DisplayObjectContainer/removeChild() 在 Main/collideEnemy() 在 Main/updateCollission()

你好..提前谢谢..我检查了关于我的问题的可用建议,但我无法解决我的问题..这个错误让我有点困惑。我已经多次查看我的代码,但我不明白我是如何得到这个错误的..请提出更正建议..请帮助..

4

1 回答 1

0

此错误在尝试时显示removeChild()不是DisplayObjectContainer.

我想,理性stage.removeChild(life_mc);collideEnemy()起作用。你必须检查life_mc父母是否stage。我想,父母bg_mc。看了上面两行代码,好像是问题所在。

尝试转换stage.removeChild(life_mc);bg_mc.removeChild(life_mc);. 如果不工作。检查所有对象的父级。你知道父母,并尝试从父母中删除。

trace(life_mc.parent);
trace(player.parent);
trace(bunker_mc.parent);

或者简单地说,运行以下代码。也许它运作良好。

(player.parent).removeChild(player);
(bunker_mc.parent).removeChild(bunker_mc);
(life_mc.parent).removeChild(life_mc);
于 2013-02-27T07:39:00.770 回答