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

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

问题描述

我正在写一个基本的精灵引擎为自己的娱乐,以获得更好的与Java的2D API aquainted。目前,我利用大量的具有透明背景的独立.png文件重新present的各种精灵和动画不同的帧,我需要。大多数的真实世界的游戏开发项目似乎要利用精灵表包含在一个单一的文件中的多个精灵或动画帧。此外,而不是利用原生图像透明度的支持,人们常常提名没有出现在精灵调色板是透明色任意颜色。一个人如何处理这样的文件编程?

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?

  • 你怎么知道其中一个精灵 开始,下一个开始
  • 你是怎么处理的透明度
  • how do you know where one sprite starts and the next begins
  • how do you deal with transparency

有可能是我没有想到的在这里等因素的影响,所以我可能会添加到列表上面,因为我想的事情或人提出建议(请在评论中这样做)。

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天全站免登陆