MyGdxGame.java
package com.mygdx.game;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.maps.objects.EllipseMapObject;
import com.badlogic.gdx.maps.objects.PolygonMapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject;
import com.badlogic.gdx.maps.tiled.renderers.IsometricTiledMapRenderer;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile;
import com.badlogic.gdx.math.Ellipse;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class MyGdxGame extends ApplicationAdapter {
private static final float VIRTUAL_WIDTH = 500.0f;
private static final float VIRTUAL_HEIGHT = 320.0f;
private static final float CAMERA_SPEED = 100.0f;
private OrthographicCamera camera;
private Viewport viewport;
private TiledMap map;
private TmxMapLoader loader;
private OrthogonalTiledMapRenderer renderer;
private Vector2 direction;
@Override
public void create() {
camera = new OrthographicCamera();
viewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, camera);
loader = new TmxMapLoader();
map = loader.load("data/maps/tiled/tiles.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
direction = new Vector2();
}
@Override
public void dispose() {
map.dispose();
renderer.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
updateCamera();
renderer.setView(camera);
renderer.render();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
}
private void updateCamera() {
direction.set(0.0f, 0.0f);
int mouseX = Gdx.input.getX();
int mouseY = Gdx.input.getY();
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
if (Gdx.input.isKeyPressed(Input.Keys.LEFT) || (Gdx.input.isTouched() && mouseX < width * 0.25f)) {
direction.x = -50;
}
else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) || (Gdx.input.isTouched() && mouseX > width * 0.75f)) {
direction.x = 50;
}
if (Gdx.input.isKeyPressed(Input.Keys.UP) || (Gdx.input.isTouched() && mouseY < height * 0.25f)) {
direction.y = 5;
}
else if (Gdx.input.isKeyPressed(Input.Keys.DOWN) || (Gdx.input.isTouched() && mouseY > height * 0.75f)) {
direction.y = -5;
}
Gdx.app.log("tiledmapobjects", "1 direction.x:"+direction.x);
//direction.nor().scl(CAMERA_SPEED * Gdx.graphics.getDeltaTime());;
Gdx.app.log("tiledmapobjects", "2 direction.x:"+direction.x);
camera.position.x += direction.x;
camera.position.y += direction.y;
TiledMapTileLayer layer = (TiledMapTileLayer)map.getLayers().get(0);
float cameraMinX = viewport.getWorldWidth() * 0.5f;
float cameraMinY = viewport.getWorldHeight() * 0.5f;
float cameraMaxX = layer.getWidth() * layer.getTileWidth() - cameraMinX;
float cameraMaxY = layer.getHeight() * layer.getTileHeight() - cameraMinY;
camera.position.x = MathUtils.clamp(camera.position.x, cameraMinX, cameraMaxX);
camera.position.y= MathUtils.clamp(camera.position.y, cameraMinY, cameraMaxY);
camera.update();
}
}
效果图:
tiles.png:
1.png:
TMX文件:
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="45" height="10" tilewidth="32" tileheight="32">
<tileset firstgid="1" name="tukuai1" tilewidth="32" tileheight="32" spacing="1" margin="1">
<image source="tiles.png" width="512" height="256"/>
</tileset>
<layer name="块层 1" width="45" height="10">
<data encoding="csv">
6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,
21,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,23,
21,22,51,51,51,81,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,81,81,51,51,51,51,51,51,51,51,51,51,51,51,51,81,81,81,51,51,51,22,23,
21,22,51,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,51,22,23,
21,22,51,22,46,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,48,22,51,22,23,
21,22,51,22,76,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,78,22,51,22,23,
21,22,51,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,51,22,23,
21,22,51,51,51,81,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,81,81,51,51,51,51,51,51,51,51,51,51,51,51,51,81,81,81,51,51,51,22,23,
21,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,23,
36,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,38
</data>
</layer>
<imagelayer name="图像图层 1" x="224" y="192">
<image source="1.png"/>
</imagelayer>
<objectgroup name="对象层 1">
<object gid="52" x="384" y="96"/>
<object gid="52" x="384" y="256"/>
<object gid="52" x="0" y="320"/>
<object gid="52" x="0" y="32"/>
<object gid="52" x="1408" y="32"/>
<object gid="52" x="1408" y="320"/>
</objectgroup>
</map>