在碰撞过程中,阵列中的多个敌人会受到影响

时间:2014-04-09 21:54:24

标签: arrays actionscript-3 flash

    private function enemyCollisionGoblin():void 
    {


        var goblinCanMove:Boolean = true;;

        for (var i:int = 0; i < aGoblinArray.length; i++)
        {
            var goblin:Goblin = aGoblinArray[i];
            //goblin hits player
            if (goblin.hitTestPoint(_character.x + 30, _character.y - 45, true) || goblin.hitTestPoint(_character.x - 30, _character.y - 45, true))
            {
                if (!defendKey)
                {
                    currentHP -= 1;
                    updateHealthBar();  
                    chckDeathCon();
                }
                goblinCanMove = false; 
                goblin.canAttack();
            }

            if (attackKey && lookingLeft &&!lookingRight)
            {
                if (goblin.hitTestPoint(_character.x + 100, _character.y - 45, true))
                {
                    goblinCanMove = false; 
                    trace("lance hits");
                    //hitOnce
                    if (!hitOnce)
                    {
                        hitOnce = true;
                        goblin.moveBack();
                        trace("take on damage");
                    }
                }
            }

            if (goblinCanMove)
            {

                goblin.canMove();

            }
        }
    }

           //this is before this happens, declared on top of main constructor 
           private var aGoblinArray = new Array(container.goblin1, container.goblin2, container.goblin3);

你好程序员,我有阵列和逻辑的问题。

我所做的是通过数组循环以确保三个敌人,地精都在循环中。

我有数据后输入一个名为goblin的变量到Goblin类,它等于地精数组,变量i等于1,2和3,它们是数组中的地精。

如果妖精击中了敌人,那么妖精可以攻击并且goblinCanMove = false; 这意味着击中敌人的阵列中的妖精不应该移动并且应该攻击。

但问题是,当第一个敌人击中地精时,(goblin1)这个地精停止移动并攻击,而goblin1和goblin2移动。但是当地精2击中敌人时,地精2会停下来进行攻击。但是goblin1和goblin3停止了移动。

当goblin3攻击敌人时它会停止并发动攻击,然后goblin1停止移动,但是goblin2确实会犹豫不决。

同样的事情适用于攻击,当我攻击goblin1时,他停止移动并且goblin2和3移动。

我攻击地精2,所有地精都受到影响。我攻击goblin3,他停下来,goblin1也是如此。

这是妖精类。

    package {
import flash.display.MovieClip;
import flash.events.Event;
import Main;
import player;

/**
 * ...
 * @author Moynul Hussain
 */
public class Goblin extends MovieClip {
    private var nTimer: Number;
    private var goblinLife; int;
    public static var lookingRight: Boolean;
    public static var canAttackLeft: Boolean;
    public static var canAttackRight: Boolean;

    public var attack: Boolean;
    public var speed: int;
    public static var move: Boolean;

    public function Goblin() {
        move = true;
        nTimer = 0;
        speed = 1;
        goblinLife = 2;
        this.addEventListener(Event.ADDED_TO_STAGE, onAdd)

    }


    private function onAdd(e: Event): void {
        //trace("added Goblin");
        removeEventListener(Event.ADDED_TO_STAGE, onAdd);
        //this.addEventListener(Event.ENTER_FRAME, goblinLoop);
    }

    public function goblinLoop():void
    {

    }

    public function canAttack():void 
    {
        this.gotoAndStop("attack");

    }

    public function canMove(): void 
    {
        nTimer = nTimer + 0.05;
        if (nTimer < 5) 
        {
            lookingRight = false;
            this.scaleX = -1;
            this.gotoAndStop("run");
            this.x += speed;
            lookingRight = true;
            //trace("right");
        } 
        else 
        {
            lookingRight = false;
        //trace("looking LEFT");
            this.x -= speed;
            this.gotoAndStop("run");
            this.scaleX = 1;
            if (nTimer > 10) //this can't be 5, it's not logical, so it has to be 10)
            {
                nTimer = 0;
            }
        }
    }



    public function moveRight():void 
    {
            this.x -= speed;
            this.gotoAndStop("run");
            this.scaleX = 1;    
    }

    public function rangeAttack():void 
    {


    }

    public function takeDamage():void 
    {
        goblinLife--;
    }

    public function doNothing():void 
    {
        this.gotoAndStop(1);
    }

    public function moveBack():void 
    {
        this.gotoAndStop("beenHit");
        this.x
    }





}

}

很抱歉,如果它需要很多东西。

1 个答案:

答案 0 :(得分:2)

问题是你的布尔值最初设置为true,但是只要一个golblin将其设置为false,它就会变成所有地精的假。尝试将其移动到for循环中:

private function enemyCollisionGoblin():void 
{
    var goblinCanMove:Boolean;
    for (var i:int = 0; i < aGoblinArray.length; i++)
    {
        goblinCanMove = true;
        /* rest of the code */
    }
}