1.AndEngine的动画修改器(modifier)有很多种,常用的有一下几种:
1.AlphaModifier:透明度修改器,有以下几种构造:
public AlphaModifier(final float pDuration, final float pFromAlpha, final float pToAlpha) {
this(pDuration, pFromAlpha, pToAlpha, null, EaseLinear.getInstance());
}
public AlphaModifier(final float pDuration, final float pFromAlpha, final float pToAlpha, final IEaseFunction pEaseFunction) {
this(pDuration, pFromAlpha, pToAlpha, null, pEaseFunction);
}
public AlphaModifier(final float pDuration, final float pFromAlpha, final float pToAlpha, final IEntityModifierListener pEntityModifierListener) {
super(pDuration, pFromAlpha, pToAlpha, pEntityModifierListener, EaseLinear.getInstance());
}
public AlphaModifier(final float pDuration, final float pFromAlpha, final float pToAlpha, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction) {
super(pDuration, pFromAlpha, pToAlpha, pEntityModifierListener, pEaseFunction);
}
protected AlphaModifier(final AlphaModifier pAlphaModifier) {
super(pAlphaModifier);
}
pDuration代表动画修改周期,就是就是完成整个透明度变化所要的时间,单位为秒.
pFromAlpha代表起始透明度
pToAlpha代表目标透明度
2.ScaleModifier,比例修改器.主要是对动画的大小比例进行修改:
public ScaleModifier(final float pDuration, final float pFromScale, final float pToScale) {
this(pDuration, pFromScale, pToScale, null, EaseLinear.getInstance());
}
public ScaleModifier(final float pDuration, final float pFromScale, final float pToScale, final IEaseFunction pEaseFunction) {
this(pDuration, pFromScale, pToScale, null, pEaseFunction);
}
public ScaleModifier(final float pDuration, final float pFromScale, final float pToScale, final IEntityModifierListener pEntityModifierListener) {
this(pDuration, pFromScale, pToScale, pFromScale, pToScale, pEntityModifierListener, EaseLinear.getInstance());
}
public ScaleModifier(final float pDuration, final float pFromScale, final float pToScale, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction) {
this(pDuration, pFromScale, pToScale, pFromScale, pToScale, pEntityModifierListener, pEaseFunction);
}
public ScaleModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY) {
this(pDuration, pFromScaleX, pToScaleX, pFromScaleY, pToScaleY, null, EaseLinear.getInstance());
}
public ScaleModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY, final IEaseFunction pEaseFunction) {
this(pDuration, pFromScaleX, pToScaleX, pFromScaleY, pToScaleY, null, pEaseFunction);
}
public ScaleModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY, final IEntityModifierListener pEntityModifierListener) {
super(pDuration, pFromScaleX, pToScaleX, pFromScaleY, pToScaleY, pEntityModifierListener, EaseLinear.getInstance());
}
public ScaleModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction) {
super(pDuration, pFromScaleX, pToScaleX, pFromScaleY, pToScaleY, pEntityModifierListener, pEaseFunction);
}
protected ScaleModifier(final ScaleModifier pScaleModifier) {
super(pScaleModifier);
}
pDuration代表动画修改周期,就是就是完成整个变化所要的时间,单位为秒.
pFromScale代表起始透大小比例
pToScale代表目标大小比例
此外,还有DelayModifier,ColorModifier,MoveModifier等等.
另外还有一些组合式的修改器,如SequenceEntityModifier和ParallelEntityModifier.这类修改器的一个显著特点就是可以同时
对几个样式进行修改而达到想要的效果
2.创建项目
由于我的手机是htc-g12.分辨率为800*480,其他手机的分辨率也可以选择更加合适的分辨率,只要尺寸比例一致就可以了.因为我使用到RatioResolutionPolicy
进行屏幕的绘制.它可以自动缩放到屏幕合适的大小.
private static final String TAG = "Season";
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private Camera mCamera;
private Scene mScene;
private RepeatingSpriteBackground background;
private TiledTextureRegion mFaceTextureRegion;
在 onCreateEngineOptions()处设置 camera和 EngineOption选项
@Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
EngineOptions mEngineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
return mEngineOptions;
}
需要说明的几个参数是有关 EngineOptions的:
回到 EngineOptions的源代码构造函数为:
public EngineOptions(final boolean pFullscreen, final ScreenOrientation pScreenOrientation, final IResolutionPolicy pResolutionPolicy, final Camera pCamera) {
this.mFullscreen = pFullscreen;
this.mScreenOrientation = pScreenOrientation;
this.mResolutionPolicy = pResolutionPolicy;
this.mCamera = pCamera;
}
一目了然:
如果要全屏显示,pFullscreen就应该为true.
pScreenOrientation是一个枚举类型的数据
public enum ScreenOrientation {
// ===========================================================
// Elements
// ===========================================================
/** The app will be fixed in its default Landscape mode. */
LANDSCAPE_FIXED,
/** The app will automatically rotate between the Landscape modes, depending on the orientation of the device. */
LANDSCAPE_SENSOR,
/** The app will be fixed in its default Portrait mode. */
PORTRAIT_FIXED,
/** The app will automatically rotate between the Portrait modes, depending on the orientation of the device. */
PORTRAIT_SENSOR;
}
pResolutionPolicy是屏幕的绘制策略,常用的有这几种: FillResolutionPolicy,FixedResolutionPolicy,RatioResolutionPolicy,RelativeResolutionPolicy,从名字
就可以看出各种策略的大致用途了.
在onCreateResources处,是游戏引擎在创建资源(图片,声音等)进行加载用.
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
// TODO Auto-generated method stub
this.background = new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT,
getTextureManager(), AssetBitmapTextureAtlasSource.create(
this.getAssets(), "background.png"),
getVertexBufferObjectManager());
BitmapTextureAtlas mTexture = new BitmapTextureAtlas(getTextureManager(),64,32,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "face_box_tiled.png", 0, 0,2,1);
/**
* 参数说明:
* mTexure是在内存中放置贴图资源用的,64,32是图片要求的宽和高,必须是2的n次方大小.如:2,4,8,16,32,64,128,512,1024....
* 并且要比原图的宽高要大
*
* mFaceTextureRegion相当于从mTexure中扣图,因为mTexure是由很多图集组成的,要从中截取一片出来
* 0,0代表截图的top,right坐标(起点坐标),2和1分别代表贴图中一张存在2列1行
*
*/
mTexture.load();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
onCreateScene是整个游戏的精华之处,所有游戏的控制部分都在这里实现
首先创建场景,布置好背景
mScene = new Scene();
mScene.setBackground(background);
final float centerX = (CAMERA_WIDTH - mFaceTextureRegion.getWidth()) / 2;//计算贴图的中心坐标
final float centerY = (CAMERA_HEIGHT - mFaceTextureRegion.getHeight()) / 2;
在本例程中,主要创建一个红色的外形和一个贴图动画:
final Rectangle rect = new Rectangle(centerX+100,centerY,32,32,getVertexBufferObjectManager());
rect.setColor(1, 0, 0);
final AnimatedSprite face = new AnimatedSprite(centerX - 100, centerY, mFaceTextureRegion, getVertexBufferObjectManager());
face.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
创建完精灵后,就给精灵添加修改器了,这个地方是比较灵活的,完全根据自己的需要来制作.
final LoopEntityModifier shapeModifier =
new LoopEntityModifier(new IEntityModifierListener(){
@Override
public void onModifierStarted(IModifier<IEntity> pModifier,
IEntity pItem) {
// TODO Auto-generated method stub
Log.d(TAG,"LoopEntityModifier started!");
}
@Override
public void onModifierFinished(
IModifier<IEntity> pModifier, IEntity pItem) {
// TODO Auto-generated method stub
Log.d(TAG,"LoopEntityModifier finished!");
}
},
-1, //无限循环
new ILoopEntityModifierListener(){
@Override
public void onLoopStarted(
LoopModifier<IEntity> pLoopModifier, int pLoop,
int pLoopCount) {
// TODO Auto-generated method stub
Log.d(TAG,"ILoopEntityModifierListener started!");
}
@Override
public void onLoopFinished(
LoopModifier<IEntity> pLoopModifier, int pLoop,
int pLoopCount) {
//Toast.makeText(ShapeModifiers.this, "Loops remaining: " + pLoop, Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
Log.d(TAG,"ILoopEntityModifierListener finished!");
}
},
new SequenceEntityModifier(new RotationModifier(1, 0, 90),
new AlphaModifier(5, 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)){
}));
LoopEntityModifier是一个循环修改器,第三个参数就是循环的次数,-1代表无限重复,从源代码中可以看出各个参数的作用
public class LoopEntityModifier extends LoopModifier<IEntity> implements IEntityModifier {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
// ===========================================================
// Constructors
// ===========================================================
public LoopEntityModifier(final IEntityModifier pEntityModifier) {
super(pEntityModifier);
}
public LoopEntityModifier(final IEntityModifier pEntityModifier, final int pLoopCount) {
super(pEntityModifier, pLoopCount);
}
public LoopEntityModifier(final IEntityModifier pEntityModifier, final int pLoopCount, final ILoopEntityModifierListener pLoopModifierListener) {
super(pEntityModifier, pLoopCount, pLoopModifierListener);
}
public LoopEntityModifier(final IEntityModifier pEntityModifier, final int pLoopCount, final IEntityModifierListener pEntityModifierListener) {
super(pEntityModifier, pLoopCount, pEntityModifierListener);
}
public LoopEntityModifier(final IEntityModifierListener pEntityModifierListener, final int pLoopCount, final ILoopEntityModifierListener pLoopModifierListener, final IEntityModifier pEntityModifier) {
super(pEntityModifier, pLoopCount, pLoopModifierListener, pEntityModifierListener);
}
protected LoopEntityModifier(final LoopEntityModifier pLoopEntityModifier) throws DeepCopyNotSupportedException {
super(pLoopEntityModifier);
}
@Override
public LoopEntityModifier deepCopy() throws DeepCopyNotSupportedException {
return new LoopEntityModifier(this);
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
public interface ILoopEntityModifierListener extends ILoopModifierListener<IEntity> {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
}
}
最后要做的工作就是将修改器绑定到精灵,将精灵放置到场景中
face.registerEntityModifier(shapeModifier);
rect.registerEntityModifier(shapeModifier.deepCopy());
mScene.attachChild(face);
mScene.attachChild(rect);
pOnCreateSceneCallback.onCreateSceneFinished(mScene);
3.制作完成的效果如下图所示
![](https://img-my.csdn.net/uploads/201210/30/1351582983_7084.png)
效果解释:两个精灵在5秒的时间内慢慢变为透明
在1秒时间内变为完全不透明
尺寸在2秒内变为0.5倍大小
暂停等待0.5秒
在三秒内完成由0.5倍大小到5倍大小渐变,同时旋转90度
在三秒内由5被大小变为一倍大小,同时由180度转回0度