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

Android游戲引擎libgdx使用教程4:舞臺和演員的游戲?qū)嵗?/h1>

移動開發(fā) Android
演員是游戲設計中常用的一個對象,它接受舞臺的統(tǒng)一管理,擁有一些公共的事件,比如觸摸,點擊,但是同時還有自身的響應和屬性;而舞臺就是容納演員的場所。它統(tǒng)一管理所有演員,接受輸入,同時提供一個方便的框架操作演員的時間變化。

大家先要有舞臺和演員的概念,下面通過一個游戲截圖來說明:

Android游戲引擎libgdx使用教程4:舞臺和演員的游戲?qū)嵗? /></p>
<p>請仔細觀察圖片中的元素,有些東西是不能動,有些可以動,有些有特效,有些沒有,有各種圖片,但是其實它們都可以統(tǒng)一稱為演員(Actor)。圖中用框標出的就是演員。而整個游戲界面就是我們的舞臺。</p>
<p>演員是游戲設計中常用的一個對象,它接受舞臺的統(tǒng)一管理,擁有一些公共的事件,比如觸摸,點擊,但是同時還有自身的響應和屬性;而舞臺就是容納演員的場所。它統(tǒng)一管理所有演員,接受輸入,同時提供一個方便的框架操作演員的時間變化。</p>
<p>Stage類</p>
<p>我們來看一下Stage類:</p>
<p>       protected final Group root;</p>
<p>       protected final SpriteBatch batch;</p>
<p>       protected Camera camera;</p>
<p>它擁有一個Group,一個SpriteBatch,還有一個相機。</p>
<p>SpriteBatch我們在前幾篇說過,這里就不再重復了。</p>
<p>Group是一個類,用于容納和控制演員。但是這里要注意Group本身其實也是繼承自Actor。</p>
<p>相機我們這里跳過,以后再說,可以暫時理解成一個控制觀察視角和指標轉(zhuǎn)化的工具。</p>
<p>當我們擁有一個演員后就可以調(diào)用addActor方法加入舞臺。</p>
<p>舞臺可以獲取輸入,但是需要設置。</p>
<p>       Gdx.input.setInputProcessor(stage);</p>
<p>舞臺和演員實例分享</p>
<p>下面來個列子,控制一個人物前進。</p>
<p style=Android游戲引擎libgdx使用教程4:舞臺和演員的游戲?qū)嵗? /></p>
<p>控制人物的按鈕:</p>
<p style=Android游戲引擎libgdx使用教程4:舞臺和演員的游戲?qū)嵗? /></p>
<p>將所需的圖片放到assert中:</p>
<p style=Android游戲引擎libgdx使用教程4:舞臺和演員的游戲?qū)嵗? /></p>
<p>新建三個類:</p>
<p>FirstGame,實現(xiàn)接口ApplicationListener</p>
<p>FirstActor,繼承Actor</p>
<p>NarrowButton,繼承Actor</p>
<p>先看一下FirstGame</p>
<p>聲明一個Stage,然后實例化FirstActor和NarrowButton,將二者加入舞臺中,最后設置輸入響應為Stage。</p>
<pre><ol class=

  • package com.cnblogs.htynkn.listener;     
  • import java.util.Date;     
  • import java.util.Random;     
  • import javax.microedition.khronos.opengles.GL;     
  • import android.util.Log;     
  • import com.badlogic.gdx.ApplicationListener;     
  • import com.badlogic.gdx.Gdx;     
  • import com.badlogic.gdx.graphics.GL10;     
  • import com.badlogic.gdx.graphics.g2d.BitmapFont;     
  • import com.badlogic.gdx.scenes.scene2d.Stage;     
  • import com.cnblogs.htynkn.domain.FirstActor;     
  • import com.cnblogs.htynkn.domain.NarrowButton;     
  • public class FirstGame implements ApplicationListener {     
  • private Stage stage;     
  • private FirstActor firstActor;     
  • private NarrowButton button;     
  • @Override     
  • public void create() {     
  • stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),     
  • true);     
  • firstActor = new FirstActor("renwu");     
  • button = new NarrowButton("narrow");     
  • stage.addActor(firstActor);     
  • stage.addActor(button);     
  • Gdx.input.setInputProcessor(stage);     
  • }     
  • @Override     
  • public void dispose() {     
  • stage.dispose();     
  • }     
  • @Override     
  • public void pause() {     
  • // TODO Auto-generated method stub     
  • }     
  • @Override     
  • public void render() {     
  • Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);     
  • stage.act(Gdx.graphics.getDeltaTime());     
  • stage.draw();     
  • }     
  • @Override     
  • public void resize(int width, int height) {     
  • // TODO Auto-generated method stub     
  • }     
  • @Override     
  • public void resume() {     
  • // TODO Auto-generated method stub     
  • }     
  • }   
  • 再看一下FirstActor。

    聲明一個Texture用于繪制。在構造方法中獲取到高度和寬度,以便于后期的hit時間判斷。

    1. package com.cnblogs.htynkn.domain;     
    2. import com.badlogic.gdx.Gdx;     
    3. import com.badlogic.gdx.graphics.Texture;     
    4. import com.badlogic.gdx.graphics.g2d.SpriteBatch;     
    5. import com.badlogic.gdx.scenes.scene2d.Actor;     
    6. public class FirstActor extends Actor {     
    7. Texture texture;     
    8. @Override     
    9. public void draw(SpriteBatch batch, float parentAlpha) {     
    10. batch.draw(texture, this.x, this.y);     
    11. }     
    12. @Override     
    13. public Actor hit(float x, float y) {     
    14. if (x > 0 && y > 0 && this.height > y && this.width > x) {     
    15. return this;     
    16. else {     
    17. return null;     
    18. }     
    19. }     
    20. @Override     
    21. public boolean touchDown(float x, float y, int pointer) {     
    22. // TODO Auto-generated method stub     
    23. return false;     
    24. }     
    25. @Override     
    26. public void touchDragged(float x, float y, int pointer) {     
    27. // TODO Auto-generated method stub     
    28. }     
    29. @Override     
    30. public void touchUp(float x, float y, int pointer) {     
    31. // TODO Auto-generated method stub     
    32. }     
    33. public FirstActor(String name) {     
    34. super(name);     
    35. texture = new Texture(Gdx.files.internal("actor1.gif"));     
    36. this.height = texture.getHeight();     
    37. this.width = texture.getWidth();     
    38. }     
    39. }   

    NarrowButton中代碼繪制部分和上面的以下,主要是有個點擊后控制人物行動的問題。

    修改touchDown事件:

    通過Group獲取到FirstActor,控制x值。

    1. public boolean touchDown(float x, float y, int pointer) {     
    2. Actor actor = this.parent.findActor("renwu");     
    3. actor.x += 10;     
    4. return false;     
    5. }   

    效果:

    Android游戲引擎libgdx使用教程4:舞臺和演員的游戲?qū)嵗? /></p>
<p>到此為止一個最簡單的人物控制我們已經(jīng)實現(xiàn)了。但是這個有實例還有很多可以改進的地方,比如方向按鈕沒有點擊效果,人物沒有移動效果。</p>
<p>我們可以使用Animation來實現(xiàn)。添加一張圖片:</p>
<p style=Android游戲引擎libgdx使用教程4:舞臺和演員的游戲?qū)嵗? /></p>
<p>具體的原理我們看一下Animation類:</p>
<pre><ol class=

  • public class Animation {     
  • final TextureRegion[] keyFrames;     
  • public float frameDuration;     
  • /** Constructor, storing the frame duration and key frames.    
  • *    
  • * @param frameDuration the time between frames in seconds.    
  • * @param keyFrames the {@link TextureRegion}s representing the frames. */     
  • public Animation (float frameDuration, List keyFrames) {     
  • this.frameDuration = frameDuration;     
  • this.keyFrames = new TextureRegion[keyFrames.size()];     
  • for(int i = 0, n = keyFrames.size(); i < n; i++) {     
  • this.keyFrames[i] = (TextureRegion)keyFrames.get(i);     
  • }     
  • }     
  • /** Constructor, storing the frame duration and key frames.    
  • *    
  • * @param frameDuration the time between frames in seconds.    
  • * @param keyFrames the {@link TextureRegion}s representing the frames. */     
  • public Animation (float frameDuration, TextureRegion... keyFrames) {     
  • this.frameDuration = frameDuration;     
  • this.keyFrames = keyFrames;     
  • }     
  • /** Returns a {@link TextureRegion} based on the so called state time. This is the amount of seconds an object has spent in the    
  • * state this Animation instance represents, e.g. running, jumping and so on. The mode specifies whether the animation is    
  • * looping or not.    
  • * @param stateTime the time spent in the state represented by this animation.    
  • * @param looping whether the animation is looping or not.    
  • * @return the TextureRegion representing the frame of animation for the given state time. */     
  • public TextureRegion getKeyFrame (float stateTime, boolean looping) {     
  • int frameNumber = (int)(stateTime / frameDuration);     
  • if (!looping) {     
  • frameNumber = Math.min(keyFrames.length - 1, frameNumber);     
  • else {     
  • frameNumber = frameNumber % keyFrames.length;     
  • }     
  • return keyFrames[frameNumber];     
  • }     
  • }   
  • 可以看出所謂的動畫其實是一張一張的圖片不斷切換(其實所有的動畫都是這個樣子的)。

    我們構造一個圖片列表然后根據(jù)事件變動不停取出,重新繪制就形成動畫了。

    注意一下傳入的時間和圖片列表大小的問題,修改FirstActor代碼如下:

    1. package com.cnblogs.htynkn.domain;     
    2. import com.badlogic.gdx.Gdx;     
    3. import com.badlogic.gdx.graphics.Texture;     
    4. import com.badlogic.gdx.graphics.g2d.Animation;     
    5. import com.badlogic.gdx.graphics.g2d.SpriteBatch;     
    6. import com.badlogic.gdx.graphics.g2d.TextureRegion;     
    7. import com.badlogic.gdx.scenes.scene2d.Actor;     
    8. public class FirstActor extends Actor {     
    9. Texture texture1;     
    10. Texture texture2;     
    11. Animation animation;     
    12. TextureRegion[] walksFrame;     
    13. float stateTime;     
    14. @Override   
    15. public void draw(SpriteBatch batch, float parentAlpha) {     
    16. stateTime += Gdx.graphics.getDeltaTime();     
    17. TextureRegion currentFrame = animation.getKeyFrame(stateTime, true);     
    18. batch.draw(currentFrame, this.x, this.y);     
    19. }     
    20. @Override   
    21. public Actor hit(float x, float y) {     
    22. Gdx.app.log("INFO", x + " " + this.width);     
    23. if (x > 0 && y > 0 && this.height > y && this.width > x) {     
    24. return this;     
    25. else {     
    26. return null;     
    27. }     
    28. }     
    29. @Override   
    30. public boolean touchDown(float x, float y, int pointer) {     
    31. // TODO Auto-generated method stub     
    32. return false;     
    33. }     
    34. @Override   
    35. public void touchDragged(float x, float y, int pointer) {     
    36. // TODO Auto-generated method stub     
    37. }     
    38. @Override   
    39. public void touchUp(float x, float y, int pointer) {     
    40. // TODO Auto-generated method stub     
    41. }     
    42. public FirstActor(String name) {     
    43. super(name);     
    44. texture1 = new Texture(Gdx.files.internal("actor1.gif"));     
    45. texture2 = new Texture(Gdx.files.internal("actor2.gif"));     
    46. this.height = texture1.getHeight();     
    47. this.width = texture1.getWidth();     
    48. TextureRegion region1;     
    49. TextureRegion region2;     
    50. region1 = new TextureRegion(texture1);     
    51. region2 = new TextureRegion(texture2);     
    52. walksFrame = new TextureRegion[30];     
    53. for (int i = 0; i < 30; i++) {     
    54. if (i % 2 == 0) {     
    55. walksFrame[i] = region1;     
    56. else {     
    57. walksFrame[i] = region2;     
    58. }     
    59. }     
    60. animation = new Animation(0.25f, walksFrame);     
    61. }     
    62. }   

    效果:

    Android游戲引擎libgdx使用教程4:舞臺和演員的游戲?qū)嵗? /></p>
<p>這里注意一下,為什么我們要Texture轉(zhuǎn)為TextureRegion。這是因為在實際開發(fā)中的圖片是集成在一起的,比如所有角色要用的圖片都是放在一張圖里,然后分割截取的,對應的輔助方法TextureRegion.split。</p>
<p>另外我們可以發(fā)現(xiàn)NarrowButton和FirstActor中有大量代碼重復了,可能有朋友覺得應該提取一下,其實libgdx已經(jīng)幫我們做了,可以參考</p>
<p style=Android游戲引擎libgdx使用教程4:舞臺和演員的游戲?qū)嵗? /></p>
<p>圖片分享:</p>
<p>這里有一些常用的UI控件,估計下一篇可以講到。</p></div> <div   id=責任編輯:閆佳明 來源: jizhuomi

    相關推薦

    2013-12-06 10:31:14

    Android游戲引擎libgdx教程

    2013-12-04 17:27:10

    Android游戲引擎libgdx教程

    2013-12-04 17:14:57

    Android游戲引擎libgdx教程

    2013-12-04 13:30:45

    Android游戲引擎libgdx教程

    2013-12-06 10:12:49

    Android游戲引擎libgdx教程

    2013-12-06 09:59:53

    Android游戲引擎libgdx教程

    2013-12-06 10:22:42

    Android游戲引擎libgdx教程

    2013-12-06 10:35:28

    Android游戲引擎libgdx教程

    2013-12-04 16:07:27

    Android游戲引擎libgdx教程

    2013-12-04 16:21:02

    Android游戲引擎libgdx教程

    2011-07-18 11:07:12

    iPhone 游戲 引擎

    2011-07-18 10:53:09

    2011-07-18 12:29:10

    2011-07-18 11:39:58

    iPhone 游戲 引擎

    2011-07-18 11:23:29

    iPhone 游戲 動畫

    2011-12-12 13:58:11

    TinyCoiOSAndroid

    2013-04-19 01:42:02

    2014-07-15 10:34:14

    Android游戲引擎

    2013-05-20 15:42:22

    2011-05-31 15:45:38

    Android 游戲引擎 開源
    點贊
    收藏

    51CTO技術棧公眾號