【问题标题】:Multiple If Statements - Checking Player Collision Only Once多个 If 语句 - 只检查一次玩家碰撞
【发布时间】:2016-12-16 11:51:32
【问题描述】:

我目前正在制作游戏,对于玩家移动,我有 if 语句来检查你是否正在撞墙。

这是我正在做的一个小例子:

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

        // move

}

但是,我的问题是,现在我正在为不同的级别添加更多的墙,if 语句变得有点不稳定。例如,添加第二面墙:

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

            // move

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

            // move

    }

但这只是将移动速度加倍,因为如果你没有撞到任何墙壁,两者都会导致 true。

我的尝试

我试过添加 else ,像这样:

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

        // move

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

        // move

}

但这会导致不检查两面墙。

如何添加多个 if 语句来有效地检查多个墙?

【问题讨论】:

    标签: java if-statement


    【解决方案1】:

    你要做的是检查它是否撞到任何墙壁,如果没有,移动

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

    【讨论】:

    • Aaaa 就这么简单。谢谢!
    【解决方案2】:

    但这只是将移动速度加倍,因为两者都会导致 true if 你没有撞墙。

    鉴于这两个 if 语句,结果应该是这样的。

    这是一些简单的概率论。

    当你与 wall1 相交时,它会变成红色

    当你与 wall2 相交时,它会变成绿色

    当你不移动或其他任何情况下,它会变成黑色。

    我怎样才能完成添加多个 if 语句来检查 多墙有效吗?

    我猜你想检查玩家是否成功不撞墙,它将获得速度奖励。

    您需要更改算法。 例如,

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-12
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      相关资源
      最近更新 更多