当按下不同的键时如何更改精灵表中的帧。 XNA

Posted

技术标签:

【中文标题】当按下不同的键时如何更改精灵表中的帧。 XNA【英文标题】:How to change frames in sprite sheet when different keys are pressed. XNA 【发布时间】:2013-11-15 12:20:30 【问题描述】:

我想要实现的是一个逼真的行走动画,其中角色处于静态帧,然后当按下 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));

//------------------------------------------------------------------------------------------------

【问题讨论】:

没有人会阅读所有这些内容,请将您的问题缩小到特定问题。 【参考方案1】:

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

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

像这样画

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

精灵表图像示例

【讨论】:

以上是关于当按下不同的键时如何更改精灵表中的帧。 XNA的主要内容,如果未能解决你的问题,请参考以下文章

按下美元键时如何找出在 keyup 或 keypress 事件上按下的键

当按下umlaut键时,Python Tkinter触发keyevent

Delphi 常用属性说明(超长)

更改键盘挂钩回调中的键盘布局

按下键盘中的返回键时,UITextView 不记得字体样式

XNA精灵排序模式