奇怪的碰撞检测系统行为

时间:2012-12-21 02:49:32

标签: actionscript-3

我正在为我的平台游戏写一个物理引擎。我所做的是在我的角色的每个末端绘制4条垂直线和4条水平线,然后测试这些线上的碰撞。这是代码:

这在每个帧之前运行

override public function update(e:Event = null):void
    {
        this.vx *= FRICTION;
        this.y += this.vy;
        this.x += this.vx;


        onGround = false;
        for (var i:uint = 0; i < level.walls.length; i++)
        {
            var sTile:ComponentView = level.walls[i];
            if (vSLsw.hitTestObject(sTile))
            {
                hitArray[0] = 1;
                if ((this.y + charHeight) >= sTile.y)
                {
                    this.y = sTile.y - charHeight;
                    onGround = true;
                }
            }
            else
                horizontalCollisionCheck(sTile);
            if (vSLse.hitTestObject(sTile))
            {
                hitArray[1] = 1;
                if ((this.y + charHeight) >= sTile.y)
                {
                    this.y = sTile.y - charHeight;
                    onGround = true;
                }
            }
            else
                horizontalCollisionCheck(sTile);

        }
        if (onGround)
            this.vy = 0;
        else 
            this.vy += GRAVITY;

        if (this.vy > SPEEDLIMITY)
            this.vy = SPEEDLIMITY;
        else if ( this.vy < -SPEEDLIMITY)
            this.vy = -SPEEDLIMITY;

        if (this.vx > SPEEDLIMITX)
            this.vx = SPEEDLIMITX;
        else if ( this.vx < -SPEEDLIMITX)
            this.vx = -SPEEDLIMITX;

        //reset array after each frame
        hitArray = [0, 0, 0, 0, 0, 0, 0, 0];
    }

private function horizontalCollisionCheck(sTile:ComponentView):void
    {
        if (hSLnw.hitTestObject(sTile))
        {
            hitArray[4] = 1;
            if (this.x < sTile.x + sTile.width + 2)
            {
                this.vx = 0;
                this.x = sTile.x + sTile.width + 2;
            }
        }
        else if (hSLne.hitTestObject(sTile))
        {
            hitArray[5] = 1;
            if (this.x + charWidth > sTile.x - 2)
            {
                this.vx = 0;
                this.x = sTile.x - charWidth - 2;
            }
        }
        if (hSLsw.hitTestObject(sTile))
        {
            hitArray[6] = 1;
            if (this.x < sTile.x + sTile.width + 2)
            {
                this.vx = 0;
                this.x = sTile.x + sTile.width + 2;
            }
        }
        else if (hSLse.hitTestObject(sTile))
        {
            hitArray[7] = 1;
            if (this.x + charWidth > sTile.x - 2)
            {
                this.vx = 0;
                this.x = sTile.x - charWidth - 2;
            }
        }
    }

问题在于,在这种情况下,当墙壁与角色右侧的线条碰撞时(vSLse,hSLse,hSLne),它可以正常工作但是当它与左边碰撞时,它会很奇怪。但是当我交换检查时(先检查vSLse而不是vSLsw),会发生相反的事情。它在左侧工作,但在右侧不太好。看看你自己,这是瑞士法郎。谢谢你的阅读。

SWF

0 个答案:

没有答案