gpt4 book ai didi

java - 将 Sprite 表动态切割成具有可变帧大小的单独位图

转载 作者:行者123 更新时间:2023-12-01 04:11:22 25 4
gpt4 key购买 nike

我想创建一个千篇一律的类,它采用源矩形并动态地逐步遍历 Sprite 表中的每个帧,直到每个图像都被剪切为动画。当 Sprite 表中的每个帧大小都相同时,我可以毫无问题地做到这一点,但几乎不可能在具有相同帧大小的动画中找到具有任何复杂性的 Sprite 表。例如:

Simple sprite sheet with constant frame size

按照我想要的方式进行剪切和动画处理,但是:

More complex bitmap with variable frame size

由于它的帧大小可变,因此会变得很有趣(因为我的程序当前假设所有帧的大小相同)。有没有办法以某种方式感知 Sprite 表中每个单独帧的帧大小,或者这是一个失败的原因?

创建“千篇一律”源框架的当前代码:

// Indirect Variable Sets (Sprite Animation and Sprite Sheet Cuts)
framesPerRow = frameCount/spriteSheetRows;
spriteWidth = bmp.getWidth() / framesPerRow; // cut the sheet into pieces based on
// sprite width: frames/row ratio
spriteHeight = bmp.getHeight()/spriteSheetRows; // cut the sheet horizontally into pieces based on
// total height : total rows ratio

setSourceRect(new Rect(0, 0, spriteWidth, spriteHeight));
setFramePeriod(1000 / fps); // set the framePeriod based on desired fps

然后在我的更新方法中使用它:

public void update(long gameTimeInMillis){
// If the game time has been longer than the frame period...
// the reason that we need frameTicker is so that we can use our variable (frameTicker)
// to keep track of the last time that the frame was updated (relative to our game)
if (gameTimeInMillis > frameTicker + framePeriod){
frameTicker = gameTimeInMillis; // set last update time (current time)
// increment the animation frame
currentFrame++;

// Get current column in sprite sheet:
// this works like this, imagine we are at frame 20, and we have 5 frames per row
// 20%5 = 0 so we are at the end of the row, if we are at frame 22, 22%5 = 2, etc.
frameColumn = currentFrame%framesPerRow;

// if we are at our max frame count (note, we start at 0) then reset our animation
if(currentFrame >= frameCount){
currentFrame = 0;
}

// increment the sprite sheet row if we are at the end of the row
if(frameColumn == 0){
currentRow++;
}

// if we are at our max rows (note, we start at 0) then reset our animation rows
if(currentRow >= spriteSheetRows){
currentRow = 0;
}

// define the "cookie cutter" sourceRectangle for our sprite sheet
this.sourceRect.left = frameColumn * spriteWidth;
this.sourceRect.right = this.sourceRect.left + spriteWidth;
this.sourceRect.top = currentRow * spriteHeight;
this.sourceRect.bottom = this.sourceRect.top + spriteHeight;
Log.d(TAG, "Top coordinates = " + currentRow);
}
}

由于我不是艺术家,所以我的目标是使用预渲染的 Sprite 表,以便我可以在 2D 动画环境中发挥我的技能。问题是,我发现的大多数 Sprite 表似乎都有可变框架,这使得它们对我来说相当无用,除非我能找到一种更精确地切割它们的方法(或另一种切割方法,我是否缺少 API 工具? )

最佳答案

您可以添加一些元数据与 Sprite 表一起定义像素位置以及表中每个帧的宽度/高度。这比拥有完美的网格需要更多的工作,但它将使您能够按原样使用更广泛的 Sprite 表。例如(不按比例)如果您去获取第 1 帧,您会发现它从像素位置 128 开始为 16x32,并且您可以根据整个纸张的宽度/高度计算纹理坐标。这听起来很困惑,但是一旦你让它工作起来,它应该会给你带来更大的灵 active ,并且可以在各种 Sprite 表中重复使用。

否则,您可以使用 Gimp 将各个帧剪切并粘贴到网格上。或者,将帧切割成具有相同宽度/高度的单个图像,然后使用 ImageMagick 之类的工具将单个图像组装成 Sprite 表。我不记得实际的命令,但我在命令行中使用 ImageMagick 将 Reiner 的一些 Tilesets 组装成 Sprite 表。 ImageMagick 会将目录中的所有图像连接起来,您甚至可以告诉它为您调整它们的大小,以便您可以从更高分辨率的图像开始,并在需要时强制它们为 2 的幂。

关于java - 将 Sprite 表动态切割成具有可变帧大小的单独位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19881635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com