所以这里的问题是我用来在抵消地图和移动玩家之间切换。为简单起见,我只关注左右。
这是问题的存在,我可以说:
// Determines whether to move map or player
if (game.player.mapX <= 470) {
game.movePlayerX = true;
}
if (game.player.mapX > 470) {
game.movePlayerX = false;
}
处理实际运动的部分:
// Move player left
if (game.controls.left) {
// Adjusts player.mapX
game.player.mapX -= 1;
if (game.movePlayerX == false) {
// Offsets game map
game.map.rendered = false;
game.map.xOffset += 1;
// Offsets water layer
game.water.rendered = false;
game.water.xOffset += 1;
return;
} else {
// Moves player on canvas
game.player.rendered = false,
game.player.canvasX -= 1;
}
}
// Move player right
if (game.controls.right) {
// Adjusts player.mapX
game.player.mapX += 1;
if (game.movePlayerX == false) {
// Offsets game map
game.map.rendered = false;
game.map.xOffset -= 1;
// Offsets water layer
game.water.rendered = false;
game.water.xOffset -= 1;
return;
} else {
// Moves player on canvas
game.player.rendered = false,
game.player.canvasX += 1;
}
}
问题:当您向左移动超过470(此前称为“阈值”)时,它可以正常工作。地图停止偏移,玩家开始移动。但是,当你向右移动超过阈值时,因为它正在检查game.player.mapX > 470
它实际上并没有接管直到471 ...这应该没问题,但它不会再开始抵消地图直到472.这会导致每次超过阈值时,偏移量与玩家坐标的距离越远越远。
有什么想法吗?