[英]How to get subImages from a sprite sheet with different sprite sizes?
[英]How to access a different row from a sprite sheet?
我正在使用教程來了解精靈如何使用 draw() 方法以及使用游戲循環來工作。 我根據自己的項目對代碼進行了調整。
我的問題是如何訪問除第二行之外的精靈表的另一行。 我的精靈表有 9 列和 20 行。
public class Sprite implements Drawable {
private static final int BMP_COLUMNS = 9;
private static final int BMP_ROWS = 20;
private int x = 0;
private int y = 0;
private int xSpeed = 5;
private Bitmap bmp;
float fracsect = 30;
private GameContent gameContent;
private int currentFrame = 0;
private int width;
private int height;
public Sprite(GameContent gameContent, Bitmap bmp) {
this.gameContent = gameContent;
this.bmp = bmp;
this.width = bmp.getWidth() / BMP_COLUMNS;
this.height = bmp.getHeight() / BMP_ROWS;
}
@Override
public void update(float fracsec) {
if (x > gameContent.getGameWidth() - width - xSpeed) {
xSpeed = -5;
}
if (x + xSpeed < 0) {
xSpeed = 5;
}
x = x + xSpeed;
currentFrame = ++currentFrame % BMP_COLUMNS;
}
@Override
public void draw(Canvas canvas) {
update(fracsect);
int srcX = currentFrame * width;
int srcY = 1*height - 41;
Rect src = new Rect(srcX +20 , srcY,srcX + width,srcY + height-38); // Generates
Rect dst = new Rect(x,y,x+width -30, y+height-30); // Scales
canvas.drawBitmap(bmp, src, dst, null);
}
}
如何訪問第二行以及如何將其更改為第三行或第四行?
到目前為止我所理解的是,使用精靈作為對象作為位圖而不是通過 imageview 代碼的代碼實現方式不同。 關於如何訪問精靈表的不同行有什么建議嗎? 我盡可能使用 android 文檔和教程來理解這個過程。
這里也是教程: http : //www.edu4java.com/en/androidgame/androidgame4.html
從我所看到的,您正在遍歷第一行,依次繪制每個精靈,也許是為了創建動畫。
如果要為第二行繪制動畫,只需將“高度”添加到“srcY”即可。 看起來你有這個想法,但你減去而不是添加。 請記住,計算機上的 Y 坐標是從上到下的(即 (0,0) 是左上角,(0, 10) 是低 10 個像素)。
同樣,對於第三行和第四行,只需再次將 'height' 添加到 'srcY'。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.