剛看了下CardinalSplineMoveModifierExample,將裏面大致看了下,寫了點註釋,代碼主題如下:
public class CardinalSplineMoveModifierExample extends SimpleBaseGameActivity {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private static final int COUNT = 400;
private static final float DURATION = 4;
private static final float SIZE = 25;
private static final float[] CONTROLPOINT_1_XS = { 2 * (CAMERA_WIDTH / 4),
1 * (CAMERA_WIDTH / 4), 1.5f * (CAMERA_WIDTH / 4),
2 * (CAMERA_WIDTH / 4) };
private static final float[] CONTROLPOINT_2_XS = { 2 * (CAMERA_WIDTH / 4),
3 * (CAMERA_WIDTH / 4), 2.5f * (CAMERA_WIDTH / 4),
2 * (CAMERA_WIDTH / 4) };
private static final float[] CONTROLPOINT_YS = {
3.5f * (CAMERA_HEIGHT / 4), 2 * (CAMERA_HEIGHT / 4),
1 * (CAMERA_HEIGHT / 4), 1.5f * (CAMERA_HEIGHT / 4), };
// ===========================================================
// Fields
// ===========================================================
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@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() {
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0, 0, 0));
for (int i = 0; i < COUNT; i++) {
final float tension = MathUtils.random(-0.5f, 0.5f);
this.addRectangleWithTension(scene, tension,
MathUtils.random(0, DURATION * 2f));
}
return scene;
}
private void addRectangleWithTension(final Scene pScene,
final float pTension, float pDelay) {
// 聲明矩形
final Rectangle rectangle = new Rectangle(-SIZE, -SIZE, SIZE, SIZE,
this.getVertexBufferObjectManager());
rectangle.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE);
if (pTension < 0) {
// 此處原本爲rectangle.setColor(1 - pTension, 0, 0, 0.5f);
// 修改成這樣只是爲了多幾個顏色,好玩而已
rectangle.setColor(1 - pTension, pDelay, pDelay, 0.5f);
} else {
rectangle.setColor(pTension, pDelay, pDelay, 0.5f);
}
// 聲明基數樣條修改器
final CardinalSplineMoveModifierConfig catmullRomMoveModifierConfig1 = new CardinalSplineMoveModifierConfig(
CardinalSplineMoveModifierExample.CONTROLPOINT_1_XS.length,
pTension);
final CardinalSplineMoveModifierConfig catmullRomMoveModifierConfig2 = new CardinalSplineMoveModifierConfig(
CardinalSplineMoveModifierExample.CONTROLPOINT_1_XS.length,
pTension);
for (int i = 0; i < CardinalSplineMoveModifierExample.CONTROLPOINT_1_XS.length; i++) {
// 設置控制點,catmullRomMoveModifierConfig1表示的是,左邊那半部分
catmullRomMoveModifierConfig1.setControlPoint(i,
CardinalSplineMoveModifierExample.CONTROLPOINT_1_XS[i]
- SIZE / 2,
CardinalSplineMoveModifierExample.CONTROLPOINT_YS[i] - SIZE
/ 2);
// 設置控制點,catmullRomMoveModifierConfig2表示右半部分
catmullRomMoveModifierConfig2.setControlPoint(i,
CardinalSplineMoveModifierExample.CONTROLPOINT_2_XS[i]
- SIZE / 2,
CardinalSplineMoveModifierExample.CONTROLPOINT_YS[i] - SIZE
/ 2);
}
// 給矩形註冊修改器
rectangle
.registerEntityModifier(
// 聲明一個順序修飾器
new SequenceEntityModifier(
// 延遲修飾器
new DelayModifier(pDelay),
// 循環修飾器
new LoopEntityModifier(
// 順序修飾器
new SequenceEntityModifier(
// 集合修飾器
new ParallelEntityModifier(
// 基數樣條修飾器
new CardinalSplineMoveModifier(
CardinalSplineMoveModifierExample.DURATION,
catmullRomMoveModifierConfig1,
EaseLinear
.getInstance()),
// 旋轉修飾器
new RotationModifier(
CardinalSplineMoveModifierExample.DURATION,
-45, -315)),
// 集合修飾器
new ParallelEntityModifier(
// 基數樣條修飾器
new CardinalSplineMoveModifier(
CardinalSplineMoveModifierExample.DURATION,
catmullRomMoveModifierConfig2,
EaseLinear
.getInstance()),
// 旋轉修飾器
new RotationModifier(
CardinalSplineMoveModifierExample.DURATION,
45, 315))))));
pScene.attachChild(rectangle);
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
早上起來,爭取能防着寫一個W形狀出來!