Android開發_libgdx遊戲引擎教程外篇 優美的自訂進度條 (八)

來源:互聯網
上載者:User
這次的外一篇需要大家先掌握一些簡單的libgdx的知識看起來才更加容易些: android遊戲開發架構libgdx的使用(四)--舞台和演員http://www.apkbus.com/android-19750-1-1.html libgdx 學習筆記二繪製映像http://www.apkbus.com/android-44573-1-1.html我們先建立一個ProgressBar的類,這個類繼承自Libgdx中的演員Actor類,並實現了我在第二講中提到過的Disposable介面,為的是能及時釋放記憶體。

  1. public class ProgressBar extends Actor implements Disposable{
  2.     Texture platform;
  3.     Texture bar;
  4.     int height;
  5.     int width;
  6.     float progress;
  7.     //做了一個簡單的適配,powerx和powery分別當前裝置解析度的權重,以現在主流的800*480為基準
  8.     float powerx;
  9.     float powery;
  10.     @Override
  11.     public void draw(SpriteBatch batch, float arg1) {
  12.        // TODO Auto-generated method stub
  13.        batch.draw(platform, (Gdx.graphics.getWidth()-bar.getWidth()*powerx)/2, 0,platform.getWidth()*powerx,platform.getHeight()*powery);
  14.     batch.draw(bar,(Gdx.graphics.getWidth()-bar.getWidth()*powerx)/2,0,bar.getWidth()*progress/100f*powerx,bar.getHeight()*powery);
  15.     }
  16.     @Override
  17.     public Actor hit(float arg0, float arg1) {
  18.        // TODO Auto-generated method stub
  19.        return null;
  20.     }
  21.     public ProgressBar(int x,int y) {
  22.        super();
  23.        //設定Actor的位置,這裡並沒有什麼用,純粹為了和大家介紹一下
  24.        this.x=x;
  25.        this.y=y;
  26.        platform=new Texture(Gdx.files.internal("black.png"));
  27.        bar=new Texture(Gdx.files.internal("green.png"));
  28.        height=Gdx.graphics.getHeight();
  29.        width=Gdx.graphics.getWidth();
  30.        //做了一個簡單的適配,powerx和powery分別當前裝置解析度的權重,以現在主流的800*480為基準
  31.        powerx=Gdx.graphics.getWidth()/800f;
  32.        powery=Gdx.graphics.getHeight()/480f;
  33.     }
  34.     public void setProgress(float progress){
  35.        this.progress=progress;
  36.     }
  37.     public void dispose() {
  38.        // TODO Auto-generated method stub
  39.        platform.dispose();
  40.        bar.dispose();
  41.     }
  42.    
  43. }

複製代碼這裡對上面的變數和語句都做一個簡單的說明:Texture platform;Texture bar;前者是進度條的底座的圖片,而bar則代表進度條的圖片。       至於Actor中的draw()方法,這是一個系統自動調用的方法,描述這個演員Actor執行個體怎麼繪製,draw()方法並不需要我們人工去調用,而是在把Actor加入舞台Stage中後,在ApplicationListener的render()函數中不停地調用stage.draw(),系統會自動調用已經加入stage中的actor的draw()方法,也就將actor一起繪製出來了。具體請看:android遊戲開發架構libgdx的使用(四)--舞台和演員http://www.apkbus.com/android-19750-1-1.html

剩下的語句也很簡單,draw()中用SpriteBatch畫出了兩張圖,其中bar的繪製長度根據進度progress變化,也就產生了進度條的效果。

這次我們重寫一個ApplicationListener,再用AndroidApplication啟動,源碼還是在第三講的基礎上進行修改和添加。

  1. public class Progress implements ApplicationListener {
  2.     ProgressBar bar;
  3.     Stage stage;
  4.     @Override
  5.     public void create() {
  6.        // TODO Auto-generated method stub
  7.        bar=new ProgressBar(0,0);
  8.        //建立一個舞台
  9.        stage=new Stage(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(), true);
  10.        stage.addActor(bar);
  11.     }
  12.     @Override
  13.     public void dispose() {
  14.        // TODO Auto-generated method stub
  15.        bar.dispose();
  16.     }
  17.     @Override
  18.     public void pause() {
  19.        // TODO Auto-generated method stub
  20.     }
  21.     @Override
  22.     public void render() {
  23.        // TODO Auto-generated method stub
  24.        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  25.        Gdx.gl.glClearColor(1f,1f,1f,0f);
  26.        stage.act(Gdx.graphics.getDeltaTime());
  27.        stage.draw();
  28.        if(bar.progress<100)
  29.            bar.progress+=0.5;
  30.        //重新置零
  31.        if(bar.progress==100)
  32.            bar.progress=0;
  33.     }
  34.     @Override
  35.     public void resize(int arg0, int arg1) {
  36.        // TODO Auto-generated method stub
  37.     }
  38.     @Override
  39.     public void resume() {
  40.        // TODO Auto-generated method stub
  41.     }
  42. }

複製代碼這裡相比之前的demo有一處變化:Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);Gdx.gl.glClearColor(1f,1f,1f,0f);大家可以看到我將清屏後的顏色置為(1,1,1,0),即為白色,大家可以在下面看到效果。再用AndroidApplication啟動:

  1. public class MyGame implements ApplicationListener {
  2.     SpriteBatch batch;
  3.     BitmapFont bf;
  4.     ParticleEffect particle;
  5.     ParticleEffect tem;
  6.     ParticleEffectPool particlepool;
  7.     ArrayList<ParticleEffect> particlelist;
  8.         public void create () {
  9.                 // STUB
  10.         batch=new SpriteBatch();
  11.         bf=new BitmapFont();
  12.         //初始化粒子變數
  13.         particle = new ParticleEffect();
  14.         particle.load(Gdx.files.internal("particle.p"), Gdx.files.internal(""));
  15.         particlepool=new ParticleEffectPool(particle, 5, 10);
  16.         particlelist=new ArrayList<ParticleEffect>();
  17.         }
  18.         public void render () {
  19.                 // STUB
  20.         Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  21.         Gdx.gl.glClearColor(0f,0f,0f,0f);
  22.         batch.begin();
  23.         bf.draw(batch, "Testin  Mkey libgdx(3)",Gdx.graphics.getWidth()*0.4f, Gdx.graphics.getHeight()/2);
  24.         batch.end();
  25.         if(true){
  26.              if(Gdx.input.isTouched()){
  27.                 //當此觸摸點與上一觸摸點距離大於一定值的時候觸發新的粒子系統,由此減小系統負擔
  28.                 tem=particlepool.obtain();
  29.                 tem.setPosition(Gdx.input.getX(),Gdx.graphics.getHeight()-Gdx.input.getY());
  30.                 particlelist.add(tem);
  31.              }
  32.              batch.begin();
  33.                 for(int i=0;i<particlelist.size();i++){
  34.              particlelist.get(i).draw(batch, Gdx.graphics.getDeltaTime());
  35.                 }
  36.                 batch.end();
  37.       
  38.                 //清除已經播放完成的粒子系統
  39.                 ParticleEffect temparticle;
  40.                 for(int i=0;i<particlelist.size();i++){
  41.                   temparticle=particlelist.get(i);
  42.                  if(temparticle.isComplete()){
  43.                      particlelist.remove(i);
  44.                  }
  45.                 }
  46.              }
  47.         }
  48.         public void resize (int width, int height) {
  49.                 // STUB
  50.         }
  51.         public void pause () {
  52.                 // STUB
  53.         }
  54.         public void resume () {
  55.                 // STUB
  56.         }
  57.         public void dispose () {
  58.                 // STUB
  59.         batch.dispose();
  60.         bf.dispose();
  61.         //千萬別忘了釋放記憶體
  62.         particle.dispose();
  63.         if(tem!=null)
  64.             tem.dispose();
  65.         particlepool.clear();
  66.         }
  67. }

複製代碼

運行即可,大功告成!附
福利圖:哈哈,下面是我的用libgdx寫的一個比較大的遊戲,還沒有完全寫好,可以先泄露一點諜照哦,如果大家喜歡Libgdx的話,請多多支援我的Testin杯libgdx系列教程,如果支援的人多而且大家都想要源碼的話,我考慮把源碼放出來供大家學習學習。

遊戲其實就是寵物養成遊戲,可以普通對戰及藍芽對戰,寵物進化等等。不知道大家能不能從源碼的名字中看出什麼功能來呢?哈哈這就是我用本篇的進度條寫的一個非同步載入的等待頁面,還沒有完善,先意思意思。諜照!!!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.