我想创建一个 cookie-cutter 类型的类,它采用源矩形并动态地遍历精灵表中的每一帧,直到每个图像都被剪掉用于动画。当精灵表中的每个帧大小相同时,我可以做到这一点,但几乎不可能在具有相同帧大小的动画中找到具有任何复杂性的精灵表。例如:
就像我想要的那样被剪切和动画,但是:
由于它的帧大小可变,因此变得有趣(因为我的程序目前假设所有帧的大小都相同)。有没有办法在某种程度上感知精灵表中每个单独帧的帧大小,或者这是一个失败的原因?
当前创建“cookie切割器”源框架的代码:
// 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);
}
}
因为我不是艺术家,所以我的目标是使用预渲染的精灵表,以便我可以在 2D 动画环境中提高我的技能。问题是,我发现的大多数精灵表似乎都有可变帧,这使得它们对我来说相当无用,除非我能找到一种更精确地切割它们的方法(或另一种切割方法,是否有我缺少的 API 工具? )