正如标题所示,我在为HTML5画布游戏创建一个实体对象时遇到了麻烦。
到目前为止,我可以阻止玩家走进盒子的左右两侧,并阻止它跳到盒子里(玩家击中盒子的底部然后掉下来)但我似乎无法让玩家站立/沿着顶部行走,它只是不断地向上或向下漂浮(取决于我给它的+或 - 号码以阻止它进入盒子。)
玩家还需要能够在对象顶部跳起/跳起。
这是我到目前为止获得的代码:
function collides(player, platform){
//return !(player.x > platform.x + platform.width || player.x + player.width < platform.x || player.y > platform.y + platform.height || player.y + player.height < platform.y);
if(player.x + player.width > platform.x && !(player.x > platform.x + platform.width) && !(player.y + player.height < platform.y) && !(player.y > platform.y + platform.height)){
player.velocity_x = -1;
}
if(player.x < platform.x + platform.width && !(player.x < platform.x) && !(player.y + player.height < platform.y) && !(player.y > platform.y + platform.height)){
player.velocity_x = 1;
}
if(player.y + player.height < platform.y && player.x + player.width > platform.x && player.x < platform.x + platform.width && (player.y < platform.y + platform.height)){
//player.y = platform.y -41;
player.velocity_y = -1;
//jumping = false;
}
if(player.y < platform.y + platform.height && player.x + player.width > platform.x && player.x < platform.x + platform.width && !(player.y + player.height < platform.y)){
player.velocity_y = 1;
}
}