如何设置敌人的伤害等级

时间:2013-03-13 10:43:02

标签: actionscript-3

提前致谢...

我对我在游戏中为敌人设置伤害等级的逻辑提出了一点疑问。以下是我的敌人等级

package scripts.enemy
{
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.display.Stage;

    public class Enemy1 extends MovieClip
    {
        private var BG:MovieClip;
        private var speed:Number = 0.5;
        private var ease:Number = .005;
        private var rad:Number = 57.2957795;

        public function Enemy1(BG:MovieClip) : void
        {
            var RandomX:Array = new Array(-150,-200,-250,-300,-350,-400,-450,-500,-550,150,200,250,300,350,400,450,500,550);
            var RandomY:Array = new Array(-150,-200,-250,-300,-350,-400,150,200,250,300,350,400);
            var r:int = (Math.random() * 18);
            var s:int = (Math.random() * 12);

            x = RandomX[r];
            y = RandomY[s];

            this.BG = BG;

            addEventListener(Event.ENTER_FRAME, moveEnemy); //false, 0, true);.
        }
        private function moveEnemy(e:Event):void
        {
            var dx:Number = x - 10;
            var dy:Number = y - 10;

            this.x -= dx * ease;
            this.y -= dy * ease;

            this.rotation = (Math.atan2(dy,dx) * rad) + 180;

        }

    }

}

以下是一些代码,这些代码可以让我从我的Main类中找到麻烦

// ......... 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 (kill == true)
                {
                    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);
                        bugsKill +=  1;
                        kill = false;
                        return true;
                    }
                }
            }
            if (Level >= 2)
            {
                for (var j = 0; j < enemies2.length; j++)
                {
                    tempEnemy2 = enemies2[j];
                    if (kill == true)
                    {
                        if (tempEnemy2.hitTestPoint(test.x,test.y,true))
                        {
                            bug2HitCount -= 1;
                            if (bug2HitCount == 0)
                            {
                                enemies2.splice(j, 1);
                                bg_mc.removeChild(tempEnemy2);
                                createDead(Dead2,deadArray2,tempEnemy2.x,tempEnemy2.y,tempEnemy2.rotation);
                                Score +=  20;
                                Scr_txt.text = String(Score);
                                bugsKill +=  1;
                                kill = false;
                                //bug2HitCount = bug2HitRate;
                                return true;
                            }
                            kill = false;
                            return true;
                        }

                    }
                }
            }

            return false;
        }

        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))
                {
                    life_mc.scaleX -=  rate;
                    if (life_mc.scaleX <= 0.05)
                    {
                        stage.removeEventListener(Event.ENTER_FRAME,updateCollission);
                        Timer1.stop();
                        Mouse.show();
                        stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUpFun);
                        stage.removeEventListener(Event.ENTER_FRAME,updateStage);
                        stage.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownFun);
                        (player.parent).removeChild(player);
                        (bunker_mc.parent).removeChild(bunker_mc);
                        (life_mc.parent).removeChild(life_mc);
                        (sniper_mc.parent).removeChild(sniper_mc);
                        removeElement(bullets);
                        EndFun();
                        gunFire = false;
                        gotoAndStop("end");
                        Level = 1;
                    }
                }
            }
        }
        //...........function of Timer Complete Event.....................
        private function TimerEnd(e:TimerEvent):void
        {
            EndBug();
            gotoAndStop("end");
        }
        //...........function of Timer Complete Event.....................
        private function EndBug():void
        {
            HelpTimer = new Timer(1000,1);
            HelpTimer.addEventListener(TimerEvent.TIMER_COMPLETE,HelpFun);
            HelpTimer.start();
            stage.removeEventListener(Event.ENTER_FRAME,updateStage);
            stage.removeEventListener(Event.ENTER_FRAME,updateCollission);
            function HelpFun(Event:TimerEvent)
            {
                stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUpFun);
                stage.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownFun);
                gunFire = false;
                bg_mc.removeChild(player);
                bg_mc.removeChild(bunker_mc);
                (life_mc.parent).removeChild(life_mc);
                bg_mc.removeChild(sniper_mc);
                EndFun();
                Score = 0;
                Level +=  1;
                totalBugs += 5;
            }
        }
        //..................Function for ending the Game And removing the Reamining Enemies.................
        private function EndFun():void
        {
            Mouse.show();
            removeElement(dustArray);
            if (Level == 1)
            {
                removeElement(enemies1);
                removeElement(deadArray1);
                gotoAndStop("level2");
            }
            if (Level == 2)
            {
                removeElement(enemies1);
                removeElement(deadArray1);
                removeElement(enemies2);
                removeElement(deadArray2);
                gotoAndStop("level3");
            }
            if (Level == 3)
            {
                ......
            }

            .....................
            .....................



        }
    }
}

在这段代码中,我在第2级添加了一种新型的敌人,我也为其HitTest属性编写了代码。其中每级2级敌人需要超过1枚子弹才能杀死..但是当我射击时向一个敌人发射子弹,然后我向另一个同类型的敌人射击另一个子弹,另一个敌人被击毙。这意味着第二个敌人只能在一颗子弹中被杀死。那我怎么能解决这个问题..?

请帮助..提前致谢..

1 个答案:

答案 0 :(得分:0)

代码中的问题在于checkCollision函数。它基本上超过了第一个for循环而忽略了第二个循环。但是最好是你只需在你的敌人类中添加一个健康参数来分配敌人的健康状态,并让每个子弹的生命值降低1.这样你就可以通过你的阵列并移除任何达到0生命值的敌人。

编辑:我只是再次查看了你的代码并意识到它是bug2HitCount,这使得一切都搞砸了。