【游戏引擎LibGdx】利用演员制作简单动画(简单仿制保卫萝卜开头画面)

来源:互联网 发布:sybase数据库用的多吗 编辑:程序博客网 时间:2024/04/29 20:24

本文章使用的是libgdx 0.98的版本,请注意。原文地址http://leake2546.sinaapp.com/?p=7
目的:模仿保卫萝卜的开头画面。
原理:利用Action 的各种组合实现。
分析:萝卜头的萝卜叶在不停的晃动。萝卜叶子是一个Actor,通过不断旋转实现晃动的效果。
那么接下的工作就清晰了,就是:
1.首先把这些萝卜叶子的Actor绘制上去,摆好位置;
2.叶子进行旋转动作;
3.不断重复旋转;

先看效果图:

【不知道为什么插入不了视频。。。只能委屈大家点击看效果图】
运行效果

首先需要定义一个舞台,用于承载这些叶子;
Stage stage;

接下来,就是把叶子绘制到stage上面。
这里我使用了TexturePacker工具,把所有图片都打包起来,既压缩了图片,又方便读取,推荐使用。
读取打包的文件,找打对应的几个叶子,add到舞台上,设置好对应位置。

主要用到的素材:

robot_leaf1robot_leaf1

robot_leaf2robot_leaf2

robot_leaf3robot_leaf3

robotrobot

 

 

代码如下:

TextureAtlas spriteSheet = new TextureAtlas(
Gdx.files.internal("luobo/mainscreen.txt"));
AtlasRegion btn_mostorRegion = spriteSheet.findRegion("menu_monstor");
AtlasRegion btn_mostor_clickRegion = spriteSheet
.findRegion("menu_monstor_click");
ImageButton btn_mostor = new ImageButton(new TextureRegionDrawable(
btn_mostorRegion), new TextureRegionDrawable(
btn_mostor_clickRegion));
stage.addActor(btn_mostor);
btn_mostor.setPosition(
20 + btn_advance.getWidth() + btn_bossmode.getWidth(), 20);

AtlasRegion robotRegion = spriteSheet.findRegion("robot");
AtlasRegion robotr_leafRegion1 = spriteSheet.findRegion("robot_leaf1");
AtlasRegion robotr_leafRegion2 = spriteSheet.findRegion("robot_leaf2");
AtlasRegion robotr_leafRegion3 = spriteSheet.findRegion("robot_leaf3");

Image robotImage = new Image(robotRegion);
Image robot_leafImage1 = new Image(robotr_leafRegion1);
robot_leafImage2 = new Image(robotr_leafRegion2);
robot_leafImage3 = new Image(robotr_leafRegion3);

stage.addActor(robot_leafImage1);
stage.addActor(robot_leafImage3);
stage.addActor(robot_leafImage2);
stage.addActor(robotImage);

robotImage.setPosition(Gdx.graphics.getWidth()*2/5, Gdx.graphics.getHeight()*3/7);
robot_leafImage1.setPosition(robotImage.getX() - 50,robotImage.getY()+robot_leafImage1.getHeight());
robot_leafImage2.setPosition(robotImage.getX() + 30,robotImage.getY()+robot_leafImage2.getHeight());
robot_leafImage3.setPosition(robotImage.getX() + robot_leafImage2.getWidth(),robotImage.getY()+robot_leafImage3.getHeight());

 

把叶子绘制上去之后,就只差晃动的动作了。

利用Action,里面有:
RotateToAction--顾名思义就是进行旋转的动作。
用法:RotateToAction rotateToAction = Actions.rotateTo(角度,旋转的时间);
或者new 一个RotateToAciton对象,然后设置对应的角度和旋转时间。

SequenceAction--顺序执行的动作,里面有一系列动作,顺序执行。
用法: SequenceAction sequenceAction = Actions.sequence(action1, action2, action3,action4...);
添加N个action进去,按照action1,action2,aciton3,action4..的顺序执行下去,执行完即停止。
或者new 一个SequenceAction对象,然后设置对应参数。

RepeatAction---循环动作。通过设置循环次数来进行循环,如果循环次数设置设置为FOREVER的话,就无线循环下去。
用法:RepeatAction repeatAction = Actions.repeat(循环次数, 循环执行的Action);
循环次数可以为任意正整数,循环次数为FORERVER的相当于等于-1;
RepeatAction.FOREVER=-1

DelayAction---延时,一般用于暂停循环动作或者顺序执行动作。
用法:DelayAction delayAction = Actions.delay(时间);

 

单个叶子晃动的逻辑是:顺时针转10度,逆时针转10度,顺时针转10度,逆时针转10度,然后停止2秒,继续下次的晃动。

int rotate = 5;
float rotateDuration = 0.1f;

RotateToAction rotateToAction1 = Actions.rotateTo(rotate,
rotateDuration);
RotateToAction rotateToAction2 = Actions.rotateTo(-rotate,
rotateDuration);
RotateToAction rotateToAction3 = Actions.rotateTo(rotate,
rotateDuration);
RotateToAction rotateToAction4 = Actions.rotateTo(-rotate,
rotateDuration);

DelayAction delayAction = Actions.delay(2f);

SequenceAction sequenceAction = Actions.sequence(
rotateToAction1, rotateToAction2, rotateToAction3,
rotateToAction4, delayAction);

RepeatAction repeatAction = Actions.repeat(
RepeatAction.FOREVER, sequenceAction);
robot_leafImage3.addAction(repeatAction);

 

 

 

下面是完整添加动画动作逻辑的函数代码:

public void addAnimation() {

SequenceAction sequenceAction = this.addLeafAction();

Action endAction = Actions.run(new Runnable() {

@Override
public void run() {
if (robot_leafImage3.getActions().size <= 0) {
// SequenceAction sequenceAction =
// StartScreen.this.addLeafAction();
int rotate = 5;
float rotateDuration = 0.1f;

RotateToAction rotateToAction1 = Actions.rotateTo(rotate,
rotateDuration);
RotateToAction rotateToAction2 = Actions.rotateTo(-rotate,
rotateDuration);
RotateToAction rotateToAction3 = Actions.rotateTo(rotate,
rotateDuration);
RotateToAction rotateToAction4 = Actions.rotateTo(-rotate,
rotateDuration);

DelayAction delayAction = Actions.delay(2f);

SequenceAction sequenceAction = Actions.sequence(
rotateToAction1, rotateToAction2, rotateToAction3,
rotateToAction4, delayAction);

RepeatAction repeatAction = Actions.repeat(
RepeatAction.FOREVER, sequenceAction);
robot_leafImage3.addAction(repeatAction);
}
}
});
sequenceAction.addAction(endAction);
RepeatAction repeatAction = Actions.repeat(RepeatAction.FOREVER,
sequenceAction);
robot_leafImage2.addAction(repeatAction);

}

public SequenceAction addLeafAction() {
int rotate = 5;
float rotateDuration = 0.1f;

RotateToAction rotateToAction1 = Actions.rotateTo(rotate,
rotateDuration);
RotateToAction rotateToAction2 = Actions.rotateTo(-rotate,
rotateDuration);
RotateToAction rotateToAction3 = Actions.rotateTo(rotate,
rotateDuration);
RotateToAction rotateToAction4 = Actions.rotateTo(-rotate,
rotateDuration);

DelayAction delayAction = Actions.delay(2f);

SequenceAction sequenceAction = Actions.sequence(delayAction,
rotateToAction1, rotateToAction2, rotateToAction3,
rotateToAction4, delayAction);

return sequenceAction;
}