日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Android游戲引擎libgdx使用教程4:舞臺(tái)和演員的游戲?qū)嵗?/div>

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

創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元全椒做網(wǎng)站,已為上家服務(wù),為全椒各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792

請(qǐng)仔細(xì)觀察圖片中的元素,有些東西是不能動(dòng),有些可以動(dòng),有些有特效,有些沒有,有各種圖片,但是其實(shí)它們都可以統(tǒng)一稱為演員(Actor)。圖中用框標(biāo)出的就是演員。而整個(gè)游戲界面就是我們的舞臺(tái)。

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

Stage類

我們來看一下Stage類:

       protected final Group root;

       protected final SpriteBatch batch;

       protected Camera camera;

它擁有一個(gè)Group,一個(gè)SpriteBatch,還有一個(gè)相機(jī)。

SpriteBatch我們?cè)谇皫灼f過,這里就不再重復(fù)了。

Group是一個(gè)類,用于容納和控制演員。但是這里要注意Group本身其實(shí)也是繼承自Actor。

相機(jī)我們這里跳過,以后再說,可以暫時(shí)理解成一個(gè)控制觀察視角和指標(biāo)轉(zhuǎn)化的工具。

當(dāng)我們擁有一個(gè)演員后就可以調(diào)用addActor方法加入舞臺(tái)。

舞臺(tái)可以獲取輸入,但是需要設(shè)置。

       Gdx.input.setInputProcessor(stage);

舞臺(tái)和演員實(shí)例分享

下面來個(gè)列子,控制一個(gè)人物前進(jìn)。

控制人物的按鈕:

將所需的圖片放到assert中:

新建三個(gè)類:

FirstGame,實(shí)現(xiàn)接口ApplicationListener

FirstActor,繼承Actor

NarrowButton,繼承Actor

先看一下FirstGame

聲明一個(gè)Stage,然后實(shí)例化FirstActor和NarrowButton,將二者加入舞臺(tái)中,最后設(shè)置輸入響應(yīng)為Stage。

 
 
 
  1. package com.cnblogs.htynkn.listener;    
  2. import java.util.Date;    
  3. import java.util.Random;    
  4. import javax.microedition.khronos.opengles.GL;    
  5. import Android.util.Log;    
  6. import com.badlogic.gdx.ApplicationListener;    
  7. import com.badlogic.gdx.Gdx;    
  8. import com.badlogic.gdx.graphics.GL10;    
  9. import com.badlogic.gdx.graphics.g2d.BitmapFont;    
  10. import com.badlogic.gdx.scenes.scene2d.Stage;    
  11. import com.cnblogs.htynkn.domain.FirstActor;    
  12. import com.cnblogs.htynkn.domain.NarrowButton;    
  13. public class FirstGame implements ApplicationListener {    
  14. private Stage stage;    
  15. private FirstActor firstActor;    
  16. private NarrowButton button;    
  17. @Override    
  18. public void create() {    
  19. stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),    
  20. true);    
  21. firstActor = new FirstActor("renwu");    
  22. button = new NarrowButton("narrow");    
  23. stage.addActor(firstActor);    
  24. stage.addActor(button);    
  25. Gdx.input.setInputProcessor(stage);    
  26. }    
  27. @Override    
  28. public void dispose() {    
  29. stage.dispose();    
  30. }    
  31. @Override    
  32. public void pause() {    
  33. // TODO Auto-generated method stub    
  34. }    
  35. @Override    
  36. public void render() {    
  37. Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);    
  38. stage.act(Gdx.graphics.getDeltaTime());    
  39. stage.draw();    
  40. }    
  41. @Override    
  42. public void resize(int width, int height) {    
  43. // TODO Auto-generated method stub    
  44. }    
  45. @Override    
  46. public void resume() {    
  47. // TODO Auto-generated method stub    
  48. }    
  49. }  

再看一下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. }  

效果:

到此為止一個(gè)最簡(jiǎn)單的人物控制我們已經(jīng)實(shí)現(xiàn)了。但是這個(gè)有實(shí)例還有很多可以改進(jìn)的地方,比如方向按鈕沒有點(diǎn)擊效果,人物沒有移動(dòng)效果。

我們可以使用Animation來實(shí)現(xiàn)。添加一張圖片:

具體的原理我們看一下Animation類:

 
 
 
  1. public class Animation {    
  2. final TextureRegion[] keyFrames;    
  3. public float frameDuration;    
  4. /** Constructor, storing the frame duration and key frames.   
  5. *   
  6. * @param frameDuration the time between frames in seconds.   
  7. * @param keyFrames the {@link TextureRegion}s representing the frames. */    
  8. public Animation (float frameDuration, List keyFrames) {    
  9. this.frameDuration = frameDuration;    
  10. this.keyFrames = new TextureRegion[keyFrames.size()];    
  11. for(int i = 0, n = keyFrames.size(); i < n; i++) {    
  12. this.keyFrames[i] = (TextureRegion)keyFrames.get(i);    
  13. }    
  14. }    
  15. /** Constructor, storing the frame duration and key frames.   
  16. *   
  17. * @param frameDuration the time between frames in seconds.   
  18. * @param keyFrames the {@link TextureRegion}s representing the frames. */    
  19. public Animation (float frameDuration, TextureRegion... keyFrames) {    
  20. this.frameDuration = frameDuration;    
  21. this.keyFrames = keyFrames;    
  22. }    
  23. /** Returns a {@link TextureRegion} based on the so called state time. This is the amount of seconds an object has spent in the   
  24. * state this Animation instance represents, e.g. running, jumping and so on. The mode specifies whether the animation is   
  25. * looping or not.   
  26. * @param stateTime the time spent in the state represented by this animation.   
  27. * @param looping whether the animation is looping or not.   
  28. * @return the TextureRegion representing the frame of animation for the given state time. */    
  29. public TextureRegion getKeyFrame (float stateTime, boolean looping) {    
  30. int frameNumber = (int)(stateTime / frameDuration);    
  31. if (!looping) {    
  32. frameNumber = Math.min(keyFrames.length - 1, frameNumber);    
  33. } else {    
  34. frameNumber = frameNumber % keyFrames.length;    
  35. }    
  36. return keyFrames[frameNumber];    
  37. }    
  38. }  

可以看出所謂的動(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. }  

效果:

這里注意一下,為什么我們要Texture轉(zhuǎn)為TextureRegion。這是因?yàn)樵趯?shí)際開發(fā)中的圖片是集成在一起的,比如所有角色要用的圖片都是放在一張圖里,然后分割截取的,對(duì)應(yīng)的輔助方法TextureRegion.split。

另外我們可以發(fā)現(xiàn)NarrowButton和FirstActor中有大量代碼重復(fù)了,可能有朋友覺得應(yīng)該提取一下,其實(shí)libgdx已經(jīng)幫我們做了,可以參考

圖片分享:

這里有一些常用的UI控件,估計(jì)下一篇可以講到。


分享標(biāo)題:Android游戲引擎libgdx使用教程4:舞臺(tái)和演員的游戲?qū)嵗?
本文鏈接:http://www.5511xx.com/article/coosoge.html