Android游戏引擎libgdx使用教程6:演员与演出

移动开发 Android
我发现libgdx的更新不是一般快,每天都有几个版本的。那个通过文件配置UI样式让我觉得非常有意思,但是具体操作中有诸多问题。一个个写例子太麻烦了,而且实际运用中也多是多个组合运用,下面来看一个综合性的示例。

上一节讲了常用UI类和舞台,本节我们已经能够很容易的制作一出戏了。

因为actor类在绘制是以x、y值为基准,所以我们可以通过控制x、y值变化演员的位置,但是演员的其他效果需要配合Action类进行操作。

Action类是一个抽象类,所有的具体实现都在com.badlogic.gdx.scenes.scene2d.actions包中。

Android游戏引擎libgdx使用教程6:演员与演出

而包中的类依功能而言可以分为两类:

1、控制Action
2、表现Action

控制Action没有直接表现效果,它操作的对象是表现Action。比如Delay。

表现Action就是直接的表现效果,继承自AnimationAction,操作对象是Actor。比如MoveTo。

现在挨着说吧:

控制类:

Delay $ (Action action, float duration)

延迟duration秒执行action。

Forever $ (Action action)

 一直执行action。

Parallel $ (Action... actions)

并行(同时)执行actions。

Repeat $ (Action action, int times)

重复action times次。

Sequence $ (Action... actions)

按顺序执行actions。

Remove $ ()

删除所有Action。

表现类:

FadeIn $ (float duration) FadeOut $ (float duration)

淡入淡出效果

FadeTo $ (float alpha, float duration)

duration秒改变至alpha。

MoveTo $ (float x, float y, float duration) MoveBy $ (float x, float y, float duration)

用duration移动到x,y处。

RotateBy $ (float rotation, float duration) RotateTo $ (float rotation, float duration)

用duration秒旋转rotation度。

ScaleTo $ (float scaleX, float scaleY, float duration)

缩放x到scaleX,y到scaleY,用时duration秒。

实例演示

一个个写例子太麻烦了,而且实际运用中也多是多个组合运用,下面来看一个综合性的示例:

我们的主角是

 Android游戏引擎libgdx使用教程6:演员与演出

通过action的组合实现闪烁,飞动,旋转等效果。

代码如下:

  1. package com.cnblogs.htynkn.listener;     
  2. import com.badlogic.gdx.ApplicationListener;     
  3. import com.badlogic.gdx.Gdx;     
  4. import com.badlogic.gdx.graphics.GL10;     
  5. import com.badlogic.gdx.graphics.Texture;     
  6. import com.badlogic.gdx.graphics.Texture.TextureFilter;     
  7. import com.badlogic.gdx.math.MathUtils;     
  8. import com.badlogic.gdx.scenes.scene2d.Action;     
  9. import com.badlogic.gdx.scenes.scene2d.Stage;     
  10. import com.badlogic.gdx.scenes.scene2d.actions.FadeIn;     
  11. import com.badlogic.gdx.scenes.scene2d.actions.FadeOut;     
  12. import com.badlogic.gdx.scenes.scene2d.actions.MoveBy;     
  13. import com.badlogic.gdx.scenes.scene2d.actions.MoveTo;     
  14. import com.badlogic.gdx.scenes.scene2d.actions.Parallel;     
  15. import com.badlogic.gdx.scenes.scene2d.actions.Repeat;     
  16. import com.badlogic.gdx.scenes.scene2d.actions.RotateTo;     
  17. import com.badlogic.gdx.scenes.scene2d.actions.Sequence;     
  18. import com.badlogic.gdx.scenes.scene2d.actors.Image;     
  19. public class FirstGame implements ApplicationListener {     
  20. private Stage stage;     
  21. private Texture texture;     
  22. @Override     
  23. public void create() {     
  24. stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),     
  25. true);     
  26. texture = new Texture(Gdx.files.internal("star.png"));     
  27. texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);     
  28. float duration = 4f;     
  29. int maxwidth = Gdx.graphics.getWidth() - texture.getWidth();     
  30. int maxheight = Gdx.graphics.getHeight() - texture.getHeight();     
  31. for (int i = 0; i < 20; i++) {     
  32. Image image = new Image("star" + i, texture);     
  33. image.x = MathUtils.random(0, maxwidth);     
  34. image.y = MathUtils.random(0, Gdx.graphics.getHeight()); //随机出现     
  35. Action moveAction = Sequence.$(MoveTo.$(MathUtils.random(0,     
  36. maxwidth), MathUtils.random(0, maxheight), duration / 2),     
  37. MoveBy.$(MathUtils.random(0, maxwidth), MathUtils.random(0,     
  38. maxheight), duration / 2)); //移动方向和地点随机     
  39. Action rotateAction = RotateTo.$(360, duration); //旋转     
  40. Action fadeAction = Repeat.$(Sequence.$(FadeOut.$(duration / 20),     
  41. FadeIn.$(duration / 20)), 10); //闪烁,重复10次     
  42. image.action(Parallel.$(moveAction, rotateAction, fadeAction)); //所有action并行     
  43. stage.addActor(image);     
  44. }     
  45. Gdx.input.setInputProcessor(stage);     
  46. }     
  47. @Override     
  48. public void dispose() {     
  49. texture.dispose();     
  50. stage.dispose();     
  51. }     
  52. @Override     
  53. public void pause() {     
  54. // TODO Auto-generated method stub     
  55. }     
  56. @Override     
  57. public void render() {     
  58. Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);     
  59. stage.act(Gdx.graphics.getDeltaTime());     
  60. stage.draw();     
  61. }     
  62. @Override     
  63. public void resize(int width, int height) {     
  64. // TODO Auto-generated method stub     
  65. }     
  66. @Override     
  67. public void resume() {     
  68. // TODO Auto-generated method stub     
  69. }     
  70. }   

其实每个action的用法都很简单,主要问题是怎么组合排列来显示一种符合需求的效果。

我发现libgdx的更新不是一般快,每天都有几个版本的。那个通过文件配置UI样式让我觉得非常有意思,但是具体操作中有诸多问题。

责任编辑:闫佳明 来源: jizhuomi
相关推荐

2013-12-04 16:28:29

Android游戏引擎libgdx教程

2013-12-04 13:30:45

Android游戏引擎libgdx教程

2013-12-06 10:31:14

Android游戏引擎libgdx教程

2013-12-06 09:59:53

Android游戏引擎libgdx教程

2013-12-04 17:14:57

Android游戏引擎libgdx教程

2013-12-04 16:21:02

Android游戏引擎libgdx教程

2013-12-06 10:12:49

Android游戏引擎libgdx教程

2013-12-06 10:22:42

Android游戏引擎libgdx教程

2013-12-04 16:07:27

Android游戏引擎libgdx教程

2013-12-06 10:35:28

Android游戏引擎libgdx教程

2011-07-18 12:29:10

2011-07-18 11:07:12

iPhone 游戏 引擎

2011-07-18 10:53:09

2011-07-18 11:23:29

iPhone 游戏 动画

2011-07-18 11:39:58

iPhone 游戏 引擎

2014-06-03 09:35:19

Javalibgdx

2014-07-15 10:34:14

Android游戏引擎

2014-07-17 11:10:19

Android开源游戏引擎

2011-05-31 15:45:38

Android 游戏引擎 开源

2013-06-07 13:20:16

Android开发开源游戏引擎游戏开发
点赞
收藏

51CTO技术栈公众号