0

我想要实现的是一个逼真的步行动画,其中角色处于静态帧,然后Right按下它会加载我创建的角色动画。

我已经尝试过解决这个问题,到目前为止,我得到的只是错误。
这是我的代码:
雪碧

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);

    }

动画精灵

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;
    }

播放器精灵。

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));

//------------------------------------------------------------------------------------------------
4

1 回答 1

1

最好的方法是使用精灵表来制作动画。一张包含所有精灵的图像。然后通过更改currentFrame(从 0 到 7)简单地显示图像的这一部分。

sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight)

像这样画

SpriteBatch.Draw (Texture2D, Position, sourceRect, Color)

精灵表图像示例 在此处输入图像描述

于 2013-11-05T13:53:16.583 回答