首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从包含行和列的SpriteSheet中获取Sprite

从包含行和列的SpriteSheet中获取Sprite
EN

Game Development用户
提问于 2016-08-07 06:06:40
回答 2查看 6.7K关注 0票数 3

我在读取带有行和列的spritesheet时遇到了困难。如果它只是一行,那么我的代码可以正常工作。这张纸是这样的:

来源:人物动画,衣服,盔甲,武器,骷髅敌人,战斗假人

现在,它只适用于第一行。我想不出怎么把它移到下一行去。我知道我必须把我的SourceRect换成xy,但不知道是什么。

这是我的密码:

代码语言:javascript
复制
class SpriteSheet
{
    public Texture2D Texture { get; set; }
    public Vector2 Origin { get; set; }
    public Rectangle SourceRect { get; set; }
    public Vector2 position;

    private float timer, interval;
    private int currentFrame;
    private int spriteWidth, spriteHeight, spriteSpeed;
    private int rows, columns;
    private KeyboardState currentKBState, previousKBState;

    public SpriteSheet(Texture2D texture, int spriteWidth, int spriteHeight, int rows = 1, int columns = 0, int currentFrame = 1)
    {
        Texture = texture;
        this.currentFrame = currentFrame;
        this.spriteWidth = spriteWidth;
        this.spriteHeight = spriteHeight;
        this.rows = rows;
        this.columns = columns;

        spriteSpeed = 100;
        interval = 200;
    }

    // Handle movement of sprite
    public void Update(GameTime gameTime)
    {
        previousKBState = currentKBState;
        currentKBState = Keyboard.GetState();

        // Get the sprite from the sheet
        // THIS LINE NEEDS TO BE CHANGED I THINK
        SourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);

        if(currentKBState.GetPressedKeys().Length == 0)
        {
            if (currentFrame > 0 && currentFrame < 9)
                currentFrame = 0;

            if (currentFrame > 9 && currentFrame < 18)
                currentFrame = 9;

            if (currentFrame > 18 && currentFrame < 27)
                currentFrame = 18;

            if (currentFrame > 27 && currentFrame < 36)
                currentFrame = 27;
        }

        // Animate Up
        if (currentKBState.IsKeyDown(Keys.Up))
        {
            AnimateUp(gameTime);

            // Check boundary
            if (position.Y > 25)
                position.Y -= (float) (spriteSpeed * gameTime.ElapsedGameTime.TotalSeconds);
        }
    }

    private void AnimateUp(GameTime gameTime)
    {
        if (currentKBState != previousKBState)
            currentFrame = 1;

        timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

        if(timer > interval)
        {
            currentFrame++;
            if (currentFrame > 8)
                currentFrame = 0;

            timer = 0f;
        }
    }
}
EN

回答 2

Game Development用户

发布于 2016-08-07 07:49:57

你的y坐标总是0,所以只使用第一行.您需要更改这一行:

代码语言:javascript
复制
SourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);

如下所示(参见第二个参数):

代码语言:javascript
复制
SourceRect = new Rectangle(currentFrame * spriteWidth, wantedRow * spriteHeight, spriteWidth, spriteHeight);
票数 3
EN

Game Development用户

发布于 2016-08-07 18:15:18

因此,我对代码做了一些修改,以使其工作:

构造函数更改:

代码语言:javascript
复制
public SpriteSheet(Texture2D texture, int rows = 1, int columns = 0, int currentFrame = 0)
{
    this.texture = texture;
    this.sheetRows = rows;
    this.sheetColumns = columns;

    // Default = top left
    this.currentFrame = currentFrame;

    // Get a single sprite's width and height
    sheetWidth = texture.Width / sheetColumns;
    sheetHeight = texture.Height / sheetRows;
}

我注意到我不需要手动提供雪碧的widthheight,我可以从雪碧表中得到它。我现在在移动方法中处理spriteSpeedinterval,而不是构造函数。

我将SourceRect行从Update方法中移除,并将其移动到自定义的Draw方法中:

代码语言:javascript
复制
public void Draw(SpriteBatch spriteBatch)
{
    // Get the correct frame
    spriteRow = currentFrame / sheetColumns;
    spriteColumn = currentFrame % sheetColumns;

    sourceRect = new Rectangle(sheetWidth * spriteColumn, sheetHeight * spriteRow, sheetWidth, sheetHeight);
    origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);

    spriteBatch.Draw(texture: texture, sourceRectangle: sourceRect, position: position, origin: origin, scale: new Vector2(1.0f));
}

最后,获得雪碧的rowcolumn变得更加容易,只要稍微划分一下。

如果我想要row 3column 1currentFrame就是18。知道了这一点,修复代码中没有按键的地方重置currentFrame的部分是非常简单的。

票数 0
EN
页面原文内容由Game Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://gamedev.stackexchange.com/questions/127767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档