## android游戏开发框架libgdx的使用（十七）—TiledMap中角色的行动路径

1.随主角的移动变化,靠近主角或远离主角

2.按照固定路线移动

3.不动

public Enemy(List<Vector2> vector2s, TextureRegion region) {
super(region);
this.vector2s = vector2s;
currentIndex = 0;
this.x = vector2s.get(currentIndex).x;
this.y = vector2s.get(currentIndex).y;
}

float x = Math.abs(v1.x - v2.x);
float y = Math.abs(v1.y - v2.y);
float z = ( float) MathUtil.distanceBetweenTwoPoints(v1, v2);
float moveX = 0f;
float moveY = 0f;
moveX = (x / z) * stepLength;
moveY = (y / z) * stepLength;
if ( this.x < v2.x) {
this.x += moveX;
} else {
this.x -= moveX;
}
if ( this.y < v2.y) {
this.y += moveY;
} else {
this.y -= moveY;
}

distanceBetweenTwoPoints是我自己写的方法，计算两点距离。

int nextIndex = currentIndex + 1 >= vector2s.size() - 1 ? vector2s
.size() - 1 : currentIndex + 1;
Vector2 v1 = vector2s.get(currentIndex);
Vector2 v2 = vector2s.get(nextIndex);
if (MathUtil.distanceBetweenTwoPoints( new Vector2( this.x, this.y), v2) < 1) {
currentIndex = currentIndex + 1 < vector2s.size() - 1 ? currentIndex + 1
: vector2s.size() - 1;
nextIndex = currentIndex + 1 >= vector2s.size() - 1 ? vector2s
.size() - 1 : currentIndex + 1;
v1 = vector2s.get(currentIndex);
v2 = vector2s.get(nextIndex);
}

Enemy用的图片是这张

package com.cnblogs.htynkn.game;

import java.util.ArrayList;
import java.util.List;

import javax.swing.text.ZoneView;
import javax.swing.text.html.MinimalHTMLWriter;

import com.cnblogs.htynkn.actors.Enemy;

public class MapDemo implements ApplicationListener, InputProcessor {

Stage stage;
float width;
float height;
private TiledMap map;
private TileAtlas atlas;
private TileMapRenderer tileMapRenderer;
Vector3 camDirection = new Vector3(1, 1, 0);
Vector2 maxCamPosition = new Vector2(0, 0);
Vector3 moveVector = new Vector3(0, 0, 0);
Enemy enemy;
int i = 0;

@Override
public void create() {
final String path = "map/";
FileHandle mapHandle = Gdx.files.internal(path + mapname + ".tmx");

atlas = new TileAtlas(map, new FileHandle("map/"));
tileMapRenderer = new TileMapRenderer(map, atlas, 10, 10);
maxCamPosition.set(tileMapRenderer.getMapWidthUnits(), tileMapRenderer
.getMapHeightUnits());

width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
stage = new Stage(width, height, true);

List<Vector2> list = new ArrayList<Vector2>();
// 获取所有wayPoints
for (TiledObjectGroup group : map.objectGroups) {
for (TiledObject object : group.objects) {
if (object.name.startsWith("wayPoint")) {
System.out.println(object.name + " X:" + object.x + " Y:"
+ object.y);
list
- object.y));
}
}
}
TextureAtlas region = new TextureAtlas(Gdx.files.internal("imgs/pack"));
Texture texture = region.findRegion("Enemy").getTexture();
TextureRegion[][] regions = TextureRegion.split(texture, 25, 33);
enemy = new Enemy(list, regions[1][2]);
InputMultiplexer inputMultiplexer = new InputMultiplexer();
Gdx.input.setInputProcessor(inputMultiplexer);
}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public void pause() {
// TODO Auto-generated method stub

}

@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
OrthographicCamera c = (OrthographicCamera) stage.getCamera();
c.position.set(enemy.x, enemy.y, 0);
stage.act(Gdx.graphics.getDeltaTime());
tileMapRenderer.render(c);
stage.draw();
}

@Override
public void resize( int width, int height) {
// TODO Auto-generated method stub

}

@Override
public void resume() {
// TODO Auto-generated method stub

}

@Override
public boolean keyDown( int keycode) {
return false;
}

@Override
public boolean keyTyped( char character) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean keyUp( int keycode) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean scrolled( int amount) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean touchDown( int x, int y, int pointer, int button) {
return false;
}

@Override
public boolean touchDragged( int x, int y, int pointer) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean touchMoved( int x, int y) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean touchUp( int x, int y, int pointer, int button) {
Gdx.app.log("Info", "touchUp: x:" + x + " y: " + y + " pointer: "
+ pointer + " button: " + button);
return false;
}
}

package com.cnblogs.htynkn.actors;

import java.util.ArrayList;
import java.util.List;

public class Enemy extends Image {

List<Vector2> vector2s = new ArrayList<Vector2>();
int currentIndex;
float stepLength = 1f;

public Enemy(List<Vector2> vector2s, TextureRegion region) {
super(region);
this.vector2s = vector2s;
currentIndex = 0;
this.x = vector2s.get(currentIndex).x;
this.y = vector2s.get(currentIndex).y;
}

@Override
public void draw(SpriteBatch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
}

@Override
public Actor hit( float x, float y) {
return null;
}
@Override
public void act( float delta) {
int nextIndex = currentIndex + 1 >= vector2s.size() - 1 ? vector2s
.size() - 1 : currentIndex + 1;
Vector2 v1 = vector2s.get(currentIndex);
Vector2 v2 = vector2s.get(nextIndex);
if (MathUtil.distanceBetweenTwoPoints( new Vector2( this.x, this.y), v2) < 1) {
currentIndex = currentIndex + 1 < vector2s.size() - 1 ? currentIndex + 1
: vector2s.size() - 1;
nextIndex = currentIndex + 1 >= vector2s.size() - 1 ? vector2s
.size() - 1 : currentIndex + 1;
v1 = vector2s.get(currentIndex);
v2 = vector2s.get(nextIndex);
}
float x = Math.abs(v1.x - v2.x);
float y = Math.abs(v1.y - v2.y);
float z = ( float) MathUtil.distanceBetweenTwoPoints(v1, v2);
float moveX = 0f;
float moveY = 0f;
moveX = (x / z) * stepLength;
moveY = (y / z) * stepLength;
if ( this.x < v2.x) {
this.x += moveX;
} else {
this.x -= moveX;
}
if ( this.y < v2.y) {
this.y += moveY;
} else {
this.y -= moveY;
}
System.out.println("pos: " + this.x + "," + this.y + " v1:"
+ v1.toString() + " v2:" + v2.toString() + " d:" + z + " move:"
+ moveX + " , " + moveY);
super.act(delta);
}
}