libgdx基本使用——舞台与演员

除了FirstGame以外的类的代码,都是和之前的博客一样的。。。


package com.doodle.rdemo1;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class SecondGame implements ApplicationListener {

	private Stage stage;//舞台
	
	private Label label;//标签
	private Button button;//按钮
	
	@Override
	public void create() {
		stage = new Stage(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),true);//舞台的宽、高、??
		
		//第一个参数是要显示的内容,第二个参数是LabelStyle,其中LabelStyle的构造函数接受一个BitmapFont和一个Color值
		label = new Label("FPS: " + Gdx.graphics.getFramesPerSecond(), new Label.LabelStyle(new BitmapFont(Gdx.files.internal("jd.fnt"), Gdx.files.internal("jd.png"), true),Color.BLUE) );
		label.setX(0);//这是label在舞台中的位置
		label.setY(Gdx.graphics.getHeight() - label.getHeight() - 5);
		stage.addActor(label);//往舞台中添加演员
		
		//Button的构造函数的构造如下:Button(TextureRegionDrawable(TextureRegion(Texture())))
		button = new Button(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("button.png")))));
		stage.addActor(button);
		
		Gdx.input.setInputProcessor(stage);//输出舞台(以上仅仅是设置舞台效果)
	}

	@Override
	public void dispose() {
		stage.dispose();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		label.setText("FPS: " + Gdx.graphics.getFramesPerSecond() );//如果不加的话,label的值不会发生改变
		label.setAlignment(Align.center);
		
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

}


本demo源码的下载链接:http://download.csdn.net/detail/caihongshijie6/6978077

你可能感兴趣的:(libgdx基本使用——舞台与演员)