【问题标题】:[AS3]Collision with walls using hitTestObject[AS3]使用 hitTestObject 与墙壁碰撞
【发布时间】:2014-05-20 23:08:16
【问题描述】:

所以我目前正在尝试做一个几乎像迷宫一样的游戏。 问题在于墙壁碰撞,一旦角色撞到墙壁,我就无法再将他弄出来,无论我在碰撞后尝试将他带向哪个方向,他都会“卡住”。 我想的解决方案之一是每当角色撞到墙上时,“支持他”,这样就不会再检测到碰撞。然而,当我这样做时,他以一种奇怪的方式穿过墙壁。 这是我的代码,所以你们可以知道我在做什么:

function keyPressed(event:KeyboardEvent):void
{

    if (event.keyCode == Keyboard.LEFT)
    {
       leftArrow = true;
       if(char.hitTestObject(test))
       {
           leftHit= true;
       } else { 
           leftHit = false;
       }
    }
    if (event.keyCode == Keyboard.RIGHT)
    {
       rightArrow = true;
       if(char.hitTestObject(test))
       {
           rightHit= true;
       } else { 
           rightHit = false;
       }
    }


}

function keyReleased(event:KeyboardEvent):void 
{

    if (event.keyCode == Keyboard.LEFT) 
    {
        leftArrow = false;
    }
    if (event.keyCode == Keyboard.RIGHT)
    {
       rightArrow = false;
    }

}

function walking(event:Event):void {
    if (rightArrow) {
        if(rightHit)
            char.x -= speed;
        else
            char.x += speed;    
    }

    if (leftArrow) {
        if(leftHit)
            char.x += speed;
        else
            char.x -= speed;
    }
}

这段代码的很大一部分实际上是从另一个问同样问题的人那里得到的。即使按照其他主题中的建议进行操作,问题仍然存在。 非常感谢您的帮助!

【问题讨论】:

  • 你的速度值是否在帧与帧之间保持一致?还是基于自上一帧以来经过的时间?
  • 帧与帧一致。它基本上是一个常数,我将它的值保留为 10。

标签: actionscript-3 flash collision-detection collision maze


【解决方案1】:

根据要求,这是我的解决方案:

 if (rightArrow) {
                if (!test.hitTestPoint(MovieClip(root).char.x+speed, MovieClip(root).char.y, true))
                {
                    MovieClip(root).char.x += speed;    
                    x = x-speed; //moving the background
                }
            }

            if (leftArrow) {
                if (!test.hitTestPoint(MovieClip(root).char.x-speed, MovieClip(root).char.y, true))
                {
                    MovieClip(root).char.x -= speed;
                    x = x+speed; //moving the background
                }
            }
            if (upArrow) {
                if (!test.hitTestPoint(MovieClip(root).char.x, MovieClip(root).char.y-speed, true))
                {
                    MovieClip(root).char.y -= speed;    
                    y = y+speed; //moving the background
                }
            }

            if (downArrow) {
                if (!test.hitTestPoint(MovieClip(root).char.x, MovieClip(root).char.y+speed, true))
                {
                    MovieClip(root).char.y += speed;
                    y = y-speed; //moving the background
                }
            }

已经有一段时间了,有些东西我真的不记得了,但据我所知,我检查了增加我的角色的速度是否会使其与墙壁发生碰撞。如果发生这种情况,即使我看起来有足够的空间,我也不会移动角色。我觉得差不多就这些了。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2015-08-03
    相关资源
    最近更新 更多