gpt4 book ai didi

java - 数组填充为 'null' - LibGDX

转载 作者:行者123 更新时间:2023-12-01 13:10:07 27 4
gpt4 key购买 nike

在我的 LibGDX 游戏中,我试图找出一种基于 .PNG 图像文件中的像素创建 map 的方法。我使用 BufferedImage 类将图像中的 RGB 值转换并存储到数组中,然后根据其值返回一个 Tile。但是,包含所有 Tiles 的数组只是用“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”。

下面的代码位于一个单独的类中,我在其中调用生成方法并将图 block 数组存储在迭代器中。在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]);
}

渲染方法

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

最佳答案

您的图 block 是有条件填充的(这意味着它们有可能未填充)

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));
}
}

关于java - 数组填充为 'null' - LibGDX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22943644/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com