Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1060901
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-03-05 13:29: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);
阅读(1618) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~