我想要实现的是一个逼真的漫步动画,其中一个角色处于静态帧,然后当按下右时,它将加载我创建的角色动画。
我已经尝试过搞乱这一点,到目前为止我所得到的都是错误
这是我的代码:
精灵
public class Sprite
{
public static SpriteManager spriteManager;
private int sectorNumber;
private string name;
private TextureData textureData;
private SpritePresentationInfo spritePresentationInfo;
private SpritePositionInfo spritePositionInfo;
public Sprite(string name, TextureData textureData,
SpritePresentationInfo spritePresentationInfo,
SpritePositionInfo spritePositionInfo)
{
this.name = name;
this.textureData = textureData;
this.spritePresentationInfo = spritePresentationInfo;
this.spritePositionInfo = spritePositionInfo;
//make sure we set first time around
this.sectorNumber = Collision.getSectorNumber(this.POSITIONINFO.BOUNDS);
}
AnimatedSprite
public class AnimatedSprite : Sprite
{
protected AnimatedTextureData animatedTextureData;
protected int frameRate, startFrame, currentFrame = 0;
protected bool bRepeatAnimation, bPause;
protected double timeSinceLastFrameInMs, timeBetweenFrameInMs;
#region PROPERTIES
#endregion
public AnimatedSprite(string name, AnimatedTextureData animatedTextureData,
SpritePresentationInfo spritePresentationInfo,
SpritePositionInfo spritePositionInfo,
int frameRate, int startFrame, bool bRepeatAnimation)
: base(name, animatedTextureData, spritePresentationInfo, spritePositionInfo)
{
this.animatedTextureData = animatedTextureData;// (AnimatedTextureData)animatedTextureData.Clone();
this.frameRate = frameRate;
timeBetweenFrameInMs = 1000.0 / frameRate; //time between each frame if they play at frameRate per second (e.g. 24fps gives timeBetweenFrameInMs = 1000ms/24 per second)
timeSinceLastFrameInMs = timeBetweenFrameInMs; //means no initial delay in animation
this.startFrame = startFrame;
currentFrame = startFrame;
this.bRepeatAnimation = bRepeatAnimation;
}
PlayerSprite。
public class PlayerSprite : Sprite
{
protected Keys leftKey, rightKey;
//private float moveIncrement = 0.5f;
private int move;
public PlayerSprite(string name, AnimatedTextureData textureData, SpritePresentationInfo spritePresentationInfo,
SpritePositionInfo spritePositionInfo, Keys leftKey, Keys rightKey)
: base(name, textureData, spritePresentationInfo, spritePositionInfo)
{
this.leftKey = leftKey;
this.rightKey = rightKey;
}
//-----------------------------------------------------------------------------------
// Use this if we do not want to use the parents
//-----------------------------------------------------------------------------------
public override void Update(GameTime gameTime)
{
handleInput(gameTime);
base.Update(gameTime);
}
//-----------------------------------------------------------------------------------
// Use this if we do not want to use the parents
//-----------------------------------------------------------------------------------
protected override void handleInput(GameTime gameTime)
{
this.move = (int)(GameData.PLAYER_MOVE_INCREMENT * gameTime.ElapsedGameTime.Milliseconds);
if (SpriteManager.GAME.KEYBOARDMANAGER.isKeyDown(leftKey))
MoveBy(-move, 0);
//This is where i want to initialize the walk animation.
if (SpriteManager.GAME.KEYBOARDMANAGER.isKeyDown(rightKey))
MoveBy(move, 0);
}
主。我没有按键主要工作和动画。
textureManager.Add("PlayerAnimation", "Assets\\Animations\\Characters\\Player\\Player_AnimationFinal", 8, 75,200);
//------------------------------------------------------------------------------------------------
//Player Animation
//------------------------------------------------------------------------------------------------
AnimatedTextureData playerAnimatedTextureData = (AnimatedTextureData)textureManager.Get("PlayerAnimation");
SpritePresentationInfo playerAnimatedPresentationInfo = new SpritePresentationInfo(playerAnimatedTextureData.FULLSOURCERECTANGLE, 0);
SpritePositionInfo playerAnimatedPositionInfo = new SpritePositionInfo(new Vector2(100, 700), playerAnimatedTextureData.Width(), playerAnimatedTextureData.Height(), 0, 2, playerAnimatedTextureData.CENTREORIGIN);
spriteManager.Add(new AnimatedSprite("PlayerAnimation", playerAnimatedTextureData, playerAnimatedPresentationInfo, playerAnimatedPositionInfo,10, 0, true));
//------------------------------------------------------------------------------------------------
答案 0 :(得分:1)
最好的方法是使用精灵表进行动画制作。一个包含所有精灵的单个图像。然后简单地通过更改currentFrame
(从0到7),您只显示图像的这一部分。
sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight)
并像这样画画
SpriteBatch.Draw (Texture2D, Position, sourceRect, Color)
精灵表图像的示例