四、实例代码
1.所用素材 :图片(白色球,可能因为背景原因会看不到) 图片(银色星星)
2.功能分析:
(1)设计一个滑屏可以出现星星和圆球效果的项目。
(2)使用星星和圆球实现旋转、移动、淡入淡出效果。
(3)所有动作结束后,打印一句话,解释动作结束后,执行的动作。
3.创建舞台,实例化需要用的纹理图片,同时实现输入接口,来处理触屏事件。
stage = new Stage(); starTexture = new Texture(Gdx.files.internal("study/action/star.png")); ballTexture = new Texture(Gdx.files.internal("study/action/ball.png")); Gdx.input.setInputProcessor(this);
4.重写touchDragged方法,触摸屏幕时实例化出小球或星星
@Override public boolean touchDragged(int screenX, int screenY, int pointer) { createStar(MathUtils.randomBoolean(), screenX, 480 - screenY,MathUtils.random(10, 20)); return false; }
5.实例化出小球或星星
//利用随机boolean来随机创建ball或star private void createStar(boolean randomBoolean, int x, int y, int size) { if (randomBoolean == true) { image = new Image(starTexture); } else if (randomBoolean == false) { image = new Image(ballTexture); } this.setAction(image); image.setPosition(x - size / 2, y - size / 2); image.setSize(size, size); stage.addActor(image); }
6.编写setAction(image)方法,把传进行的image对象(image也是actor)进行动作设置
public void setAction(final Image image) { float duration = MathUtils.random(1, 5); float scale = MathUtils.random(0.5f, 2.0f); float rotate = MathUtils.random(360); Action endAction = Actions.run(new Runnable() { @Override public void run() { System.out.println("All action is completed"); } }); MoveToAction moveto = Actions.moveTo(320, 240, duration); ScaleToAction scaleto = Actions.scaleTo(scale, scale, duration); RotateToAction rotateto = Actions.rotateTo(rotate, duration); SequenceAction alpha = Actions.sequence(Actions.fadeIn(duration),Actions.fadeOut(duration), endAction); ParallelAction Paction = Actions.parallel(moveto, scaleto, rotateto,alpha); RepeatAction repeatedAction = Actions.repeat(3, Paction); RepeatAction foreverAction = Actions.forever(repeatedAction); image.addAction(Paction); //image.addAction(foreverAction); //image.addAction(repeatedAction); }
7.在Render方法中进行舞台刷新
Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw();
完整代码:
package com.mygdx.game; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.scenes.scene2d.Action; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.actions.Actions; import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction; import com.badlogic.gdx.scenes.scene2d.actions.ParallelAction; import com.badlogic.gdx.scenes.scene2d.actions.RepeatAction; import com.badlogic.gdx.scenes.scene2d.actions.RotateToAction; import com.badlogic.gdx.scenes.scene2d.actions.ScaleToAction; import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction; import com.badlogic.gdx.scenes.scene2d.ui.Image; /** * @author Administrator * */ public class MyGdxGame extends ApplicationAdapter implements InputProcessor { Texture starTexture; Texture ballTexture; Stage stage; Image image; @Override public void create() { stage = new Stage(); starTexture = new Texture(Gdx.files.internal("study/action/star.png")); ballTexture = new Texture(Gdx.files.internal("study/action/ball.png")); Gdx.input.setInputProcessor(this); } @Override public void render() { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } public void setAction(final Image image) { float duration = MathUtils.random(1, 5); float scale = MathUtils.random(0.5f, 2.0f); float rotate = MathUtils.random(360); Action endAction = Actions.run(new Runnable() { @Override public void run() { System.out.println("All action is completed"); } }); MoveToAction moveto = Actions.moveTo(320, 240, duration); ScaleToAction scaleto = Actions.scaleTo(scale, scale, duration); RotateToAction rotateto = Actions.rotateTo(rotate, duration); SequenceAction alpha = Actions.sequence(Actions.fadeIn(duration),Actions.fadeOut(duration), endAction); ParallelAction Paction = Actions.parallel(moveto, scaleto, rotateto,alpha); RepeatAction repeatedAction = Actions.repeat(3, Paction); RepeatAction foreverAction = Actions.forever(repeatedAction); image.addAction(Paction); //image.addAction(foreverAction); //image.addAction(repeatedAction); } @Override public boolean touchDragged(int screenX, int screenY, int pointer) { createStar(MathUtils.randomBoolean(), screenX, 480 - screenY,MathUtils.random(10, 20)); return false; } //利用随机boolean来随机创建ball或star private void createStar(boolean randomBoolean, int x, int y, int size) { if (randomBoolean == true) { image = new Image(starTexture); } else if (randomBoolean == false) { image = new Image(ballTexture); } this.setAction(image); image.setPosition(x - size / 2, y - size / 2); image.setSize(size, size); stage.addActor(image); } @Override public boolean keyDown(int keycode) { return false; } @Override public boolean keyUp(int keycode) { return false; } @Override public boolean keyTyped(char character) { return false; } @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { return false; } @Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { return false; } @Override public boolean mouseMoved(int screenX, int screenY) { return false; } @Override public boolean scrolled(int amount) { return false; } }
Comments 1 条评论
值得借鉴!