成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

大家先要有舞臺(tái)和演員的概念,下面通過一個(gè)游戲截圖來(lái)說明:

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

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

    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中代碼繪制部分和上面的以下,主要是有個(gè)點(diǎn)擊后控制人物行動(dòng)的問題。

    修改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:舞臺(tái)和演員的游戲?qū)嵗? /></p>
<p>到此為止一個(gè)最簡(jiǎn)單的人物控制我們已經(jīng)實(shí)現(xiàn)了。但是這個(gè)有實(shí)例還有很多可以改進(jìn)的地方,比如方向按鈕沒有點(diǎn)擊效果,人物沒有移動(dòng)效果。</p>
<p>我們可以使用Animation來(lái)實(shí)現(xiàn)。添加一張圖片:</p>
<p style=Android游戲引擎libgdx使用教程4:舞臺(tái)和演員的游戲?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];     
  • }     
  • }   
  • 可以看出所謂的動(dòng)畫其實(shí)是一張一張的圖片不斷切換(其實(shí)所有的動(dòng)畫都是這個(gè)樣子的)。

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

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

    相關(guān)推薦

    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 10:53:09

    2011-07-18 11:07:12

    iPhone 游戲 引擎

    2011-07-18 12:29:10

    2011-07-18 11:23:29

    iPhone 游戲 動(dòng)畫

    2011-07-18 11:39:58

    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

    2014-07-17 11:10:19

    Android開源游戲引擎
    點(diǎn)贊
    收藏

    51CTO技術(shù)棧公眾號(hào)

    主站蜘蛛池模板: 成人在线欧美 | 国产一级毛片精品完整视频版 | 成人国产一区二区三区精品麻豆 | 中文字幕一区二区三区不卡 | 国产精品久久久久久久久久久久久 | 国产 欧美 日韩 一区 | 亚洲一区视频在线 | 国产精久久久久久 | 欧美综合一区 | 精品一区二区三区四区五区 | 国产精品自产拍在线观看蜜 | 久久国产欧美日韩精品 | 成年男女免费视频网站 | 欧美激情国产精品 | av国产在线观看 | 久久久久国产精品免费免费搜索 | 国产黄色网址在线观看 | 亚洲欧美日韩中文在线 | 天堂色| 久久久久综合 | 99国产精品99久久久久久粉嫩 | 一区二区成人 | 99精品久久 | 欧美人妇做爰xxxⅹ性高电影 | 亚洲国产一区二区三区 | 欧美99| 亚洲精品国产一区 | 在线观看av网站永久 | 国产日韩欧美在线 | 一区天堂 | 91日韩在线 | 日韩国产精品一区二区三区 | 欧美乱码精品一区二区三区 | 久久亚洲二区 | 91欧美 | 91看片在线 | 午夜精品影院 | 波多野结衣电影一区 | 懂色中文一区二区三区在线视频 | 国产精品久久久久久久久久久久冷 | 永久精品|