在我的LibGDX游戏中,我试图找到一种基于.PNG图像文件中像素创建地图的方法。我正在使用BufferedImage类将图像中的RGB值转换并存储到Array中,然后Array根据其值返回Tile。但是,包含所有Tiles的Array只会填充“null
”值。
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.survivegameandroidtest.survive.player.Player;
import com.survivegameandroidtest.survive.tiles.ConcreteTile;
import com.survivegameandroidtest.survive.tiles.GrassTile;
public class Level {
Tile[] tiles;
int[] levelPixels;
AssetManager manager;
Player player;
SpriteBatch batch;
public Level(String path) {
try {
BufferedImage image = ImageIO.read(Level.class.getResource(path));
int width = image.getWidth();
int height = image.getHeight();
tiles = new Tile[width * height];
levelPixels = new int[width * height];
image.getRGB(0, 0, width, height, levelPixels, 0, width);
} catch(IOException e) {
e.printStackTrace();
System.out.println("Level could not load");
}
}
//GRASS = 0x24ff00
//CONCRETE = 0x8b8b8b
public void generateLevel() {
for(int i = 0; i < levelPixels.length; i++) {
if(levelPixels[i] == 0x24ff) tiles[i] = new GrassTile(manager.get("grass.png", Texture.class), new Vector2(i * 10, i * 10), new Vector2(10,10));
if(levelPixels[i] == 0x8b8b8b) tiles[i] = new ConcreteTile(manager.get("concrete.png", Texture.class), new Vector2(i * 10, i * 10), new Vector2(10,10));
}
}
public void update() {
}
public void draw(SpriteBatch batch) {
}
}
如果我添加System.out.println(tiles[1])
- 这将在控制台中打印'null'。
下面的代码在一个单独的类中,我调用generate方法并将tiles数组存储在Iterator中。在draw方法中,if语句用于确定Iterator中的当前Tile是否为null,如果是,则不会绘制/呈现tile。在游戏中,我的播放器只有一个白色屏幕,表示tiles[]
数组中的所有值都等于'null'。如果要删除if语句,我会得到NullPointerException
。
显示方法
level = new Level("/level.png");
tiles = new ArrayList<Tile>();
for(int i = 0; i < level.tiles.length; i++) {
level.generateLevel();
tiles.add(level.tiles[i]);
}
RENDER METHOD
tileIterator = tiles.iterator();
while(tileIterator.hasNext()) {
final Tile cur = tileIterator.next();
if(cur != null) {
cur.update();
cur.draw(batch);
}
}
如果我添加:
if (levelPixels[i] != 0x24ff || levelPixels[i] != 0x8b8b8b) {
tiles[i] = new GrassTile(manager.get("grass.png", Texture.class),
new Vector2(i * 10, i * 10),
new Vector2(10, 10));
}
然后我收到NullPointerException
。
答案 0 :(得分:1)
你的瓷砖是有条件填充的(这意味着它们有可能不会被填充)
public void generateLevel() {
for(int i = 0; i < levelPixels.length; i++) {
if(levelPixels[i] == 0x24ff) tiles[i] = new GrassTile(manager.get("grass.png", Texture.class), new Vector2(i * 10, i * 10), new Vector2(10,10));
if(levelPixels[i] == 0x8b8b8b) tiles[i] = new ConcreteTile(manager.get("concrete.png", Texture.class), new Vector2(i * 10, i * 10), new Vector2(10,10));
}
}
答案 1 :(得分:0)
问题出在levelPixels
数组。它应该以{{1}}方法填充,因此您应该在调用此方法后检查此数组的值是否不等于image.getRGB()
。无论如何,很明显这个数组的值都不等于0
或0x24ff
,这就是为什么0x8b8b8b
数组只是Level.tiles
的数组
请你试试以下内容:
nulls
还请初始化for (int i = 0; i < levelPixels.length; i++)
{
if (levelPixels[i] == 0x24ff) {
tiles[i] = new GrassTile(manager.get("grass.png", Texture.class),
new Vector2(i * 10, i * 10),
new Vector2(10, 10));
} else if (levelPixels[i] == 0x8b8b8b) {
tiles[i] = new ConcreteTile(manager.get("concrete.png", Texture.class),
new Vector2(i * 10, i * 10),
new Vector2(10, 10));
} else {
tiles[i] = new GrassTile(manager.get("grass.png", Texture.class),
new Vector2(i * 10, i * 10),
new Vector2(10, 10));
}
}
:manager