需要帮助建议通过键盘输入实现动画更改

Posted

技术标签:

【中文标题】需要帮助建议通过键盘输入实现动画更改【英文标题】:Need help in suggestion for implementing animation changes via keyboard input 【发布时间】:2017-01-17 10:43:18 【问题描述】:

我是新来的,在编码方面并不是最好的。 (为此使用 XNA 的单游戏变体) 我已经成功地重新创建了一个类来为我制作的精灵表设置动画,但我遇到的问题是,当我尝试实现按键和动画时,动画停止移动(卡在第一帧)

我的更新类看起来像这样

previousKBState = currentKBState;
        currentKBState = Keyboard.GetState();

        if (previousKBState.IsKeyDown(Keys.D))
        
            playerSprite.LoadGraphic(Content.Load<Texture2D>("Sprites\\walk_right.png"), 5, 15, 672, 631, 30);
        
        else if (previousKBState.IsKeyDown(Keys.A))
        
            playerSprite.LoadGraphic(Content.Load<Texture2D>("Sprites\\walk_left.png"), 5, 15, 672, 631, 30);
        
        else
        
            playerSprite.LoadGraphic(Content.Load<Texture2D>("Sprites\\idle_right.png"), 5, 10, 610, 423, 5);
        

这是我的 LoadGraphic 类

public void LoadGraphic(Texture2D texture, int rows, int columns, int width, int height, int animationSpeed)
    
        this.Texture = texture;
        this.rows = rows;
        this.columns = columns;
        this.width = width;
        this.height = height;
        this.animationSpeed = (float)1 / animationSpeed;

        totalElapsed = 0;
        currentRow = 0;
        currentColumn = 0;
    

我的绘画课

public void Draw(SpriteBatch spriteBatch, Vector2 position, Color color)
       
    spriteBatch.Draw(Texture,
        new Rectangle((int)position.X, (int)position.Y, width, height),
        new Rectangle(currentColumn * width, currentRow * height, width, height), color);
    

所以我认为我遇到的问题是,当我按住 D 或 A 键时,LoadGraphics 代码行会在动画播放之前不断刷新对另一个类的调用。

如果有人可以给我任何解决此问题的建议,将会有所帮助 谢谢

【问题讨论】:

【参考方案1】:

你是对的,你正在通过每帧调用LoadGraphic 来将所有值重置为 0。您需要检查是否已经播放了正确的动画,在这种情况下什么也不做。

另外,我会避免从您的更新代码中调用Content.Load&lt;Texture2D&gt;()。我会在LoadContent() 方法中加载所有纹理,然后在必要时使用它们。

【讨论】:

谢谢你的意见,我在LoadContent()中加载后才发现

以上是关于需要帮助建议通过键盘输入实现动画更改的主要内容,如果未能解决你的问题,请参考以下文章

自定义数字键盘组件化方案

隐藏 Ipad 的键盘,但仍然可以通过自定义键盘进入输入字段

联想手机A65打电话时不显示软键盘怎么设置出来?

角度材料自动完成 - 如何防止键盘输入以在建议面板中选择一个选项

iOS 键盘扩展 - 向标准键盘添加按钮(系统范围的输入附件视图)

以编程方式更改键盘输入语言