自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android游戲引擎libgdx使用教程6:演員與演出

移動開發(fā) Android
我發(fā)現(xiàn)libgdx的更新不是一般快,每天都有幾個版本的。那個通過文件配置UI樣式讓我覺得非常有意思,但是具體操作中有諸多問題。一個個寫例子太麻煩了,而且實際運用中也多是多個組合運用,下面來看一個綜合性的示例。

上一節(jié)講了常用UI類和舞臺,本節(jié)我們已經能夠很容易的制作一出戲了。

因為actor類在繪制是以x、y值為基準,所以我們可以通過控制x、y值變化演員的位置,但是演員的其他效果需要配合Action類進行操作。

Action類是一個抽象類,所有的具體實現(xiàn)都在com.badlogic.gdx.scenes.scene2d.actions包中。

Android游戲引擎libgdx使用教程6:演員與演出

而包中的類依功能而言可以分為兩類:

1、控制Action
2、表現(xiàn)Action

控制Action沒有直接表現(xiàn)效果,它操作的對象是表現(xiàn)Action。比如Delay。

表現(xiàn)Action就是直接的表現(xiàn)效果,繼承自AnimationAction,操作對象是Actor。比如MoveTo。

現(xiàn)在挨著說吧:

控制類:

Delay $ (Action action, float duration)

延遲duration秒執(zhí)行action。

Forever $ (Action action)

 一直執(zhí)行action。

Parallel $ (Action... actions)

并行(同時)執(zhí)行actions。

Repeat $ (Action action, int times)

重復action times次。

Sequence $ (Action... actions)

按順序執(zhí)行actions。

Remove $ ()

刪除所有Action。

表現(xiàn)類:

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的組合實現(xiàn)閃爍,飛動,旋轉等效果。

代碼如下:

  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()); //隨機出現(xiàn)     
  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的用法都很簡單,主要問題是怎么組合排列來顯示一種符合需求的效果。

我發(fā)現(xiàn)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游戲引擎

2011-05-31 15:45:38

Android 游戲引擎 開源

2014-07-17 11:10:19

Android開源游戲引擎

2013-06-07 13:20:16

Android開發(fā)開源游戲引擎游戲開發(fā)
點贊
收藏

51CTO技術棧公眾號