平铺地图对象未显示(无错误)
Tiled map object is not showing (no errors)
这段代码工作正常,没有错误,地图加载了,但我创建了一个创建对象 class,它没有创建 ant 对象或主体,我认为它创建但没有显示错误不确定的原因
public class Screens implements Screen {
Texture texture;
MyGdxGame game;
ObjectCreater objectCreater;
Player palyer;
public OrthographicCamera cam;
Viewport viewport;
public static TiledMap map;
TiledMapRenderer tiledMapRenderer;
World world;
@Override
public void show() {
cam=new OrthographicCamera(400/100,200/100);
viewport=new StretchViewport(400/100,900/100);
cam.position.set(1,1,0);
map=new TmxMapLoader().load("level1.tmx");
tiledMapRenderer=new OrthogonalTiledMapRenderer(map,1f/100f);
texture=new Texture("player.png");
game=new MyGdxGame();
world=new World(new Vector2(0,-10),true);
palyer=new Player();
objectCreater=new ObjectCreater();
}
@Override
public void render(float delta) {
movecam();
game.batch.setProjectionMatrix(cam.combined);
cam.update();
Gdx.gl.glClearColor(1, 1, 1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
tiledMapRenderer.setView(cam);
tiledMapRenderer.render();
创建对象 class 我开始从 0 数到我在地图中创建的对象,然后
我确定它是 4
public class ObjectCreater {
public static Body Groundbody;
World world;
Player player;
MyGdxGame game;
BodyDef grpund;
Screens screens;
public ObjectCreater(){
screens=new Screens();
player=new Player();
game=new MyGdxGame();
grpund=new BodyDef();
FixtureDef fixter=new FixtureDef();
PolygonShape polygonShape=new PolygonShape();
for (MapObject object:screens.map.getLayers().get(4).getObjects().getByType(RectangleMapObject.class)){
Rectangle rect = ((RectangleMapObject) object).getRectangle();
grpund.type= BodyDef.BodyType.StaticBody;
grpund.position.set(((rect.getX() + rect.getWidth() / 2)/100), (rect.getY() + rect.getHeight() / 2)/100);
Groundbody= player.world.createBody(grpund);
polygonShape.setAsBox((rect.getWidth()/2)/100,(rect.getHeight()/2)/100);
fixter.shape=polygonShape;
fixter.filter.categoryBits=16;
Groundbody.createFixture(fixter).setUserData("backw");
}
要绘制 box2d 实体,您必须创建 Box2dDebugRenderer:
Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
// in render:
debugRenderer.render(world, cam.combined);
这段代码工作正常,没有错误,地图加载了,但我创建了一个创建对象 class,它没有创建 ant 对象或主体,我认为它创建但没有显示错误不确定的原因
public class Screens implements Screen {
Texture texture;
MyGdxGame game;
ObjectCreater objectCreater;
Player palyer;
public OrthographicCamera cam;
Viewport viewport;
public static TiledMap map;
TiledMapRenderer tiledMapRenderer;
World world;
@Override
public void show() {
cam=new OrthographicCamera(400/100,200/100);
viewport=new StretchViewport(400/100,900/100);
cam.position.set(1,1,0);
map=new TmxMapLoader().load("level1.tmx");
tiledMapRenderer=new OrthogonalTiledMapRenderer(map,1f/100f);
texture=new Texture("player.png");
game=new MyGdxGame();
world=new World(new Vector2(0,-10),true);
palyer=new Player();
objectCreater=new ObjectCreater();
}
@Override
public void render(float delta) {
movecam();
game.batch.setProjectionMatrix(cam.combined);
cam.update();
Gdx.gl.glClearColor(1, 1, 1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
tiledMapRenderer.setView(cam);
tiledMapRenderer.render();
创建对象 class 我开始从 0 数到我在地图中创建的对象,然后 我确定它是 4
public class ObjectCreater {
public static Body Groundbody;
World world;
Player player;
MyGdxGame game;
BodyDef grpund;
Screens screens;
public ObjectCreater(){
screens=new Screens();
player=new Player();
game=new MyGdxGame();
grpund=new BodyDef();
FixtureDef fixter=new FixtureDef();
PolygonShape polygonShape=new PolygonShape();
for (MapObject object:screens.map.getLayers().get(4).getObjects().getByType(RectangleMapObject.class)){
Rectangle rect = ((RectangleMapObject) object).getRectangle();
grpund.type= BodyDef.BodyType.StaticBody;
grpund.position.set(((rect.getX() + rect.getWidth() / 2)/100), (rect.getY() + rect.getHeight() / 2)/100);
Groundbody= player.world.createBody(grpund);
polygonShape.setAsBox((rect.getWidth()/2)/100,(rect.getHeight()/2)/100);
fixter.shape=polygonShape;
fixter.filter.categoryBits=16;
Groundbody.createFixture(fixter).setUserData("backw");
}
要绘制 box2d 实体,您必须创建 Box2dDebugRenderer:
Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
// in render:
debugRenderer.render(world, cam.combined);