Moynul
Moynul

Reputation: 635

Assist with game logic please, if statements (maths vs hitTestPoint)

I've got an enemy and I will like this enemy to walk up to the player when it sees the hero.

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}

This is the pseudo code and from this I initiated the code below

            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;
                }

This is the hound class

/**
 * ...
 * @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;
    }




}

}

The problem is that when the the hound passes the player it is still going left. Because attack Left is still true.

I've tried doing this

                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;
                }

to make it false, but no dice.

Any tips of directions,

I want to stop the hound from attacking when he's gone through the player

Upvotes: 0

Views: 53

Answers (1)

DFreeman
DFreeman

Reputation: 542

Here:

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

You're handling/consuming the attack at this point, so you should then set attackLeft = false at the end.

Another small point:

    if (attackLeft)
    {  ... }

    if (!attackLeft)
    {  ... }

You should change this to

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

since it's only ever going to execute one or the other block of code and this will save you evaluating attackLeft twice. It's a trivial difference in this case but is good practice for when it does matter.

Upvotes: 1

Related Questions