我正在编写一个简单的平台游戏。我使用平铺地图来创建用于重叠和碰撞的关卡图层。我遇到了一个问题:我试图使玩家能够在与梯形图块发生重叠时爬上梯子。
我在gameSettings对象playerMoveY中有一个变量:false(因此默认情况下玩家只能向左或向右行走)
当玩家重叠梯形图块时,他可以攀爬,但之后始终可以攀爬,因为变量playerMoveY仍为true。我不知道如何切换回去。也许使用标志不是一个好主意。我需要建议。谢谢。
let gameSettings = {
playerSpeed: 60,
playerMoveY: false,
}
//here's the code from gameScene class
this.laddersLayer.setTileIndexCallback(29, this.allowClimb, this);
this.physics.add.overlap(this.player, this.laddersLayer);
movePlayerManager() {
if (this.cursorKeys.left.isDown) {
this.player.anims.play('playerWalkLeft', true);
this.player.setVelocityX(-gameSettings.playerSpeed);
} else if (this.cursorKeys.right.isDown) {
this.player.anims.play('playerWalkRight', true);
this.player.setVelocityX(gameSettings.playerSpeed);
} else {
this.player.setVelocityX(0);
this.player.anims.play('playerStand');
}
if (gameSettings.playerMoveY) {
if (this.cursorKeys.up.isDown) {
this.player.anims.play('playerClimb', true);
this.player.setVelocityY(-gameSettings.playerSpeed);
} else if (this.cursorKeys.down.isDown) {
this.player.anims.play('playerClimb', true);
this.player.setVelocityY(gameSettings.playerSpeed);
} else {
this.player.setVelocityY(0);
}
}
}
//and here's the callback function when overlap
allowClimb() {
gameSettings.playerMoveY = true;
}