您的位置:首页 > 移动开发 > Android开发

android游戏开发框架libgdx的使用(十)—双舞台

2013-03-14 16:34 906 查看
转自:http://www.apkbus.com/forum.php?mod=viewthread&tid=19768&extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline%26orderby%3Ddateline

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

}

}

复制代码
效果:



2012-1-17 21:40 上传
下载附件(41.94 KB)

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



2012-1-17 21:40 上传
下载附件(29.58 KB)

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

Gdx.input.setInputProcessor(stage1);

Gdx.input.setInputProcessor(stage2);

复制代码
应该这样做:

InputMultiplexer inputMultiplexer=new InputMultiplexer();

inputMultiplexer.addProcessor(stage1);

inputMultiplexer.addProcessor(stage2);

复制代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: