我在scala中有一个数组:
var walls: Array[LineSprite] = new Array[LineSprite](26)
然后我有这些 if 语句:
if(curPlayer.intersects(walls(0))) {
handleCollision
updateLives(-1)
}
if(curPlayer.intersects(walls(1))) {
handleCollision
updateLives(-1)
}
if(curPlayer.intersects(walls(2))) {
handleCollision
updateLives(-1)
}
if(curPlayer.intersects(walls(3))) {
handleCollision
updateLives(-1)
}
if(curPlayer.intersects(walls(4))) {
handleCollision
updateLives(-1)
}
if(curPlayer.intersects(walls(5))) {
handleCollision
updateLives(-1)
}
if(curPlayer.intersects(walls(6))) {
handleCollision
updateLives(-1)
}
if(curPlayer.intersects(walls(7))) {
handleCollision
updateLives(-1)
}
if(curPlayer.intersects(walls(8))) {
handleCollision
updateLives(-1)
}
if(curPlayer.intersects(walls(9))) {
handleCollision
updateLives(-1)
}
if(curPlayer.intersects(walls(10))) {
handleCollision
updateLives(-1)
}
if(curPlayer.intersects(walls(11))) {
handleCollision
updateLives(-1)
}
...
我如何才能使数组中的所有项都只有一个 if 语句,而不是为数组中调用 handleCollision 和 updateLives(-1) 的每个项目提供一个单独的 if 语句?
谢谢你。