libgdx的作者刻意的使用了舞台、演员,为了方便我们去开发游戏,其实在作者还提供给我们了一个更重要的类,那就是Action类,也叫动作类或演出类,很多朋友希望我能讲解下关于新版本的Action类是如何使用的,其实Action类分为2种,一种是负责演员表现的表现类,另一种是负责控制表现类的控制类。
一、表现类
包括旋转、放缩、移动、淡入、淡出、延迟等动作
注意:Action中所有动作都是static静态的,因此直接使用即可而不需要去实例化,直接通过Actions类进行生产即可。
(1)AlphaAction 类**:**负责改变演员的透明度。
AlphaAction alphah = Actions.alpha(0.6f, 20);
(2)ColorAction 类**:**负责改变演员的颜色。
ColorAction color= Actions.color(Color.RED, 20);
### (3)DelayAction 类**:**负责延迟执行演员动作。
DelayAction delay = Actions.delay(20.0f);
### (4)fade in / fade out 效果**:通过在固定的时间内改变演员的透明度,同时实现淡入淡出类,这个其实是AlphaAction的一个特例。
AlphaAction alpha = Actions.fadeIn(duration);
AlphaAction alpha = Actions.fadeOut(duration);
方法①:fadeIn(float duration)
在传入的时间内,透明度从初始值达到 1 ,实现淡入的效果。方法②:[fadeOut](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/actions/Actions.html#fadeOut(float))(float <wbr />duration)
**在传入的时间内,透明度从初始值达到 0 ,实现淡出的效果。### (5)MoveToAction 类**:**负责改变演员的位置
MoveToAction moveto = Actions.moveTo(240, 160, duration);
(6)RotatoToAction 类**:**负责实现演员的旋转效果
RotateToAction rotateto = Actions.rotateTo(rotate, duration);
(7)ScaleToAction 类**:**负责改变演员大小的类,即沿X轴或者Y轴放缩大小,实现改变Actor大小的效果
ScaleToAction scaleto = Actions.scaleTo(scale, scale, duration);
>
二、控制**类**
###
(1) SequenceAction 类: 按顺序执行Actions
[sequence](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/actions/Actions.html#sequence(com.badlogic.gdx.scenes.scene2d.Action,%20com.badlogic.gdx.scenes.scene2d.Action,%20com.badlogic.gdx.scenes.scene2d.Action,%20com.badlogic.gdx.scenes.scene2d.Action))([Action](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/Action.html "class in com.badlogic.gdx.scenes.scene2d") <wbr />action1, <wbr />[Action](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/Action.html "class in com.badlogic.gdx.scenes.scene2d") <wbr />action2, <wbr />[Action](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/Action.html "class in com.badlogic.gdx.scenes.scene2d") <wbr />action3, .....)
SequenceAction alpha = Actions.sequence(Actions.fadeIn(duration), endAction );
###
(2) ** ParallelAction 类:**并行执行传入的Action类
[parallel](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/actions/Actions.html#parallel(com.badlogic.gdx.scenes.scene2d.Action,%20com.badlogic.gdx.scenes.scene2d.Action,%20com.badlogic.gdx.scenes.scene2d.Action))([Action](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/Action.html "class in com.badlogic.gdx.scenes.scene2d") <wbr />action1, <wbr />[Action](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/Action.html "class in com.badlogic.gdx.scenes.scene2d") <wbr />action2, <wbr />[Action](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/Action.html "class in com.badlogic.gdx.scenes.scene2d") <wbr />action3)
ParallelAction Paction = Actions.parallel(moveto, rotateto, alpha);
###
### (3) RepeatAction
[repeat](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/actions/Actions.html#repeat(int,%20com.badlogic.gdx.scenes.scene2d.Action))(int <wbr />count, <wbr />[Action](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/Action.html "class in com.badlogic.gdx.scenes.scene2d") <wbr />repeatedAction)
RepeatAction repeatedAction = Actions.repeat(3, Paction);
###
### (4) foreverAction
[forever](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/actions/Actions.html#forever(com.badlogic.gdx.scenes.scene2d.Action))([Action](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/Action.html "class in com.badlogic.gdx.scenes.scene2d") <wbr />repeatedAction)
RepeatAction foreverAction = Actions.forever(RepeatAction);
传入的是一个重复Action,次数是无穷的,当时他的类型也是一个重复Action。
三、Run方法
所有Actions结束后,调用该方法,需要传入Runnable的一个方法。
[run](file:///E:/libgdx-0.9.7/docs/api/com/badlogic/gdx/scenes/scene2d/actions/Actions.html#run(java.lang.Runnable))(java.lang.Runnable <wbr />runnable)
Action endAction = Actions.run(new Runnable() {
@Override
public void run() {
System.out.println(“All action is completed”);
}
});
文章内容参考-奋斗小土豆丶的博文,特此说明!
四、实例代码
1.所用素材 :图片(白色球,可能因为背景原因会看不到) 图片(银色星星)
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);
}</pre>
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);
}</pre>
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;
}
}