public class EaseFunctionExample extends BaseExample {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 720;//游戏摄像机用以调节屏幕的宽度
private static final int CAMERA_HEIGHT = 480;//游戏摄像机用以调节屏幕的高度
// ===========================================================
// Fields
// ===========================================================
private Camera mCamera;//新建游戏摄像机
private Texture mFontTexture;//新建Texture 用以加载纹理
private Font mFont;//设置字体
private Texture mTexture;
private TextureRegion mFaceTextureRegion;//Texture加载纹理需要借助TextureRegion
private TextureRegion mNextTextureRegion;
//设置二维数组,用以显示提示字
private static final IEaseFunction[][] EASEFUNCTIONS = new IEaseFunction[][]{
new IEaseFunction[] {
EaseLinear.getInstance(),
EaseLinear.getInstance(),
EaseLinear.getInstance()
},
new IEaseFunction[] {
EaseBackIn.getInstance(),
EaseBackOut.getInstance(),
EaseBackInOut.getInstance()
},
new IEaseFunction[] {
EaseBounceIn.getInstance(),
EaseBounceOut.getInstance(),
EaseBounceInOut.getInstance()
},
new IEaseFunction[] {
EaseCircularIn.getInstance(),
EaseCircularOut.getInstance(),
EaseCircularInOut.getInstance()
},
new IEaseFunction[] {
EaseCubicIn.getInstance(),
EaseCubicOut.getInstance(),
EaseCubicInOut.getInstance()
},
new IEaseFunction[] {
EaseElasticIn.getInstance(),
EaseElasticOut.getInstance(),
EaseElasticInOut.getInstance()
},
new IEaseFunction[] {
EaseExponentialIn.getInstance(),
EaseExponentialOut.getInstance(),
EaseExponentialInOut.getInstance()
},
new IEaseFunction[] {
EaseQuadIn.getInstance(),
EaseQuadOut.getInstance(),
EaseQuadInOut.getInstance()
},
new IEaseFunction[] {
EaseQuartIn.getInstance(),
EaseQuartOut.getInstance(),
EaseQuartInOut.getInstance()
},
new IEaseFunction[] {
EaseQuintIn.getInstance(),
EaseQuintOut.getInstance(),
EaseQuintInOut.getInstance()
},
new IEaseFunction[] {
EaseSineIn.getInstance(),
EaseSineOut.getInstance(),
EaseSineInOut.getInstance()
},
new IEaseFunction[] {
EaseStrongIn.getInstance(),
EaseStrongOut.getInstance(),
EaseStrongInOut.getInstance()
}
};
private int mCurrentEaseFunctionSet = 0;
private final Sprite[] mFaces = new Sprite[3];
private final ChangeableText[] mEaseFunctionNameTexts = new ChangeableText[3];//设置可变字体
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public Engine onLoadEngine() {
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
}
@Override
public void onLoadResources() {
/* The font. */
this.mFontTexture = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);//TextureOptions.BILINEAR_PREMULTIPLYALPHA渲染方式
this.mFont = new Font(this.mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.WHITE);//字体
this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
this.mEngine.getFontManager().loadFont(this.mFont);
/* The textures. */
this.mTexture = new Texture(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);//建立256*128的贴图区域
this.mNextTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/next.png", 0, 0);//最后两个参数图片在贴图区域的坐标,注意不能超出贴图区域
this.mFaceTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/badge.png", 97, 0);
this.mEngine.getTextureManager().loadTexture(this.mTexture);
}
@Override
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene(1);
final HUD hud = new HUD();//利用hud绘制游戏屏幕于手机上
//图像在屏幕上的位置
final Sprite nextSprite = new Sprite(CAMERA_WIDTH - 100 - this.mNextTextureRegion.getWidth(), 0, this.mNextTextureRegion){
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
EaseFunctionExample.this.next();
}
return true;
};
};
//一个Region只能对应一个运动的AnimateSprite,要生成copy,用clone()方法
final Sprite previousSprite = new Sprite(100, 0, this.mNextTextureRegion.clone()){
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
EaseFunctionExample.this.previous();
}
return true;
};
};
//设置Texture横向翻转
previousSprite.getTextureRegion().setFlippedHorizontal(true);
hud.getTopLayer().addEntity(nextSprite);
hud.getTopLayer().addEntity(previousSprite);
hud.registerTouchArea(nextSprite);
hud.registerTouchArea(previousSprite);
this.mCamera.setHUD(hud);
/* Create the sprites that will be moving. */
//设置精灵位置
this.mFaces[0] = new Sprite(0, CAMERA_HEIGHT - 300, this.mFaceTextureRegion);
this.mFaces[1] = new Sprite(0, CAMERA_HEIGHT - 200, this.mFaceTextureRegion);
this.mFaces[2] = new Sprite(0, CAMERA_HEIGHT - 100, this.mFaceTextureRegion);
//设置文字位置
this.mEaseFunctionNameTexts[0] = new ChangeableText(0, CAMERA_HEIGHT - 250, this.mFont, "Function", 20);
this.mEaseFunctionNameTexts[1] = new ChangeableText(0, CAMERA_HEIGHT - 150, this.mFont, "Function", 20);
this.mEaseFunctionNameTexts[2] = new ChangeableText(0, CAMERA_HEIGHT - 50, this.mFont, "Function", 20);
final ILayer topLayer = scene.getTopLayer();
topLayer.addEntity(this.mFaces[0]);
topLayer.addEntity(this.mFaces[1]);
topLayer.addEntity(this.mFaces[2]);
topLayer.addEntity(this.mEaseFunctionNameTexts[0]);
topLayer.addEntity(this.mEaseFunctionNameTexts[1]);
topLayer.addEntity(this.mEaseFunctionNameTexts[2]);
//画三条分隔线
topLayer.addEntity(new Line(0, CAMERA_HEIGHT - 110, CAMERA_WIDTH, CAMERA_HEIGHT - 110));
topLayer.addEntity(new Line(0, CAMERA_HEIGHT - 210, CAMERA_WIDTH, CAMERA_HEIGHT - 210));
topLayer.addEntity(new Line(0, CAMERA_HEIGHT - 310, CAMERA_WIDTH, CAMERA_HEIGHT - 310));
return scene;
}
@Override
public void onLoadComplete() {
this.reanimate();
}
// ===========================================================
// Methods
// ===========================================================
public void next() {
this.mCurrentEaseFunctionSet++;
this.mCurrentEaseFunctionSet %= EASEFUNCTIONS.length;
this.reanimate();
}
public void previous() {
this.mCurrentEaseFunctionSet--;
if(this.mCurrentEaseFunctionSet < 0) {
this.mCurrentEaseFunctionSet += EASEFUNCTIONS.length;
}
this.reanimate();
}
private void reanimate() {
this.runOnUpdateThread(new Runnable() {
@Override
public void run() {
final IEaseFunction[] currentEaseFunctionsSet = EASEFUNCTIONS[EaseFunctionExample.this.mCurrentEaseFunctionSet];
final ChangeableText[] easeFunctionNameTexts = EaseFunctionExample.this.mEaseFunctionNameTexts;
final Sprite[] faces = EaseFunctionExample.this.mFaces;
for(int i = 0; i < 3; i++) {
easeFunctionNameTexts[i].setText(currentEaseFunctionsSet[i].getClass().getSimpleName());
final Sprite face = faces[i];
face.clearShapeModifiers();
final float y = face.getY();
face.setPosition(0, y);
face.addShapeModifier(new MoveModifier(3, 0, CAMERA_WIDTH - face.getWidth(), y, y, currentEaseFunctionsSet[i]));
}
}
});
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}