简体   繁体   English

如何从精灵表访问不同的行?

[英]How to access a different row from a sprite sheet?

I am using a tutorial to understand how sprites work using a draw() method as well as using a gameloop.我正在使用教程来了解精灵如何使用 draw() 方法以及使用游戏循环来工作。 I adjusted the code as far as I understand it for my own project.我根据自己的项目对代码进行了调整。

The question I have is how can I access a different row of my sprite sheet besides the second row.我的问题是如何访问除第二行之外的精灵表的另一行。 My sprite sheet has 9 columns and 20 rows.我的精灵表有 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);
    }
}

How do I gain access to the second row and how can I change it for instance to the third or 4th row ?如何访问第二行以及如何将其更改为第三行或第四行?

What I understand so far is that using a sprite as an object as a bitmap instead via imageview the code implementation of the code works differently.到目前为止我所理解的是,使用精灵作为对象作为位图而不是通过 imageview 代码的代码实现方式不同。 Is there any advice on how to access a different row for the sprite sheet ?关于如何访问精灵表的不同行有什么建议吗? I used the android documentation as well as the tutorial to understand this process as far as I can.我尽可能使用 android 文档和教程来理解这个过程。

Here is the tutorial also: http://www.edu4java.com/en/androidgame/androidgame4.html这里也是教程: http : //www.edu4java.com/en/androidgame/androidgame4.html

From what I can see you are iterating through the first row, drawing each sprite in turn, perhaps to create an animation.从我所看到的,您正在遍历第一行,依次绘制每个精灵,也许是为了创建动画。

If you want to draw the animation for the second row, you can simply add 'height' to 'srcY'.如果要为第二行绘制动画,只需将“高度”添加到“srcY”即可。 It looks like you had this idea but you substracted instead of adding.看起来你有这个想法,但你减去而不是添加。 Remember that Y-coordinates on computers go from top to bottom (ie (0,0) is top-left, (0, 10) is 10 pixels lower).请记住,计算机上的 Y 坐标是从上到下的(即 (0,0) 是左上角,(0, 10) 是低 10 个像素)。

Likewise for the third and fourth rows, just add 'height' to 'srcY' again.同样,对于第三行和第四行,只需再次将 'height' 添加到 'srcY'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM