MapObjects Libgdx/Tiled 对象属性?
MapObjects Libgdx/Tiled Object properties?
我有一个带有图块图像层和对象层的 TMX 文件。我已将其全部导入到我的 libGDX 项目中,但似乎无法将每个对象的矩形坐标导入到我的代码中。请看下面我的等级class和TMX文件以供参考。 (我已经坚持了好几天了,这太令人沮丧了,我已经尝试了很多东西,但我现在一团糟!):
package com.moneylife.solarsystem;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
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.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.math.Rectangle;
public class Level {
BitmapFont font;
int sizeTest = 0;
OrthogonalTiledMapRenderer renderer;
OrthographicCamera camera;
Stage mainStage;
TiledMap map;
Array<Rectangle> platformRects;
Array<Platform> platforms;
SpriteBatch spriteBatch;
float unitScale = 1 / 256f;
Platform platform;
public Level(String TmxFileName, OrthographicCamera Camera, Stage MainStage) {
font = new BitmapFont();
font.setColor(Color.RED);
mainStage = MainStage;
map = new TmxMapLoader().load(TmxFileName);
renderer = new OrthogonalTiledMapRenderer(map, unitScale);
camera = Camera;
camera.setToOrtho(false, 7, 4);
camera.update();
spriteBatch = new SpriteBatch();
platformRects = new Array<Rectangle>();
MapLayer layer = map.getLayers().get("platforms");
for (MapObject object : layer.getObjects()) {
if (object instanceof RectangleMapObject) {
RectangleMapObject rectangleObject = (RectangleMapObject) object;
// If you want to use the name to determine if it belongs in your array
if ("floorRect".equals(rectangleObject.getName())) {
Rectangle rectangle = rectangleObject.getRectangle();
platformRects.add(rectangle);
}
// If you want to look at the "type" property to determine if it belongs in your array
// if ("rectie".equals(rectangleObject.getProperties().get("type", String.class))) {
// Rectangle rectangle = rectangleObject.getRectangle();
// platformRects.add(rectangle);
// }
}
}
for (int i = 0; i < platformRects.size; i++){
platform = new Platform(platformRects.get(i));
mainStage.addActor(platform);
}
}
public void draw() {
spriteBatch.begin();
renderer.setView(camera);
renderer.render();
font.draw(spriteBatch, "plat Rects count: " + sizeTest, 0, 0);
spriteBatch.end();
}
}
以及 TMX 文件......
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-up" width="20" height="5" tilewidth="256" tileheight="256" nextobjectid="47">
<tileset firstgid="1" name="bg" tilewidth="256" tileheight="256" tilecount="12">
<image source="../textures/bg.jpg" width="1024" height="768"/>
</tileset>
<tileset firstgid="13" name="platforms" tilewidth="256" tileheight="256" tilecount="2">
<tile id="0">
<image width="256" height="256" source="../textures/tiles-brown.png"/>
</tile>
<tile id="1">
<image width="256" height="256" source="../textures/tiles-green.png"/>
</tile>
</tileset>
<layer name="background" width="20" height="5">
<data encoding="base64" compression="zlib">
eJytybkNADAIBEH/f//1ehMnyBCBNMGxIfheQURCdthWe79fT0qvaOgYYltN2xMLG0dsq2n7AsAQAbY=
</data>
</layer>
<objectgroup name="platforms">
<object id="20" name="floorRect" type="rectie" gid="14" x="1280" y="1280" width="256" height="256"/>
<object id="21" name="floorRect" type="rectie" gid="14" x="1792" y="1280" width="256" height="256"/>
<object id="22" name="floorRect" type="rectie" gid="14" x="1536" y="1280" width="256" height="256"/>
<object id="23" name="floorRect" type="rectie" gid="13" x="2304" y="1280" width="256" height="256"/>
<object id="24" name="floorRect" type="rectie" gid="13" x="2560" y="1280" width="256" height="256"/>
<object id="25" name="floorRect" type="rectie" gid="14" x="2816" y="1280" width="256" height="256"/>
<object id="26" name="floorRect" type="rectie" gid="14" x="2560" y="1024" width="256" height="256"/>
<object id="27" name="floorRect" type="rectie" gid="14" x="2304" y="1024" width="256" height="256"/>
<object id="28" name="floorRect" type="rectie" gid="14" x="3328" y="1280" width="256" height="256"/>
<object id="29" name="floorRect" type="rectie" gid="14" x="3584" y="1024" width="256" height="256"/>
<object id="30" gid="13" x="3584" y="1280" width="256" height="256"/>
<object id="31" name="floorRect" type="rectie" gid="14" x="3840" y="1280" width="256" height="256"/>
<object id="32" name="floorRect" type="rectie" gid="14" x="4096" y="1280" width="256" height="256"/>
<object id="33" name="floorRect" type="rectie" gid="14" x="4352" y="1280" width="256" height="256"/>
<object id="34" name="floorRect" type="rectie" gid="14" x="4608" y="1280" width="256" height="256"/>
<object id="35" name="floorRect" type="rectie" gid="14" x="0" y="1280" width="256" height="256"/>
<object id="36" gid="14" x="256" y="1280" width="256" height="256"/>
<object id="37" name="floorRect" type="rectie" gid="14" x="512" y="1280" width="256" height="256"/>
<object id="38" name="floorRect" type="rectie" gid="14" x="768" y="1280" width="256" height="256"/>
<object id="39" gid="13" x="1024" y="1280" width="256" height="256"/>
<object id="40" name="floorRect" type="rectie" gid="14" x="1024" y="1024" width="256" height="256"/>
<object id="41" gid="14" x="0" y="1279" width="256" height="256"/>
<object id="42" name="floorRect" type="rectie" gid="14" x="256" y="1280" width="256" height="256"/>
</objectgroup>
</map>
我想这就是您要找的...
if(object instanceof TextureMapObject) {
TextureMapObject mapObject = (TextureMapObject) object;
this.bounds = new Rectangle(mapObject.getX(), mapObject.getY(),
mapObject.getTextureRegion().getRegionWidth(),
mapObject.getTextureRegion().getRegionHeight());
}
else
this.bounds = ((RectangleMapObject) object).getRectangle();
我有一个带有图块图像层和对象层的 TMX 文件。我已将其全部导入到我的 libGDX 项目中,但似乎无法将每个对象的矩形坐标导入到我的代码中。请看下面我的等级class和TMX文件以供参考。 (我已经坚持了好几天了,这太令人沮丧了,我已经尝试了很多东西,但我现在一团糟!):
package com.moneylife.solarsystem;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
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.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.math.Rectangle;
public class Level {
BitmapFont font;
int sizeTest = 0;
OrthogonalTiledMapRenderer renderer;
OrthographicCamera camera;
Stage mainStage;
TiledMap map;
Array<Rectangle> platformRects;
Array<Platform> platforms;
SpriteBatch spriteBatch;
float unitScale = 1 / 256f;
Platform platform;
public Level(String TmxFileName, OrthographicCamera Camera, Stage MainStage) {
font = new BitmapFont();
font.setColor(Color.RED);
mainStage = MainStage;
map = new TmxMapLoader().load(TmxFileName);
renderer = new OrthogonalTiledMapRenderer(map, unitScale);
camera = Camera;
camera.setToOrtho(false, 7, 4);
camera.update();
spriteBatch = new SpriteBatch();
platformRects = new Array<Rectangle>();
MapLayer layer = map.getLayers().get("platforms");
for (MapObject object : layer.getObjects()) {
if (object instanceof RectangleMapObject) {
RectangleMapObject rectangleObject = (RectangleMapObject) object;
// If you want to use the name to determine if it belongs in your array
if ("floorRect".equals(rectangleObject.getName())) {
Rectangle rectangle = rectangleObject.getRectangle();
platformRects.add(rectangle);
}
// If you want to look at the "type" property to determine if it belongs in your array
// if ("rectie".equals(rectangleObject.getProperties().get("type", String.class))) {
// Rectangle rectangle = rectangleObject.getRectangle();
// platformRects.add(rectangle);
// }
}
}
for (int i = 0; i < platformRects.size; i++){
platform = new Platform(platformRects.get(i));
mainStage.addActor(platform);
}
}
public void draw() {
spriteBatch.begin();
renderer.setView(camera);
renderer.render();
font.draw(spriteBatch, "plat Rects count: " + sizeTest, 0, 0);
spriteBatch.end();
}
}
以及 TMX 文件......
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-up" width="20" height="5" tilewidth="256" tileheight="256" nextobjectid="47">
<tileset firstgid="1" name="bg" tilewidth="256" tileheight="256" tilecount="12">
<image source="../textures/bg.jpg" width="1024" height="768"/>
</tileset>
<tileset firstgid="13" name="platforms" tilewidth="256" tileheight="256" tilecount="2">
<tile id="0">
<image width="256" height="256" source="../textures/tiles-brown.png"/>
</tile>
<tile id="1">
<image width="256" height="256" source="../textures/tiles-green.png"/>
</tile>
</tileset>
<layer name="background" width="20" height="5">
<data encoding="base64" compression="zlib">
eJytybkNADAIBEH/f//1ehMnyBCBNMGxIfheQURCdthWe79fT0qvaOgYYltN2xMLG0dsq2n7AsAQAbY=
</data>
</layer>
<objectgroup name="platforms">
<object id="20" name="floorRect" type="rectie" gid="14" x="1280" y="1280" width="256" height="256"/>
<object id="21" name="floorRect" type="rectie" gid="14" x="1792" y="1280" width="256" height="256"/>
<object id="22" name="floorRect" type="rectie" gid="14" x="1536" y="1280" width="256" height="256"/>
<object id="23" name="floorRect" type="rectie" gid="13" x="2304" y="1280" width="256" height="256"/>
<object id="24" name="floorRect" type="rectie" gid="13" x="2560" y="1280" width="256" height="256"/>
<object id="25" name="floorRect" type="rectie" gid="14" x="2816" y="1280" width="256" height="256"/>
<object id="26" name="floorRect" type="rectie" gid="14" x="2560" y="1024" width="256" height="256"/>
<object id="27" name="floorRect" type="rectie" gid="14" x="2304" y="1024" width="256" height="256"/>
<object id="28" name="floorRect" type="rectie" gid="14" x="3328" y="1280" width="256" height="256"/>
<object id="29" name="floorRect" type="rectie" gid="14" x="3584" y="1024" width="256" height="256"/>
<object id="30" gid="13" x="3584" y="1280" width="256" height="256"/>
<object id="31" name="floorRect" type="rectie" gid="14" x="3840" y="1280" width="256" height="256"/>
<object id="32" name="floorRect" type="rectie" gid="14" x="4096" y="1280" width="256" height="256"/>
<object id="33" name="floorRect" type="rectie" gid="14" x="4352" y="1280" width="256" height="256"/>
<object id="34" name="floorRect" type="rectie" gid="14" x="4608" y="1280" width="256" height="256"/>
<object id="35" name="floorRect" type="rectie" gid="14" x="0" y="1280" width="256" height="256"/>
<object id="36" gid="14" x="256" y="1280" width="256" height="256"/>
<object id="37" name="floorRect" type="rectie" gid="14" x="512" y="1280" width="256" height="256"/>
<object id="38" name="floorRect" type="rectie" gid="14" x="768" y="1280" width="256" height="256"/>
<object id="39" gid="13" x="1024" y="1280" width="256" height="256"/>
<object id="40" name="floorRect" type="rectie" gid="14" x="1024" y="1024" width="256" height="256"/>
<object id="41" gid="14" x="0" y="1279" width="256" height="256"/>
<object id="42" name="floorRect" type="rectie" gid="14" x="256" y="1280" width="256" height="256"/>
</objectgroup>
</map>
我想这就是您要找的...
if(object instanceof TextureMapObject) {
TextureMapObject mapObject = (TextureMapObject) object;
this.bounds = new Rectangle(mapObject.getX(), mapObject.getY(),
mapObject.getTextureRegion().getRegionWidth(),
mapObject.getTextureRegion().getRegionHeight());
}
else
this.bounds = ((RectangleMapObject) object).getRectangle();