Android游戏引擎libgdx使用教程10:双舞台
游戏屏幕最常见的就是一个变化较少的背景加上一系列和用户交互的角色和部件。为了方便管理你还可以为背景建个Group方便管理。
但是有时候写的时候没有想到这个问题,或者是背景不是单纯的一个图片什么的,背景和角色还有一些混合逻辑分布在两个Stage里。我重写太麻烦,想想反正都是SpritBatch绘制出来的,用双舞台大不了多个摄像头。马上试试还真行。
先看看Stage的draw方法:
/** Renders the stage */
public void draw () {
camera.update();
if (!root.visible) return;
batch.setProjectionMatrix(camera.combined);
batch.begin();
root.draw(batch, 1);
batch.end();
} batch的话两个舞台可以共用。用Stage(width, height, stretch, batch)实例化第二个舞台。
代码如下:
package com.cnblogs.htynkn.game;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
public class JavaGame implements ApplicationListener {
Stage stage1;
Stage stage2;
float width;
float height;
@Override
public void create() {
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
stage1 = new Stage(width, height, true);
stage2 = new Stage(width, height, true,stage1.getSpriteBatch());
Image image = new Image(new TextureRegion(new Texture(Gdx.files
.internal("img/sky.jpg")), 50, 50, 480, 320));
stage1.addActor(image);
Image image2 = new Image(new TextureRegion(new Texture(Gdx.files
.internal("img/baihu.png")), 217, 157));
image2.x=(width-image2.width)/2;
image2.y=(height-image2.height)/2;
stage2.addActor(image2);
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage1.act(Gdx.graphics.getDeltaTime());
stage2.act(Gdx.graphics.getDeltaTime());
stage1.draw();
stage2.draw();
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
} 效果:

如果你对于效率追求比较极致,可以考虑对于SpritBatch的缓冲数进行修改。
还有一个需要注意,背景舞台应该先绘制,其他部件后绘制,不然效果就是下图:

关于舞台的输入控制,不能简单的使用:
Gdx.input.setInputProcessor(stage1); Gdx.input.setInputProcessor(stage2);
相关推荐
Hhanwen 2020-07-05
yanqianglifei 2020-02-21
89463661 2020-02-20
xiaodaiwang 2019-12-19
fengjing81 2019-12-02
dxyadc 2019-10-27
adayan0 2019-10-22
lfjjia 2010-11-09
herryyy 2012-12-18
PANH 2019-07-01
starksummer 2019-03-31
上海彭彭 2019-06-30
CloasGao 2019-06-29
82530995 2019-06-28
雪飞海 2019-06-26
成长之路 2018-10-18
zilianxiaozhu 2018-09-12
adayan0 2018-07-20
jinkun00 2015-05-09