在 Java 中阅读精灵表的最佳方式是什么? [英] What's the best way of reading a sprite sheet in Java?

查看:44
本文介绍了在 Java 中阅读精灵表的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基本的精灵引擎,以供自己娱乐并更好地熟悉 Java 的 2d API.目前,我正在使用大量具有透明背景的单独 .png 文件来表示我需要的各种精灵和不同的动画帧.大多数真实世界"游戏开发项目似乎都使用精灵表",其中在单个文件中包含多个精灵或动画帧.此外,人们通常会指定一种没有出现在精灵调色板中的任意颜色作为透明颜色,而不是使用原生图像透明度支持.如何以编程方式管理这样的文件?

I'm writing a basic sprite engine for my own amusement and to get better aquainted with Java's 2d API. Currently I am making use of large numbers of separate .png files with transparent backgrounds to represent the various sprites and different frames of animation that I need. Most 'real world' game development projects seem to make use of 'sprite sheets' which contain multiple sprites or frames of animation within a single file. Also, rather than making use of native image transparency support, people often nominate an arbitrary colour that does not appear in the sprite pallette to be the transparent colour. How does one manage a file like this programatically?

  • 你怎么知道一个精灵在哪里开始,下一个开始
  • 您如何处理透明度问题

这里可能还有其他我没有想到的因素,所以当我想到事情或人们提出建议时,我可能会添加到上面的列表中(请在评论中添加).

There may be other factors that I've not thought of here, so I may add to the list above as I think of things or as people make suggestions (please do so in the comments).

推荐答案

我目前使用由简单精灵编辑器生成的 XML 文件,该文件将精灵存储为(可选动画)姿势的集合,而姿势又是帧的集合或细胞.帧存储每帧信息,如表单中帧的 x 和 y 偏移、单元格宽度和高度以及任何转换(调整大小/旋转/色调/等).姿势存储单个帧和动画信息(例如速度),以及在程序中轻松识别它们的姿势名称(hero.pose = sprite.pose["standing_right"]).精灵作为文档根来保存多个姿势,例如每个面向方向的姿势.

I currently use XML files generated by a simple sprite editor that store the sprite as a collection of (optionally animated) poses, which are in turn a collection of frames or cells. Frames store per-frame information like the x and y offset of the frame in sheet, cell width and height, and any transformation (resize/rotation/hue/etc.). Poses store individual frames and animation information (speed, for example), and a pose name to easily identify them in the program (hero.pose = sprite.pose["standing_right"]). Sprites serve as a document root to hold several poses, such as a pose for each facing direction.

我之前使用的一种不太灵活的替代方法是为单元格和工作表指定固定大小,并根据这些值计算帧偏移和大小(例如,宽度始终为 32 像素,因此第三个精灵为 32 * 2).后来我开始在文件名(例如 sprite_name_32x64.png)中为不适合固定单元尺寸的精灵指定这些尺寸.我更喜欢这种新方法,尤其是它有一个简单的编辑器,它为我填充了大多数值,并允许我将精灵用作其他精灵的模板.

A less flexible alternative I used earlier was to specify fixed sizes for cells and sheets and calculate frame offsets and sizes based on these values (e.g. width is always 32 pixels, so third sprite is at 32 * 2). Later I started specifying these dimensions in the file name (e.g. sprite_name_32x64.png) for sprites that don't fit the fixed cell dimensions. I like the new approach more, especially with a simplistic editor that fills most values for me and allows me to use sprites as templates for other sprites.

我直接使用存储在 PNG 图像中的 alpha 和透明度信息,所以我不需要担心将它存储在其他地方,尽管其他方法是为每个精灵选择一个固定值并存储在某个地方,使用最左边的像素如果您知道它总是空的,请使用特定的调色板条目,如果您正在使用这些、精灵蒙版或您拥有的任何东西.

I use the alpha and transparency information stored in PNG images directly so I don't need to worry about storing it elsewhere, although other approaches would be to pick a fixed value per sprite and store somewhere, use the leftmost pixel in the pose if you know it's always empty, use a specific palette entry if you're using those, sprite masks, or what have you.

这篇关于在 Java 中阅读精灵表的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆