首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >请协助处理游戏逻辑,如果陈述(数学对hitTestPoint)

请协助处理游戏逻辑,如果陈述(数学对hitTestPoint)
EN

Stack Overflow用户
提问于 2014-04-15 00:32:45
回答 1查看 47关注 0票数 0

我有一个敌人,当他看到英雄时,我会希望这个敌人走到玩家面前。

代码语言:javascript
复制
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}

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

代码语言:javascript
复制
            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;
                }

这是猎犬班

代码语言:javascript
复制
/**
 * ...
 * @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;
    }




}

}

问题是,当猎犬从球员身边经过时,它仍在向左移动。因为左边的攻击仍然是真的。

我试过这么做

代码语言:javascript
复制
                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;
                }

让它变成假的,但没有骰子。

任何方向的提示,

我想在猎犬通过球员后阻止他进攻

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-15 08:10:57

在此:

代码语言:javascript
复制
    if (attackLeft)
    {
        TweenMax.to(this, 0.25, { blurFilter: { blurX:20 }} );  
        TweenMax.to(this, 1, { x:"-100" } );
    }

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

另一点是:

代码语言:javascript
复制
    if (attackLeft)
    {  ... }

    if (!attackLeft)
    {  ... }

你应该把这个改为

代码语言:javascript
复制
    if (attackLeft)
    {  ... }
    else
    {  ... }

因为它只执行一个或另一个代码块,这将节省您两次评估attackLeft。在这种情况下,这是一个很小的区别,但在重要的时候,这是一个很好的实践。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23072686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档