我有以下课程:
public class Food extends AbstractActor {
private float width;
private float height;
public Food() {
super(new Texture("food.png"));
}
@Override
public void update(float dt) {
}
public static Food fromJSON(JSONObject json) {
Food food = new Food();
try {
food.setPosition((float)json.getDouble("x"), (float)json.getDouble("y"));
food.setWidth((float)json.getDouble("width"));
food.setHeight((float)json.getDouble("height"));
} catch(Exception e) {}
return food;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
}
和AbstractActor:
public abstract class AbstractActor {
private Texture texture;
private float x;
private float y;
public AbstractActor(Texture texture) {
this.texture = texture;
}
public void setPosition(float x, float y) {
this.x = x;
this.y = y;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public void setTexture(Texture texture) {
this.texture = texture;
}
public Texture getTexture() {
return texture;
}
public abstract void update(float dt);
public void render(SpriteBatch sb) {
sb.begin();
sb.draw(texture, getX(), getY());
sb.end();
}
public void dispose() {
getTexture().dispose();
}
}
image food.png确实存在并存在于assets文件夹中。 问题是,几乎每个纹理都呈现为黑色矩形。任何想法如何解决这一问题? (我在Android设备上运行它)
修改 更多代码:
public class GdxGame extends ApplicationAdapter {
public static final int WIDTH = 860;
public static final int HEIGHT = 480;
public static final String TITLE = "Fishy";
private static OrthographicCamera camera;
private GameStateManager gsm;
private SpriteBatch batch;
@Override
public void create () {
camera = new OrthographicCamera(WIDTH, HEIGHT);
camera.translate(WIDTH/2, HEIGHT/2);
batch = new SpriteBatch();
gsm = new GameStateManager();
gsm.push(new MenuState(gsm));
}
@Override
public void render () {
camera.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
batch.setProjectionMatrix(camera.combined);
gsm.render(batch);
}
public static OrthographicCamera getCameraInstance() {
return camera;
}
}
GameStateManager:
public class GameStateManager {
private Stack<State> states;
public GameStateManager() {
states = new Stack<State>();
}
public void push(State state) {
states.push(state);
}
public void pop() {
states.pop();
}
public void set(State state) {
states.pop();
states.push(state);
}
public void update(float dt) {
states.peek().update(dt);
}
public void render(SpriteBatch sb) {
states.peek().render(sb);
}
}
以及呈现黑色矩形的有问题的PlayingState:
public class PlayingState extends State implements OnMessageReceived, OnConnectionFailed {
private static TCPClient client;
private String message;
private BitmapFont font;
private Player me;
private World world;
private Background background;
private Foreground foreground;
public PlayingState(GameStateManager gsm, String name, String type) {
super(gsm);
background = new Background();
foreground = new Foreground();
client = new TCPClient(this, this);
client.start();
font = new BitmapFont();
font.setColor(Color.WHITE);
try {
JSONObject json = new JSONObject();
json.put("type", type);
json.put("name", name);
client.sendMessage(MessageType.INIT, json);
} catch(Exception e) {e.printStackTrace();}
}
@Override
public void handleInput() {
}
@Override
public void update(float dt) {
handleInput();
if(world != null && me != null) {
Player player = world.getPlayerById(me.getId());
if(player != null)
me = player;
}
if(me != null && world != null) {
OrthographicCamera camera = GdxGame.getCameraInstance();
camera.zoom = -(me.getWidth()/me.getOriginalWidth());
camera.position.x = this.clamp(me.getX()/(-camera.zoom) - (GdxGame.WIDTH/2), 0, world.getWIDTH() - GdxGame.WIDTH) + GdxGame.WIDTH/2;
camera.position.y = this.clamp(me.getY()/(-camera.zoom) - (GdxGame.HEIGHT/2), 0, world.getHEIGHT() - GdxGame.HEIGHT) + GdxGame.HEIGHT/2;
}
}
@Override
public void render(SpriteBatch sb) {
if(world != null) {
background.render(sb, world);
world.render(sb);
foreground.render(sb, world);
}
if(message != null && message != "") {
sb.begin();
font.draw(sb, message, GdxGame.WIDTH/2 - message.length()*3, GdxGame.HEIGHT/2);
sb.end();
}
}
@Override
public void messageReceived(String message) {
try {
JSONObject json = new JSONObject(message);
if(MessageType.parse(json) == MessageType.ME) {
me = Player.fromJSON(json.getJSONObject("data"));
} else if(MessageType.parse(json) == MessageType.RENDER) {
world = World.fromJSON(json.getJSONObject("data"));
}
} catch(Exception e) {}
}
@Override
public void connectionFailed(Throwable e) {
message = "No internet connection or server is down.: " + e.getMessage();
}
@Override
public void dispose() {
client.dispose();
}
private float clamp (float value, float min, float max){
if(value < min/(-GdxGame.getCameraInstance().zoom)) return min/(-GdxGame.getCameraInstance().zoom);
else if(value > max/(-GdxGame.getCameraInstance().zoom)) return max/(-GdxGame.getCameraInstance().zoom);
return value;
}
}
背景和前景的绘制没有问题,唯一不正确的是上面提到的Player类和Food类。它们在World渲染方法中呈现。世界级看起来像这样:
public class World {
private Array<Player> players;
private Array<Food> food;
private int WIDTH;
private int HEIGHT;
public Array<Player> getPlayers() {
return players;
}
public void setPlayers(Array<Player> players) {
this.players = players;
}
public Array<Food> getFood() {
return food;
}
public void setFood(Array<Food> food) {
this.food = food;
}
public Player getPlayerById(String id) {
for(Player p : players) {
if(p.getId().equals( id ))
return p;
}
return null;
}
public static World fromJSON(JSONObject json) {
World world = new World();
Array<Player> players = new Array<Player>();
Array<Food> food = new Array<Food>();
try {
world.setWIDTH(json.getInt("WIDTH"));
world.setHEIGHT(json.getInt("HEIGHT"));
JSONArray jsplayers = json.getJSONArray("players");
for(int i = 0; i < jsplayers.length(); i++) {
players.add(Player.fromJSON(jsplayers.getJSONObject(i)));
}
JSONArray jsfood = json.getJSONArray("food");
for(int i = 0; i < jsfood.length(); i++) {
food.add(Food.fromJSON(jsfood.getJSONObject(i)));
}
} catch(Exception e) {}
world.setPlayers(players);
world.setFood(food);
return world;
}
public void render(SpriteBatch sb) {
if(players != null)
for(Player player : players)
if(player != null)
player.render(sb);
if(food != null)
for(Food f : food)
if(f != null)
f.render(sb);
}
public int getWIDTH() {
return WIDTH;
}
public void setWIDTH(int WIDTH) {
this.WIDTH = WIDTH;
}
public int getHEIGHT() {
return HEIGHT;
}
public void setHEIGHT(int HEIGHT) {
this.HEIGHT = HEIGHT;
}
}