AndEngine之DEMO学习(七)EntityModifierExample

接着上篇的内容,这个上个例子说的是使用IUpdateHandler进行业务更新,这回的例子是一EntityModifier的方式对业务进行更新。Modifier我们可以分成两种不同类型,一种可以叫做延时类Modifier,主要处理在一定时间段内的实体变化;一直可以叫做容器类Modifier,主要用来组合多个延时类Modifier,并按次序执行他们。容器类Modifier只有实现了三个子类,分别是循环Modifier,顺序Modifier,并行Modifier。

顾名思义,LoopModifier(循环Modifier)是让延时类Modifier循环处理。他的生命周期并不取决于他本身,而取决于他组合的Modifier是否已经执行完成,所以LoopModifier实现了IModifierListener接口,注册于他组合的Modifier中,当Modifier执行完毕后会通知LoopModifier,LoopModifier可以继续循环,也可以立即结束。

SequenceModifier(顺序Modifier)中组合的延时类Modifier会按照先后次序执行。

ParallelModifier(并行Modifier)中组合的延时类Modifier会一起作用于实体。

在延时类Modifier中又分为单值范围Modifier、两值范围Modifier、三值范围Modifier、四值范围Modifier,主要区别发生变化值的数量。例如透明度变化(AlphaModifier)就只需继承单值Modifier;而大小缩放(ScaleModifier)因为涉及X方向和Y方向,则需要两值Modifier。另外,值随时间变化的规则是由IEaseFunction决定的,下面我给出他们的类图:

AndEngine之DEMO学习(七)EntityModifierExample

最后将修改器注册到实体,即可在业务线程中进行修改了。下面上程序:

package org.andengine.examples;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.DelayModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.LoopEntityModifier.ILoopEntityModifierListener;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.modifier.RotationByModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.util.FPSLogger;
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.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.modifier.IModifier;
import org.andengine.util.modifier.LoopModifier;

import android.opengl.GLES20;
import android.widget.Toast;

public class EntityModifierExample extends SimpleBaseGameActivity {

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

	private BitmapTextureAtlas mBitmapTextureAtlas;
	private TiledTextureRegion mFaceTextureRegion;

	@Override
	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
	public void onCreateResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 32, TextureOptions.BILINEAR);
		this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_box_tiled.png", 0, 0, 2, 1);
		this.mBitmapTextureAtlas.load();
	}

	@Override
	public Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		final Scene scene = new Scene();
		scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

		final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
		final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;

		//画一个矩形
		final Rectangle rect = new Rectangle(centerX + 100, centerY, 32, 32, this.getVertexBufferObjectManager());
		rect.setColor(1, 0, 0);

		//画一个动画精灵
		final AnimatedSprite face = new AnimatedSprite(centerX - 100, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
		face.animate(100);
		face.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

		//创建实体修改器,在业务线程中更新实体状态
		final LoopEntityModifier entityModifier =
			new LoopEntityModifier(
					//EntityModifier的监听,通知LoopEntityModifier的开始和结束
					new IEntityModifierListener() {
						@Override
						public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
							EntityModifierExample.this.runOnUiThread(new Runnable() {
								@Override
								public void run() {
									Toast.makeText(EntityModifierExample.this, "Sequence started.", Toast.LENGTH_SHORT).show();
								}
							});
						}

						@Override
						public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
							EntityModifierExample.this.runOnUiThread(new Runnable() {
								@Override
								public void run() {
									Toast.makeText(EntityModifierExample.this, "Sequence finished.", Toast.LENGTH_SHORT).show();
								}
							});
						}
					},
					2,
					//循环的监听,通知每次循环的开始和结束
					new ILoopEntityModifierListener() {
						@Override
						public void onLoopStarted(final LoopModifier<IEntity> pLoopModifier, final int pLoop, final int pLoopCount) {
							EntityModifierExample.this.runOnUiThread(new Runnable() {
								@Override
								public void run() {
									Toast.makeText(EntityModifierExample.this, "Loop: '" + (pLoop + 1) + "' of '" + pLoopCount + "' started.", Toast.LENGTH_SHORT).show();
								}
							});
						}

						@Override
						public void onLoopFinished(final LoopModifier<IEntity> pLoopModifier, final int pLoop, final int pLoopCount) {
							EntityModifierExample.this.runOnUiThread(new Runnable() {
								@Override
								public void run() {
									Toast.makeText(EntityModifierExample.this, "Loop: '" + (pLoop + 1) + "' of '" + pLoopCount + "' finished.", Toast.LENGTH_SHORT).show();
								}
							});
						}
					},
					//循环Modifier中组合的Modifier,先按顺序执行
					new SequenceEntityModifier(
//							new RotationModifier(1, 0, 90),
							new AlphaModifier(2, 1, 0),
							new AlphaModifier(1, 0, 1),
							new ScaleModifier(2, 1, 0.5f),
							new DelayModifier(0.5f),
							//并行执行
							new ParallelEntityModifier(
									new ScaleModifier(3, 0.5f, 5),
									new RotationByModifier(3, 90)
							),
							new ParallelEntityModifier(
									new ScaleModifier(3, 5, 1),
									new RotationModifier(3, 180, 0)
							)
					)
			);

		//注册实体修改器
		face.registerEntityModifier(entityModifier);
		rect.registerEntityModifier(entityModifier.deepCopy());

		scene.attachChild(face);
		scene.attachChild(rect);

		return scene;
	}
}

程序真的已经没有什么可说的了,晚上继续沉迷=。=

你可能感兴趣的:(AndEngine)