所以基本上我拥有的是一个名为 的函数createBoxBoundary
,当玩家位置在一定范围内时,它将边界变量设置为 true。很简单。但是,当我在主游戏循环中多次调用此函数时,只有最近调用的函数有效。下面是我的代码示例
//It should be noted the player deminsions are 40x80
function createBoxBoundary(x,y,width,height){
//right boundaries
if(playerXPos + 40 == x && playerYPos + 80 >= y && playerYPos < y + height){
boundaryRight = true;
} else{boundaryRight = false;}
//bottom boundaries
if(playerYPos == y + height && playerXPos + 40 >= x && playerXPos <= x + width){
boundaryTop = true;
} else{boundaryTop = false;}
//left boundaries
if(playerXPos == x + width && playerYPos + 80 >= y && playerYPos <= y + height){
boundaryLeft = true;
} else{boundaryLeft = false;}
//bottom boundaries
if(playerYPos + 80 == y && playerXPos + 40 >= x && playerXPos < x + width){
boundaryBottom = true;
} else{boundaryBottom = false;}
}
我还用完整的游戏代码设置了一个小提琴。如果有人对在 javascript 中进行碰撞/边界的更好方法有建议,我也对此持开放态度。任何帮助表示赞赏!