我开始使用平铺地图编辑器作为我的新游戏的编辑器。 (这是一个平台游戏)。所以我创建了平铺地图并将其保存为.tmx文件。地图非常简单,我只用它来测试它。在我的代码中,我只是将五边形渲染在屏幕上作为“玩家”对象。当我在地图中添加以下代码时:
package com.thechief.platformer.states;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
import com.thechief.platformer.Gradual;
import com.thechief.platformer.entities.Player;
import com.thechief.platformer.textures.TextureManager;
public class GameState extends State {
public static final float GRAVITY = 15f;
private Player player;
private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
@Override
public void create() {
setUp();
camera.setToOrtho(false, Gradual.WIDTH, Gradual.HEIGHT);
player = new Player(new Vector2(camera.position.x, camera.position.y));
maploader = new TmxMapLoader();
map = maploader.load("map.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
camera.update();
}
@Override
public void update(float dt) {
player.update(dt);
}
@Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(camera.combined);
sb.begin();
renderer.setView(camera);
renderer.render();
player.render(sb);
sb.end();
}
@Override
public void dispose() {
renderer.dispose();
map.dispose();
}
}
地图渲染,但播放器无法渲染!
这是Player.java
类:
package com.thechief.platformer.entities;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.math.Vector2;
import com.thechief.platformer.textures.TextureManager;
public class Player extends Entity {
private float spd = 4;
private float velX = 0, velY = 0;
public Player(Vector2 pos) {
super(TextureManager.pentagon, pos);
}
@Override
public void update(float dt) {
if (Gdx.input.isKeyPressed(Keys.D) || Gdx.input.isKeyPressed(Keys.RIGHT)) {
pos.x += spd;
}
if (Gdx.input.isKeyPressed(Keys.A) || Gdx.input.isKeyPressed(Keys.LEFT)) {
pos.x -= spd;
}
pos.x += velX;
pos.y += velY;
}
}
这是为什么?我在代码中做错了吗?
或者玩家类有什么问题吗?我不这么认为,因为即使我用player.render(sb);
替换sb.draw(TextureManager.pentagon, x, y)
,它仍然无法呈现。
请帮忙!
提前致谢!