简体   繁体   English

如何从具有不同精灵大小的精灵表中获取subImage?

[英]How to get subImages from a sprite sheet with different sprite sizes?

I'm doing a fighting game in Java, and I'm having some trouble with the animations. 我正在用Java进行格斗游戏,并且在动画方面遇到了一些麻烦。

The thing is when a player is idle or walking, I've no problem because every sprite has a fixed width (50px), but when the player attacks the sprite becomes wider as long as the punch goes further. 问题是当玩家闲置或行走时,我没问题,因为每个子画面都有固定的宽度(50像素),但是当玩家攻击时,只要拳头越远,子画面就会越宽。 And in-game the sprite doesn't draw correctly. 在游戏中,子画面无法正确绘制。

PS: I'm saving every frame/sprite inside an arraylist, and I change MAXFRAMES manually depending on the number of sprites. PS:我将每个帧/子画面保存在arraylist中,然后根据子画面数手动更改MAXFRAMES。 SS is the spritesheet. SS是Spritesheet。

I'm using getSubImage method as below. 我正在使用如下的getSubImage方法。

public void setAnimacionIdleRight(BufferedImage ss) {
    this.animation_idle_right = new ArrayList<BufferedImage>();

    BufferedImage tmp = ss.getSubimage(0, 0, 350, 100);
    BufferedImage subtmp = null;
    int k = 1;
    for(int i = 1; i < MAXFRAMES; i++) {
        subtmp = tmp.getSubimage((CELLWIDTH * i) + k - CELLWIDTH, 1, CELLWIDTH, CELLHEIGHT);            
        this.animation_idle_right.add(subtmp);
        k++;
    }
}

SpriteSheet example: SpriteSheet示例:

例

Any Ideas? 有任何想法吗? Or is the solution brute force img loading? 还是解决方案蛮力img加载?

As sugested by MadProgrammer, you can manually set the size of each sprite in the sheet. 正如MadProgrammer的建议那样,您可以手动设置图纸中每个精灵的大小。 Since the height is always the same, then you can simply have an array with the size of the number of sprites on the sheet, and put only sprite width in it. 由于高度总是相同的,因此您可以简单地在纸上放置一个具有精灵数量的数组,并仅将精灵宽度放入其中。

To help you you could do a loop before manually setting the values like this: 为了帮助您,您可以在手动设置以下值之前先做一个循环:

float cellWidths[MAX_FRAMES/CELLNUMBER];
for(int i=0; i<MAX_FRAMES/CELLNUMBER;i++){
    cellWidths[i] = CELLWIDTH;
}
//set non default values manually.

and then you would only need to change you for like this: 然后您只需要像这样更改您:

for(int i = 1; i < MAXFRAMES; i++) {
    subtmp = tmp.getSubimage((cellWidths[i-1] * i) + k - cellWidths[i-1], 1, cellWidths[i-1], CELLHEIGHT);            
    this.animation_idle_right.add(subtmp);
    k++;
}

But i may be misunderstanding what MAX_FRAMES mean. 但是我可能会误解MAX_FRAMES的含义。

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

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