AndEngine----CardinalSplineMoveModifierExample之模仿画

2019-04-13 14:45发布

昨天看了下AndEngine里面关于CardinalSplineMoveModifierExample这块的代码,就模仿着写了一个“W”形状。 具体实现如下: /** * 模仿着画出一个W */ public class CardinalSplineMoveModifierMy extends SimpleBaseGameActivity { private int mCameraWidth = 0; private int mCameraHeight = 0; private static final int COUNT_RECTANGLE = 200; private float[] mControlPoint_1_XS = new float[4]; private float[] mControlPoint_2_XS; private float[] mControlPoint_YS; @Override public EngineOptions onCreateEngineOptions() { initCameraSize(); Camera camera = new Camera(0, 0, mCameraWidth, mCameraHeight); return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(mCameraWidth, mCameraHeight), camera); } private void initCameraSize() { mCameraWidth = getResources().getDisplayMetrics().widthPixels; mCameraHeight = getResources().getDisplayMetrics().heightPixels; } @Override protected void onCreateResources() { // Do nothings initControlPointCoordinate(); } private void initControlPointCoordinate() { float[] controlPoint_1 = { 4 * (mCameraWidth / 8), 3 * (mCameraWidth / 8), 2 * (mCameraWidth / 8), 1 * (mCameraWidth / 8) }; mControlPoint_1_XS = controlPoint_1; float[] controlPoint_2 = { 4 * (mCameraWidth / 8), 5 * (mCameraWidth / 8), 6 * (mCameraWidth / 8), 7 * (mCameraWidth / 8) }; mControlPoint_2_XS = controlPoint_2; float[] controlPoint_Y = { 4 * (mCameraHeight / 8), 6 * (mCameraHeight / 8), 4 * (mCameraHeight / 8), 2 * (mCameraHeight / 8) }; mControlPoint_YS = controlPoint_Y; } private static final float DURATION = 4; private static final float SIZE = 25; @Override protected 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_RECTANGLE; 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); // 修改成这样只是为了多几个颜 {MOD},好玩而已 rectangle.setColor(1 - pTension, pDelay, pDelay, 0.5f); } else { rectangle.setColor(pTension, pDelay, pDelay, 0.5f); } // 声明基数样条修改器 final CardinalSplineMoveModifierConfig catmullRomMoveModifierConfig1 = new CardinalSplineMoveModifierConfig( mControlPoint_1_XS.length, -1); final CardinalSplineMoveModifierConfig catmullRomMoveModifierConfig2 = new CardinalSplineMoveModifierConfig( mControlPoint_1_XS.length, -1); for (int i = 0; i < mControlPoint_1_XS.length; i++) { // 设置控制点,catmullRomMoveModifierConfig1表示的是,左边那半部分 catmullRomMoveModifierConfig1.setControlPoint(i, mControlPoint_1_XS[i] - SIZE / 2, mControlPoint_YS[i] - SIZE / 2); // 设置控制点,catmullRomMoveModifierConfig2表示右半部分 catmullRomMoveModifierConfig2.setControlPoint(i, mControlPoint_2_XS[i] - SIZE / 2, mControlPoint_YS[i] - SIZE / 2); } // 给矩形注册修改器 rectangle .registerEntityModifier( // 声明一个顺序修饰器 new SequenceEntityModifier( // 延迟修饰器 new DelayModifier(pDelay), // 循环修饰器 new LoopEntityModifier( // 顺序修饰器 new SequenceEntityModifier( // 集合修饰器 new ParallelEntityModifier( // 基数样条修饰器 new CardinalSplineMoveModifier( DURATION, catmullRomMoveModifierConfig1, EaseLinear .getInstance()), // 旋转修饰器 new RotationModifier(DURATION, -45, -315)), // 集合修饰器 new ParallelEntityModifier( // 基数样条修饰器 new CardinalSplineMoveModifier( DURATION, catmullRomMoveModifierConfig2, EaseLinear .getInstance()), // 旋转修饰器 new RotationModifier(DURATION, 45, 315)))))); pScene.attachChild(rectangle); } }
形状有点丑,但是,确实是“W”。