AndEngine屏幕跟随人物行走

这次使用了一个64格8向人物行走的图,用法和上次12格的一致。

当点击屏幕某个点时,人物走动到该触摸点后停止人物走动动画。

人物在走动过程中,屏幕中心一直跟随着人物移动。

代码中使用的图片:

AndEngine屏幕跟随人物行走_第1张图片

代码:

public class MainActivity extends SimpleBaseGameActivity {
	private static final String LOG_TAG = "test camera";

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;

	private static SmoothCamera camera;

	private static final int PLAYER_VELOCITY = 20;
	private static float vx;// 人物x轴方向的速度
	private static float vy;// 人物y轴方向的速度

	private BitmapTextureAtlas backgroundTextureAtlas;
	private TextureRegion backgroundTextureRegion;
	private BitmapTextureAtlas playerTextureAtlas;
	private TiledTextureRegion playerTextureRegion;

	private static final int STATE_MOVING = 0;
	private static final int STATE_STANDBY = 1;
	private int state = STATE_STANDBY;// 人物状态
	private static float touchedLocalX; // 记录触摸点的x坐标
	private static float touchedLocalY; // 记录触摸点的y坐标

	public EngineOptions onCreateEngineOptions() {
		camera = new SmoothCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, PLAYER_VELOCITY, PLAYER_VELOCITY, 1.5f);
		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
	}

	@Override
	protected void onCreateResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		backgroundTextureAtlas = new BitmapTextureAtlas(getTextureManager(), 2048, 1024);
		backgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundTextureAtlas, this, "background.jpg", 0, 0);
		backgroundTextureAtlas.load();

		playerTextureAtlas = new BitmapTextureAtlas(getTextureManager(), 512, 1024);
		playerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(playerTextureAtlas, getAssets(), "girl.png", 0, 0, 8, 8);
		playerTextureAtlas.load();
	}

	@Override
	protected Scene onCreateScene() {
		final Scene scene = new Scene();
		Sprite background = new Sprite(CAMERA_WIDTH / 2 - backgroundTextureRegion.getWidth() / 2, CAMERA_HEIGHT / 2 - backgroundTextureRegion.getHeight() / 2, backgroundTextureRegion, this.mEngine.getVertexBufferObjectManager());
		scene.attachChild(background);

		final AnimatedSprite player = new AnimatedSprite(CAMERA_WIDTH / 2 - playerTextureRegion.getWidth() / 2, CAMERA_HEIGHT / 2 - playerTextureRegion.getHeight() / 2, playerTextureRegion, this.mEngine.getVertexBufferObjectManager());

		scene.attachChild(player);

		camera.setChaseEntity(player);

		scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
			public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
				if (!pSceneTouchEvent.isActionDown())
					return false;

				touchedLocalX = pSceneTouchEvent.getX();
				touchedLocalY = pSceneTouchEvent.getY();
				float angle = (float) (Math.atan2(pSceneTouchEvent.getY() - player.getY(), pSceneTouchEvent.getX() - player.getX()) * 180 / Math.PI);
				Log.i(LOG_TAG, "angle: " + angle);
				if (angle >= -45 / 2 && angle < 45 / 2) {
					// right
					player.animate(new long[] { 200, 200, 200, 200, 200, 200, 200, 200 }, 16, 23, true);
				} else if (angle >= 45 / 2 && angle < 45 + 45 / 2) {
					// downright
					player.animate(new long[] { 200, 200, 200, 200, 200, 200, 200, 200 }, 32, 39, true);
				} else if (angle >= 45 - 45 / 2 && angle < 90 + 45 / 2) {
					// down
					player.animate(new long[] { 200, 200, 200, 200, 200, 200, 200, 200 }, 0, 7, true);
				} else if (angle >= 135 - 45 / 2 && angle < 135 + 45 / 2) {
					// downleft
					player.animate(new long[] { 200, 200, 200, 200, 200, 200, 200, 200 }, 40, 47, true);
				} else if (angle >= 180 - 45 / 2 || angle < -180 + 45 / 2) {
					// left
					player.animate(new long[] { 200, 200, 200, 200, 200, 200, 200, 200 }, 8, 15, true);
				} else if (angle <= -45 + 45 / 2 && angle > -45 - 45 / 2) {
					// upright
					player.animate(new long[] { 200, 200, 200, 200, 200, 200, 200, 200 }, 48, 55, true);
				} else if (angle <= -45 - 45 / 2 && angle > -90 - 45 / 2) {
					// up
					player.animate(new long[] { 200, 200, 200, 200, 200, 200, 200, 200 }, 24, 31, true);
				} else if (angle <= -90 - 45 / 2 && angle > -135 - 45 / 2) {
					// upleft
					player.animate(new long[] { 200, 200, 200, 200, 200, 200, 200, 200 }, 56, 63, true);
				}

				// calculate velocity of x & y coordinate axis
				float a = Math.abs(touchedLocalY - player.getY() - player.getHeight() / 2) / Math.abs(touchedLocalX - player.getX() - player.getWidth() / 2);
				int sideX = touchedLocalX > player.getX() + player.getWidth() / 2 ? 1 : -1;
				int sideY = touchedLocalY > player.getY() + player.getHeight() / 2 ? 1 : -1;
				vx = sideX * (PLAYER_VELOCITY / FloatMath.sqrt(1 + a * a));
				vy = sideY * a * Math.abs(vx);

				state = STATE_MOVING;

				return false;
			}
		});

		scene.registerUpdateHandler(new IUpdateHandler() {

			public void onUpdate(float pSecondsElapsed) {
				switch (state) {
				case STATE_MOVING:
					// calculate move distance of x & y coordinate axis
					float dx = vx * pSecondsElapsed;
					float dy = vy * pSecondsElapsed;
					// move the player
					player.setPosition(player.getX() + dx, player.getY() + dy);
					// estimate player's position arrive touchedLocal

					if (Math.abs(player.getX() + player.getWidth() / 2 - touchedLocalX) < 1.0f && Math.abs(player.getY() + player.getHeight() / 2 - touchedLocalY) < 1.0f) {
						state = STATE_STANDBY;
						player.stopAnimation();
					}
					break;
				case STATE_STANDBY:

					break;
				}
			}

			public void reset() {

			}

		});

		return scene;
	}
}

希望能对大家有帮助,欢迎大家拍砖


你可能感兴趣的:(String,velocity,Class,float,distance)