Android游戏引擎libgdx使用教程10:双舞台

移动开发 Android
游戏屏幕最常见的就是一个变化较少的背景加上一系列和用户交互的角色和部件。为了方便管理你还可以为背景建个Group方便管理。

游戏屏幕最常见的就是一个变化较少的背景加上一系列和用户交互的角色和部件。为了方便管理你还可以为背景建个Group方便管理。

但是有时候写的时候没有想到这个问题,或者是背景不是单纯的一个图片什么的,背景和角色还有一些混合逻辑分布在两个Stage里。我重写太麻烦,想想反正都是SpritBatch绘制出来的,用双舞台大不了多个摄像头。马上试试还真行。

先看看Stage的draw方法:

  1. /** Renders the stage */    
  2. public void draw () {    
  3. camera.update();    
  4. if (!root.visible) return;    
  5. batch.setProjectionMatrix(camera.combined);    
  6. batch.begin();    
  7. root.draw(batch, 1);    
  8. batch.end();    
  9. }   

batch的话两个舞台可以共用。用Stage(width, height, stretch, batch)实例化第二个舞台。

代码如下:

  1. package com.cnblogs.htynkn.game;   
  2. import com.badlogic.gdx.ApplicationListener;    
  3. import com.badlogic.gdx.Gdx;    
  4. import com.badlogic.gdx.InputProcessor;    
  5. import com.badlogic.gdx.graphics.GL10;    
  6. import com.badlogic.gdx.graphics.Texture;    
  7. import com.badlogic.gdx.graphics.g2d.TextureRegion;    
  8. import com.badlogic.gdx.scenes.scene2d.Stage;    
  9. import com.badlogic.gdx.scenes.scene2d.ui.Image;   
  10. public class JavaGame implements ApplicationListener {   
  11. Stage stage1;    
  12. Stage stage2;    
  13. float width;    
  14. float height;   
  15. @Override    
  16. public void create() {    
  17. width = Gdx.graphics.getWidth();    
  18. height = Gdx.graphics.getHeight();    
  19. stage1 = new Stage(width, height, true);    
  20. stage2 = new Stage(width, height, true,stage1.getSpriteBatch());    
  21. Image image = new Image(new TextureRegion(new Texture(Gdx.files    
  22. .internal("img/sky.jpg")), 5050480320));    
  23. stage1.addActor(image);    
  24. Image image2 = new Image(new TextureRegion(new Texture(Gdx.files    
  25. .internal("img/baihu.png")), 217157));    
  26. image2.x=(width-image2.width)/2;    
  27. image2.y=(height-image2.height)/2;    
  28. stage2.addActor(image2);    
  29. }   
  30. @Override    
  31. public void dispose() {    
  32. // TODO Auto-generated method stub   
  33. }   
  34. @Override    
  35. public void pause() {    
  36. // TODO Auto-generated method stub   
  37. }   
  38. @Override    
  39. public void render() {    
  40. Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);    
  41. stage1.act(Gdx.graphics.getDeltaTime());    
  42. stage2.act(Gdx.graphics.getDeltaTime());    
  43. stage1.draw();    
  44. stage2.draw();    
  45. }   
  46. @Override    
  47. public void resize(int width, int height) {    
  48. // TODO Auto-generated method stub   
  49. }   
  50. @Override    
  51. public void resume() {    
  52. // TODO Auto-generated method stub   
  53. }    
  54. }   

效果:

Android游戏引擎libgdx使用教程10:双舞台

如果你对于效率追求比较极致,可以考虑对于SpritBatch的缓冲数进行修改。

还有一个需要注意,背景舞台应该先绘制,其他部件后绘制,不然效果就是下图:

Android游戏引擎libgdx使用教程10:双舞台

关于舞台的输入控制,不能简单的使用:

  1. Gdx.input.setInputProcessor(stage1);    
  2. Gdx.input.setInputProcessor(stage2);   

应该这样做:

  1. InputMultiplexer inputMultiplexer=new InputMultiplexer();    
  2. inputMultiplexer.addProcessor(stage1);    
  3. inputMultiplexer.addProcessor(stage2);   

 

责任编辑:闫佳明 来源: jizhuomi
相关推荐

2013-12-04 16:28:29

Android游戏引擎libgdx教程

2013-12-04 17:14:57

Android游戏引擎libgdx教程

2013-12-04 13:30:45

Android游戏引擎libgdx教程

2013-12-06 09:59:53

Android游戏引擎libgdx教程

2013-12-04 17:27:10

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-04 16:21:02

Android游戏引擎libgdx教程

2013-12-06 10:35:28

Android游戏引擎libgdx教程

2011-07-18 12:29:10

2011-07-18 11:23:29

iPhone 游戏 动画

2011-07-18 11:39:58

iPhone 游戏 引擎

2011-07-18 10:53:09

2011-07-18 11:07:12

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开发开源游戏引擎游戏开发
点赞
收藏

51CTO技术栈公众号