如果声明(数学与hitTestPoint),请协助游戏逻辑

时间:2014-04-15 00:32:45

标签: actionscript-3 flash

我有一个敌人,当它看到英雄时,我会喜欢这个敌人走向玩家。

if(enemy is walking right && hero is in the range of the enemy)
{ enemy walk towards player
if(enemy touches player)
{enemy attacks //enemies goes straight through the player and ends up on the left side of player}


if(enemy is walking left && hero is in the range of the enemy)
{ enemy walk towards player
if(enemy touches player)
{enemy attacks //enemies goes straight through the player and ends up on the right t side of player}

这是伪代码,由此我启动了下面的代码

            for (var o:int = 0; o < aHellHoundArray.length; o++)
            {
                //var currentHound:HellHound = aHellHoundArray[o];

                var hound:HellHound = aHellHoundArray[o];

                hound.hellLoop();
                if (_character.x + 150 < hound.x && _character.x > hound.x - 600)
                {
                    hound.moveLeft = true;
                    hound.moveRight = false;
                }
                else
                if (_character.x + 50 < hound.x && _character.x > hound.x - 200 && rightKey || !rightKey)
                {
                    hound.moveRight = false;
                    hound.moveLeft = false;
                    hound.attackLeft = true;
                    trace("attack");
                }
                else
                {
                    hound.attackLeft = false;
                }

这是猎犬类

/**
 * ...
 * @author Moynul Hussain
 */
public class HellHound extends MovieClip
{
    TweenPlugin.activate([BlurFilterPlugin]);
    public var movementSpeed:Number = 3;
    public var moveLeft:Boolean;
    public var moveRight:Boolean;
    public var attack:Boolean;
    public var attackLeft:Boolean;
    private var resetPos:Point;
    private var DashAmount:Number = 20;
    public function HellHound() 
    {
        addEventListener(Event.ADDED_TO_STAGE, init)
    }

    private function init(e:Event):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        resetPos = new Point(x, y);

    }

    public function reset():void
    {
        x = resetPos.x;
        y = resetPos.y;
    }       

    public function hellLoop():void
    {
        if (attackLeft)
        {
            TweenMax.to(this, 0.25, { blurFilter: { blurX:20 }} );  
            TweenMax.to(this, 1, { x:"-100" } );
        }

        if (!attackLeft)
        {
            TweenMax.to(this, 0.5, { blurFilter: { blurX:0, blurY:0 }} );
        }

        if (moveLeft)
        {
            this.x -= 2;
            this.scaleX = 1;
            this.gotoAndStop("run");

        }


        if (moveRight)
        {
            this.x += 2;
            this.scaleX = -1;
            this.gotoAndStop("run");
        }

        if (!moveLeft && !moveLeft && !attack)
        {   
        //  TweenLite.to(this,0,{blurFilter:{blurX:0}});
        //  TweenLite.to(this,0,{blurFilter:{blurY:1000}});
            TweenMax.to(this, 0.5, { blurFilter: { blurX:0, blurY:0 }} );
        }
    }

    public function dontMove():void 
    {
        moveLeft = false;
        moveRight = false;
    }




}

}

问题是,当猎犬经过玩家时,它仍然会离开。因为攻击左派仍然是真的。

我试过这个

                if (_character.x + 150 < hound.x && _character.x > hound.x - 600)
                {
                    hound.moveLeft = true;
                    hound.moveRight = false;
                }
                else
                if (_character.x + 50 < hound.x && _character.x > hound.x - 200 && rightKey || !rightKey)
                {
                    hound.moveRight = false;
                    hound.moveLeft = false;
                    hound.attackLeft = true;
                    trace("attack");
                }
                else
                {
                    hound.attackLeft = false;
                }

使其成为假,但没有骰子。

任何指示提示,

我想阻止猎犬穿过玩家时攻击

1 个答案:

答案 0 :(得分:1)

这里:

    if (attackLeft)
    {
        TweenMax.to(this, 0.25, { blurFilter: { blurX:20 }} );  
        TweenMax.to(this, 1, { x:"-100" } );
    }

此时您正在处理/消耗攻击,因此您应该在结束时设置attackLeft = false。

另一个小问题:

    if (attackLeft)
    {  ... }

    if (!attackLeft)
    {  ... }

您应该将其更改为

    if (attackLeft)
    {  ... }
    else
    {  ... }

因为它只会执行一个或另一个代码块,这将节省你两次评估attackLeft。在这种情况下,这是一个微不足道的差异,但在它确实重要的时候是很好的做法。