简体   繁体   English

多个if语句-仅检查一次玩家碰撞

[英]Multiple If Statements - Checking Player Collision Only Once

I am currently making a game and for player movement, I have if statements that check if you are running into the walls. 我目前正在制作游戏,为了让玩家移动,我有if语句来检查您是否碰壁。

Here is a small example of what I am doing: 这是我正在做的一个小例子:

if(!(player.intersects(wall1)){

        // move

}

However, my problem is that now that I am adding more walls for different levels, the if statements are getting a bit wonky. 但是,我的问题是,由于现在我为不同级别添加了更多墙,因此if语句变得有点不可思议。 For example, adding a second wall: 例如,添加第二道墙:

    if(!(player.intersects(wall1)){

            // move

    }
    if(!(player.intersects(wall2)){

            // move

    }

But this just doubles the movement speed since both result in true if you aren't hitting any walls. 但这只是移动速度的两倍,因为如果您没有碰到任何墙壁,两者都会得出正确的结果。

What I've Tried 我尝试过的

I've tried adding else to it, like this: 我试图添加其他内容,如下所示:

if(!(player.intersects(wall1)){

        // move

} else
if(!(player.intersects(wall2)){

        // move

}

But this will result in not checking for both walls. 但这将导致不检查两个墙。

How can I accomplish adding multiple if-statements that check for multiple walls effectively? 如何完成添加多个if语句,以有效地检查多个墙?

What you want to do is check it if hits any of the walls and if not, move 您要做的是检查是否碰到任何墙壁,如果没有碰到,请移动

if(!(player.intersects(wall1) && !(player.intersects(wall2) /* and so on */){

        // move

}

But this just doubles the movement speed since both result in true if you aren't hitting any walls. 但这只是移动速度的两倍,因为如果您没有碰到任何墙壁,两者都会得出正确的结果。

Given that two if-statements, the result should be like this. 给定两个if语句,结果应该是这样的。

图片为插图

This is some simple probability theory. 这是一些简单的概率论。

When you intersects wall1, it will fall into red 与wall1相交时,它将变成红色

When you intersects wall2, it will fall into green 与wall2相交时,它将变为绿色

When you are not moving or any other case, it will fall into black. 当您不动或其他情况时,它会变成黑色。

How can I accomplish adding multiple if-statements that check for multiple walls effectively? 如何完成添加多个if语句,以有效地检查多个墙?

I guess you want to check if the player successfully not collide with wall, it will be rewarded with speed. 我想您想检查一下玩家是否成功不与墙碰撞,它将获得速度的回报。

You need to change the algorithm. 您需要更改算法。 For example, 例如,

// a line of area is newly added as a checkpoint
if (player not collide with walls and passed through a line of area) { 
    rewarded 
} else if (player collide with walls and passed through a line of area) {
    not rewarded
} else {
    not rewarded
    // it may be probably not moving or not collide with anything and 
    // not passing through our checkpoint
}

hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM