我正在制作 Pokemon like 游戏,我需要让我的Sprite像Pokemon的游戏一样移动。 我的意思是, tile to tile ,cell to cell。我正在使用 tIDE (图块地图编辑器),我的图块宽度 32px 。我希望播放器移动32px每32px 并在移动过程中播放动画。正如口袋妖怪一样。因此,如果我按住一个键,玩家会不断移动,如果我按一次键,他只需移动一次32px。
这是我目前的移动功能:
public void movePlayer(String keyDown, GameTime gameTime)
{
if (keyDown == "up")
{
playerPosition.Y -= 2;
//Animation part, with a timer to switch animation
if (time > 0)
{
directionSprite = directionSpriteTab[4];
time -= gameTime.ElapsedGameTime.Milliseconds;
time2 = interval;
}
if (time2 > 0 && time <= 0)
{
directionSprite = directionSpriteTab[5];
time2 -= gameTime.ElapsedGameTime.Milliseconds;
}
if (time2 <= 0 && time <= 0)
{
time = interval;
}
}
//same for other keys ...
}
使用该代码我的播放器移动顺畅,但是当我停止按下一个键时,他在两个Tiles之间停止并且它真的很烦人的碰撞,例如当我想进入一个房子时,门是32px大而且很难制作玩家进入。
答案 0 :(得分:0)
你可以添加布尔变量或枚举来保持玩家移动的状态(枚举可能更好但是为了简单起见,我将在示例中使用bool)。例如
bool movingUp = false; // Set to true when keyDown == "up".
然后您可以使用if语句检查播放器是否位于下一个磁贴的坐标处。如果他不是,继续更新playerPosition.Y。像这样的东西
if (keyDown == "up")
{
movingUp = true;
// Could be type Vector2 or something else, depends on what playerPosition is.
playerMoveDestination = new Point(playerPosition.X, playerPosition.Y - 32);
}
if (movingUp)
{
// Calculates the distance from the destination tile.
// When the distance is small enough you want to snap to the tile to avoid
// overshooting. In this case the snap distance is less than 2 pixels because the
// character moves 2 pixels per frame.
if (playerPosition.Y - playerMoveDestination.Y < 2)
{
playerPosition.Y = playerMoveDestination.Y;
movingUp = false;
}
else
{
playerPosition.Y -= 2;
if (time > 0)
{
directionSprite = directionSpriteTab[4];
time -= gameTime.ElapsedGameTime.Milliseconds;
time2 = interval;
}
if (time2 > 0 && time <= 0)
{
directionSprite = directionSpriteTab[5];
time2 -= gameTime.ElapsedGameTime.Milliseconds;
}
if (time2 <= 0 && time <= 0)
{
time = interval;
}
}
}
现在,如果玩家放开了向上键,它将继续移动角色,直到它到达下一个图块。
编辑:抱歉,我的代码中有错误。当玩家正好在一个牌上时,playerPosition.Y%32评估为0,小于2,所以当按下向上键时会立即执行并停止移动。要解决此问题,您可以使用playerMoveDestination变量,当玩家按下向上键时,它会设置为(playerPosition.X,playerPosition.Y - 32)。我更改了代码来执行此操作。
答案 1 :(得分:0)
现在Sprite正常移动但没有Tile to Tile ......如果我释放他停止的键并且没有继续向方向区块移动,他会像以前一样移动。
当我添加“向下”键部分时,它会产生奇怪的动作x)
这是带有“向下”的代码你能看出我做错了吗?
public void movePlayer(String keyDown, GameTime gameTime)
{
if (keyDown == "up")
{
movingUp = true;
// Could be type Vector2 or something else, depends on what playerPosition is.
playerMoveDestination = new Vector2(playerPosition.X, playerPosition.Y - 32);
}
if (movingUp)
{
// Calculates the distance from the destination tile.
// When the distance is small enough you want to snap to the tile to avoid
// overshooting. In this case the snap distance is 2 or less pixels because the
// character moves 2 pixels per frame.
if (playerPosition.Y - playerMoveDestination.Y < 2)
{
playerPosition.Y = playerMoveDestination.Y;
movingUp = false;
}
else
{
playerPosition.Y -= 2;
}
}
if (keyDown == "down")
{
movingDown = true;
// Could be type Vector2 or something else, depends on what playerPosition is.
playerMoveDestination = new Vector2(playerPosition.X, playerPosition.Y + 32);
}
if (movingDown)
{
// Calculates the distance from the destination tile.
// When the distance is small enough you want to snap to the tile to avoid
// overshooting. In this case the snap distance is 2 or less pixels because the
// character moves 2 pixels per frame.
if (playerPosition.Y + playerMoveDestination.Y < 2)
{
playerPosition.Y = playerMoveDestination.Y;
movingDown = false;
}
else
{
playerPosition.Y += 2;
}
}
}
我初始化了“playerMoveDestination”和movingDown / Up bool:
bool movingUp = false; // Set to true when keyDown == "up".
bool movingDown = false;
Vector2 playerMoveDestination;
我将它们放在“movePlayer”功能之上。
再次感谢您的帮助!