andengine编程之sprite(三)

在andengine编程之sprite(二)中,我们介绍了一些sprite中所用到的方法,这次,我们要结合这些来完成精灵的触摸和简单的移动,拖拽效果

关于sprite点击效果,之前在menu中也有提到。ButtonSprite的构造方法中,需要传入 new OnClickListener()按键监听机制,可以实现点击效果的切换。看看源代码的话,就能明白,它其实就是对onAreaTouched方法进行了封装。这次我们就来讲讲onAreaTouched,其实,它的使用更为简单。

在上一个TestSprite基础上,我们来改进一下,为face精灵增加触摸机制:

原来的精灵图片比较小,我们这里做了放大处理,以便于点击

	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		Scene mScene = new Scene();

		face = new AnimatedSprite(100, 100, mSpriteTiledTextureRegion,
				getVertexBufferObjectManager()) {
			// 是否选中的标志
			boolean mGrabbed = false;

			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				switch (pSceneTouchEvent.getAction()) {
				case TouchEvent.ACTION_DOWN:
					mGrabbed = true;
					// 将原图片放大到4.5倍(之前设置的是4倍)
					setScale(4.5f);
					break;

				case TouchEvent.ACTION_MOVE:
					if (mGrabbed) {
						// 从新设置精灵的坐标
						setPosition(pSceneTouchEvent.getX(),
								pSceneTouchEvent.getY());
					}
					break;

				case TouchEvent.ACTION_UP:
					if (mGrabbed) {
						mGrabbed = false;
						// 将图片还原到之前的样子,4倍大小
						setScale(4f);
					}
					break;
				}
				return true;
			}
		};
		// 为了点击区域大一些,我们将图片放大4倍
		face.setScale(4f);
		face.animate(new long[] { 200, 200 }, 0, 1, true);

		// 设置背景
		mScene.setBackground(background);
		mScene.attachChild(face);

		// 注册精灵要实现触摸效果
		mScene.registerTouchArea(face);

		pOnCreateSceneCallback.onCreateSceneFinished(mScene);
	}

然后,你可以试试点住笑脸,拖动拖动看看。

除了点中精灵对它的位置重新设置外,我们还可以对整个场景进行监听。在触摸到场景中任意一个位置的时候,我们同样改变一下精灵的当前坐标。


	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		Scene mScene = new Scene();

		final AnimatedSprite face = new AnimatedSprite(100, 100,
				mSpriteTiledTextureRegion, getVertexBufferObjectManager());
		// 为了点击区域大一些,我们将图片放大4倍
		face.setScale(4f);
		face.animate(new long[] { 200, 200 }, 0, 1, true);

		// 设置背景
		mScene.setBackground(background);
		mScene.attachChild(face);
		// 注册精灵要实现触摸效果
		mScene.registerTouchArea(face);

		// 为场景注册触摸监听事件
		mScene.setOnSceneTouchListener(new IOnSceneTouchListener() {

			public boolean onSceneTouchEvent(Scene pScene,
					TouchEvent pSceneTouchEvent) {

				switch (pSceneTouchEvent.getAction()) {
				// 这里,我们只取了按下抬起时的效果,方便我们观察
				case TouchEvent.ACTION_UP:
					face.setPosition(pSceneTouchEvent.getX(),
							pSceneTouchEvent.getY());
					break;
				}
				return true;
			}
		});

		pOnCreateSceneCallback.onCreateSceneFinished(mScene);
	}


以下是包含了两种触摸机制的源代码:


package com.testsprite;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.RepeatingSpriteBackground;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

public class TestSprite extends BaseGameActivity {
	private static final int CAMERA_WIDTH = 800;
	private static final int CAMERA_HEIGHT = 480;

	private RepeatingSpriteBackground background;

	private TiledTextureRegion mSpriteTiledTextureRegion;

	public EngineOptions onCreateEngineOptions() {
		Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions mEngineOptions = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
				mCamera);
		return mEngineOptions;
	}

	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {

		this.background = new RepeatingSpriteBackground(800, 480,
				getTextureManager(), AssetBitmapTextureAtlasSource.create(
						this.getAssets(), "background.png"),
				getVertexBufferObjectManager());

		BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(
				getTextureManager(), 64, 32, TextureOptions.DEFAULT);
		mSpriteTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(mBitmapTextureAtlas, this,
						"face_circle_tiled.png", 0, 0, 2, 1);
		mBitmapTextureAtlas.load();

		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		Scene mScene = new Scene();

		final AnimatedSprite face = new AnimatedSprite(100, 100,
				mSpriteTiledTextureRegion, getVertexBufferObjectManager()) {
			// 是否选中的标志
			boolean mGrabbed = false;

			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				switch (pSceneTouchEvent.getAction()) {
				case TouchEvent.ACTION_DOWN:
					mGrabbed = true;
					// 将原图片放大到4.5倍(之前设置的是4倍)
					setScale(4.5f);
					break;

				case TouchEvent.ACTION_MOVE:
					if (mGrabbed) {
						// 从新设置精灵的坐标
						setPosition(pSceneTouchEvent.getX(),
								pSceneTouchEvent.getY());
					}
					break;

				case TouchEvent.ACTION_UP:
					if (mGrabbed) {
						mGrabbed = false;
						// 将图片还原到之前的样子,4倍大小
						setScale(4f);
					}
					break;
				}
				return true;
			}
		};
		// 为了点击区域大一些,我们将图片放大4倍
		face.setScale(4f);
		face.animate(new long[] { 200, 200 }, 0, 1, true);

		// 设置背景
		mScene.setBackground(background);
		mScene.attachChild(face);
		// 注册精灵要实现触摸效果
		mScene.registerTouchArea(face);

		// 为场景注册触摸监听事件
		mScene.setOnSceneTouchListener(new IOnSceneTouchListener() {

			public boolean onSceneTouchEvent(Scene pScene,
					TouchEvent pSceneTouchEvent) {

				switch (pSceneTouchEvent.getAction()) {
				// 这里,我们只取了按下抬起时的效果,方便我们观察
				case TouchEvent.ACTION_UP:
					face.setPosition(pSceneTouchEvent.getX(),
							pSceneTouchEvent.getY());
					break;
				}
				return true;
			}
		});

		pOnCreateSceneCallback.onCreateSceneFinished(mScene);
	}

	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		pOnPopulateSceneCallback.onPopulateSceneFinished();

	}

}


Sprite触摸机制

你可能感兴趣的:(编程,exception,Class,UP,float,menu)