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
    } 
}

效果:
image1

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

image2

关于舞台的输入控制,不能简单的使用:
Gdx.input.setInputProcessor(stage1); Gdx.input.setInputProcessor(stage2);
应该这样做:
InputMultiplexer inputMultiplexer=new InputMultiplexer(); inputMultiplexer.addProcessor(stage1); inputMultiplexer.addProcessor(stage2);


转载:http://www.huangyunkun.com/2012/01/09/libgdx_10/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值