AndEngine处理12宫格4向行走图

其他类型的比如16宫格的或8向行走图操作方法类似,也可参考本文。

下面是我使用的两张图片,左边的player.png为人物行走图右边的next.png我用来作为上下左右按钮的图片。


代码:

public class MainActivity extends SimpleBaseGameActivity {
	private static final int CAMERA_WIDTH = 800;
	private static final int CAMERA_HEIGHT = 480;

	private BitmapTextureAtlas mBitmapTextureAtlas;
	private TiledTextureRegion mPlayerTextureRegion;
	private BitmapTextureAtlas mArrowTextureAtlas;
	private TextureRegion mArrowTextureRegion;

	public EngineOptions onCreateEngineOptions() {
		final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
	}

	@Override
	protected void onCreateResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 128, 128);
		this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "player.png", 0, 0, 3, 4);
		this.mBitmapTextureAtlas.load();

		this.mArrowTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 128, 128);
		this.mArrowTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mArrowTextureAtlas, this, "next.png", 0, 0);
		this.mArrowTextureAtlas.load();
	}

	@Override
	protected Scene onCreateScene() {
		final Scene scene = new Scene();

		final AnimatedSprite player = new AnimatedSprite(100, 100, 48, 64, this.mPlayerTextureRegion, this.getVertexBufferObjectManager());
		scene.attachChild(player);

		final Sprite up = new Sprite(72, 480-120, this.mArrowTextureRegion, this.getVertexBufferObjectManager()) {
                        // 重写精灵触摸事件
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {				                        // 从精灵动画中读取索引从0到2的帧进行重复播放,每一帧持续时间为200毫秒
				player.animate(new long[] { 200, 200, 200 }, 0, 2, true);
				return true;
			}

		};
		up.setRotation(270);// 将箭头图片旋转279度,作为向上的按钮
		up.setScale(0.5f);// 图片比较大,缩小一半

		final Sprite down = new Sprite(72, 480-72, this.mArrowTextureRegion, this.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				player.animate(new long[] { 200, 200, 200 }, 6, 8, true);
				return true;
			}
		};
		down.setRotation(90);
		down.setScale(0.5f);
		
		final Sprite left = new Sprite(24, 480-72, this.mArrowTextureRegion, this.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				player.animate(new long[] { 200, 200, 200 }, 9, 11, true);
				return true;
			}
		};
		left.setRotation(180);
		left.setScale(0.5f);
		
		final Sprite right = new Sprite(120, 480-72, this.mArrowTextureRegion, this.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				player.animate(new long[] { 200, 200, 200 }, 3, 5, true);
				return true;
			}
		};
		right.setScale(0.5f);
		// 将方向键加入到场景中
		scene.attachChild(up);
		scene.attachChild(down);
		scene.attachChild(left);
		scene.attachChild(right);
		// 注册精灵的触摸事件
		scene.registerTouchArea(up);
		scene.registerTouchArea(down);
		scene.registerTouchArea(left);
		scene.registerTouchArea(right);
		// 绑定场景的触摸方式
		scene.setTouchAreaBindingOnActionDownEnabled(true);

		return scene;
	}

}



你可能感兴趣的:(Class,UP,float)