AS3碰撞过程不断返回False

时间:2012-05-10 22:39:34

标签: actionscript-3 collision-detection side-scroller

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

}

My Collision处理在上面......我遇到了这个问题,即使我在_boundaries中与一个对象发生碰撞,Collision布尔也会不断返回为false。它会在我的播放器上产生一个摇晃效果,因为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;

}

1 个答案:

答案 0 :(得分:1)

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

(2)当你发现碰撞时,Player.y -= 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,因此需要进行改进以确定确切的影响点,然后在停止前移动那里。