Xna 如何制作像口袋妖怪这样的老式精灵动作(瓷砖对瓷砖)
我正在做一个类似口袋妖怪的游戏,我需要让我的精灵像口袋妖怪的游戏一样移动。 我的意思是,一块一块地,一个单元格一个单元格地。我正在使用tIDE(一个平铺贴图编辑器),我的平铺宽度是32px。我希望播放器在移动过程中每32px移动32px,并带有动画。就像口袋妖怪一样。因此,如果我按住一个键,玩家会不断移动,如果我按一次键,他只移动一次,所以32px 以下是我当前的移动功能:Xna 如何制作像口袋妖怪这样的老式精灵动作(瓷砖对瓷砖),xna,sprite,game-physics,Xna,Sprite,Game Physics,我正在做一个类似口袋妖怪的游戏,我需要让我的精灵像口袋妖怪的游戏一样移动。 我的意思是,一块一块地,一个单元格一个单元格地。我正在使用tIDE(一个平铺贴图编辑器),我的平铺宽度是32px。我希望播放器在移动过程中每32px移动32px,并带有动画。就像口袋妖怪一样。因此,如果我按住一个键,玩家会不断移动,如果我按一次键,他只移动一次,所以32px 以下是我当前的移动功能: public void movePlayer(String keyDown, GameTime gameTime)
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 ...
}
public void movePlayer(字符串向下键,游戏时间游戏时间)
{
如果(按键向下=“向上”)
{
玩家位置Y-=2;
//动画部分,用定时器切换动画
如果(时间>0)
{
directionSprite=directionSpriteTab[4];
时间-=gameTime.ElapsedGameTime.ms;
时间2=间隔;
}
如果(time2>0&&time,您可以添加布尔变量或枚举来保存玩家的移动状态(枚举可能更好,但为了简单起见,我将在示例中使用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;
}
}
}
if(向下键==“向上”)
{
movingUp=true;
//可能是Vector2类型或其他类型,取决于播放位置。
playerMoveDestination=新点(playerPosition.X,playerPosition.Y-32);
}
如果(移动)
{
//计算与目标磁贴的距离。
//当距离足够小时,您希望捕捉到平铺以避免
//超调。在这种情况下,捕捉距离小于2像素,因为
//角色每帧移动2像素。
if(playerPosition.Y-playerMoveDestination.Y<2)
{
playerPosition.Y=playerMoveDestination.Y;
movingUp=false;
}
其他的
{
玩家位置Y-=2;
如果(时间>0)
{
directionSprite=directionSpriteTab[4];
时间-=gameTime.ElapsedGameTime.ms;
时间2=间隔;
}
如果(time2>0&&time现在精灵正常移动,但不是平铺到平铺…他像以前一样移动,如果我松开键,他会停止,不会继续平铺方向
当我加上“向下”键时,它会做出奇怪的动作(x)
这是带“down”的代码你能看到我是否做错了什么吗
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;
}
}
}
我把它们放在“movePlayer”函数之上
再次感谢您的帮助!更改了答案以修复错误。如果对您有效,请接受答案。我们需要更多类似口袋妖怪的游戏:)Ty感谢您的帮助,我认为游戏不会完成。^^“这只是学校的一个项目。我回答了您仍然有一些问题:p
bool movingUp = false; // Set to true when keyDown == "up".
bool movingDown = false;
Vector2 playerMoveDestination;