0
function ProcessCollisions():void{

//respawn if player died
if(percentHP == 0){
    Player.x = _startMarker.x;
    Player.y = _startMarker.y;
    Gun.x = Player.x;
    Gun.y = Player.y - 45;
    _boundaries.x = 0;
    _boundaries.y = 0;
    Player.vy = 0;
    currentHP = maxHP;
}
//when Player is Falling
if(Player.vy > 0){
    //Otherwise process collisions of boundaires
        var collision:Boolean = false;
        if(_boundaries.hitTestPoint(Player.x, Player.y, true)){collision = true;}
        if(collision == true){
            while(collision == true){
                Player.y -= 0.1;
                Gun.y -= 0.1;
                collision = false;
                if(_boundaries.hitTestPoint(Player.x, Player.y, true)){collision = true;}
            }
            trace("Collision is " + collision);
            currentHP -= Player.vy * .1
            Player.vy = 0;
            Gun.vy = 0;
            JumpCount = 0;
        }
}

}

我的碰撞处理在上面......我有这个问题,即使我与 _boundaries 内的对象发生碰撞,碰撞布尔值也会不断返回为假。当 while 循环不断将玩家拉出边界并且玩家似乎无限陷入其中时,它会在我的玩家身上产生震动效果......有人可以帮忙吗?同样在由 Enter Frame Timer 调用的 Frame 处理程序中,我对 Player.vy 进行了更多更改,如下所示

function FrameHandler(e:Event):void{
    //If Statements to slow character
    if(aKeyPressed == false && Player.vx > 0){
        Player.vx -= 1
    }
    else if(dKeyPressed == false && Player.vx < 0){
        Player.vx += 1
    }
    //Gravitates Player
     Player.vy += 1.5;
    //Controls arrays of bullets for Weapons
    //Process Collisions
    ProcessCollisions();
    //Scroll Stage
    scrollStage();
    //Attunes for multiple keypresses
    MultipleKeypresses();
    //Keeps Gun attached to Players Arm
    Gun.x = Player.x
    Gun.y = Player.y - 45;
    if(sKeyPressed){
        Gun.y = Player.y - 13;
        Gun.x = Player.x + 8;
    }
    //Checks Health
    updateHealthBar()
    //move Player and Gun
    Gun.y += Player.vy;
    Gun.x += Player.vx;
    Player.x += Player.vx;
    Player.y += Player.vy;

}
4

1 回答 1

1

(1)trace("Collision is " + collision);仅在您离开while(collision == true)循环后执行,因此您只会"Collision is false"在跟踪中看到。

(2) 当您检测到碰撞时,Player.y -= 0.1;以 0.1 的步长将玩家向上移动,因此您与碰撞对象的距离永远不会超过 0.1

然后,设置好Player.vy=0后,它会在下一帧重置为 1.5(按Player.vy += 1.5;),并Player.y以 1.5 递增……这将始终导致另一次碰撞!

编辑:解决这个问题的方法取决于你想要什么效果:如果你想让玩家像蹦床一样从物体上反弹,你可以Player.vy = 0;Player.vy = -Player.vy;.

或者,为了让玩家降落在对象上并停留在那里,您可以在更新之前测试潜在的碰撞Player.y。像这样的东西:

//move Player and Gun, if we're not about to collide
if(_boundaries.hitTestPoint((Player.x+Player.vx), (Player.y + Player.vy), true)==false){
    Player.y += Player.vy;
    Gun.y += Player.vy;
    Player.x += Player.vx;
    Gun.x += Player.vx;
} else {
    // we're about to collide: stop moving
    Player.vy = 0;
    Player.vx = 0;
}

这不是一个完美的解决方案:它可能会使玩家降落在距离物体一定距离的地方,具体取决于vyvx,因此改进方法是计算出确切的撞击点,并在停止之前移动到那里。

于 2012-05-10T22:55:44.643 回答