将 TiledMap 加载到阶段 class ,libgdx
loading TiledMap into stage class ,libgdx
我正在尝试将 tmx 地图加载到舞台 class 但是当我 运行 我的游戏屏幕不显示任何关于地图的信息,只显示角色,我认为问题是相机但不知道如何解决:
摘要屏幕:
public abstract class AbstractScreen extends Stage implements Screen{
protected AbstractScreen(){
//Crea un punto de vista por defecto
super(new StretchViewport(1024, 720));
}
/**
* que deberá ser implementado por cada pantalla de nuestro juego.
* Este método se utiliza para añadir los actores a cada escena
* (LibGDX Scene2d).
*/
public abstract void buildStage();
public abstract void dispose();
public void render(float arg0) {
/*
* Limpiamos la Screen
*/
//Con esto se define el color con el que se borrara el buffer al hacer un glClear()
//Rgb y alpha(transparecia)
Gdx.gl20.glClearColor(0, 0, 0, 1);
// limpiar el buffer depth, el buffer secundario,
//para que no tenga cosas previamente dibujadas
Gdx.gl.glClear(GL11.GL_COLOR_BUFFER_BIT);
//buildStage();
//llamada metodos stage
super.act(Gdx.graphics.getDeltaTime());
super.draw();
}
public void show() {
// TODO Auto-generated method stub
Gdx.input.setInputProcessor(this);
}
public void resize(int width, int height) {
getViewport().update(width, height, true);
}
public void hide() {}
public void pause() {}
public void resume() {}
}
屏幕:
public class SpriteScreen extends AbstractScreen implements InputProcessor {
//private SpriteActor mActor;
private Player jugador;
private TouchPadPlayer touchpad;
private TiledMap map;
private static OrthogonalTiledMapRenderer renderer;
private TiledMapTileLayer collision;
public SpriteScreen() {
this.jugador = new Player(getWidth() / 2, getHeight() / 2);
this.touchpad = new TouchPadPlayer();
this.touchpad.setPosition(100, 100);
this.map=new TmxMapLoader().load("assests/maps/example.tmx");
collision = (TiledMapTileLayer)map.getLayers().get("Colisiones");
renderer = new OrthogonalTiledMapRenderer(map, 1 / 16f);
}
@Override
public void buildStage() {
// TODO Auto-generated method stub
renderer.render();
Gdx.input.setInputProcessor(this);
addActor(this.jugador);
addActor(this.touchpad);
this.getCamera().update();
renderer.setView((OrthographicCamera) this.getCamera());
renderer.render();
}
@Override
public void dispose() {
// TODO Auto-generated method stub
map.dispose();
renderer.dispose();
}
首先,您的瓷砖尺寸是多少?你确定它是 16 x 16 像素吗?如果是这种情况,那么在 AbstractScreen 的调整大小方法中,您需要将相机调整为:
public void resize(int width, int height) {
getViewport().update(width, height, true);
getViewport().getcamera().viewportWidth = YourScreenWidth / 16f;
getViewport().getcamera().viewportHeight = YourScreenHeight / 16f;
//set the camera to start from 0,0 and not from the center of the screen
getViewport().getcamera().setToOrtho(false,
getViewport().getcamera().viewportWidth / 2f,
getViewport().getcamera().viewportHeight/ 2f);
getViewport().getcamera().update();
}
这应该会在整个屏幕上显示您的地图。基本上,对于平铺地图,您无法以编程方式调整它们的大小,因为没有 setSize、setWidth 等方法,所以诀窍是使用相机和平铺大小来正确显示它们。
您必须在 'Screen' 的 render()
方法而不是 buildStage()
方法中调用 renderer.render();
。
我正在尝试将 tmx 地图加载到舞台 class 但是当我 运行 我的游戏屏幕不显示任何关于地图的信息,只显示角色,我认为问题是相机但不知道如何解决:
摘要屏幕:
public abstract class AbstractScreen extends Stage implements Screen{
protected AbstractScreen(){
//Crea un punto de vista por defecto
super(new StretchViewport(1024, 720));
}
/**
* que deberá ser implementado por cada pantalla de nuestro juego.
* Este método se utiliza para añadir los actores a cada escena
* (LibGDX Scene2d).
*/
public abstract void buildStage();
public abstract void dispose();
public void render(float arg0) {
/*
* Limpiamos la Screen
*/
//Con esto se define el color con el que se borrara el buffer al hacer un glClear()
//Rgb y alpha(transparecia)
Gdx.gl20.glClearColor(0, 0, 0, 1);
// limpiar el buffer depth, el buffer secundario,
//para que no tenga cosas previamente dibujadas
Gdx.gl.glClear(GL11.GL_COLOR_BUFFER_BIT);
//buildStage();
//llamada metodos stage
super.act(Gdx.graphics.getDeltaTime());
super.draw();
}
public void show() {
// TODO Auto-generated method stub
Gdx.input.setInputProcessor(this);
}
public void resize(int width, int height) {
getViewport().update(width, height, true);
}
public void hide() {}
public void pause() {}
public void resume() {}
}
屏幕:
public class SpriteScreen extends AbstractScreen implements InputProcessor {
//private SpriteActor mActor;
private Player jugador;
private TouchPadPlayer touchpad;
private TiledMap map;
private static OrthogonalTiledMapRenderer renderer;
private TiledMapTileLayer collision;
public SpriteScreen() {
this.jugador = new Player(getWidth() / 2, getHeight() / 2);
this.touchpad = new TouchPadPlayer();
this.touchpad.setPosition(100, 100);
this.map=new TmxMapLoader().load("assests/maps/example.tmx");
collision = (TiledMapTileLayer)map.getLayers().get("Colisiones");
renderer = new OrthogonalTiledMapRenderer(map, 1 / 16f);
}
@Override
public void buildStage() {
// TODO Auto-generated method stub
renderer.render();
Gdx.input.setInputProcessor(this);
addActor(this.jugador);
addActor(this.touchpad);
this.getCamera().update();
renderer.setView((OrthographicCamera) this.getCamera());
renderer.render();
}
@Override
public void dispose() {
// TODO Auto-generated method stub
map.dispose();
renderer.dispose();
}
首先,您的瓷砖尺寸是多少?你确定它是 16 x 16 像素吗?如果是这种情况,那么在 AbstractScreen 的调整大小方法中,您需要将相机调整为:
public void resize(int width, int height) {
getViewport().update(width, height, true);
getViewport().getcamera().viewportWidth = YourScreenWidth / 16f;
getViewport().getcamera().viewportHeight = YourScreenHeight / 16f;
//set the camera to start from 0,0 and not from the center of the screen
getViewport().getcamera().setToOrtho(false,
getViewport().getcamera().viewportWidth / 2f,
getViewport().getcamera().viewportHeight/ 2f);
getViewport().getcamera().update();
}
这应该会在整个屏幕上显示您的地图。基本上,对于平铺地图,您无法以编程方式调整它们的大小,因为没有 setSize、setWidth 等方法,所以诀窍是使用相机和平铺大小来正确显示它们。
您必须在 'Screen' 的 render()
方法而不是 buildStage()
方法中调用 renderer.render();
。