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

本文详细解析了游戏开发中使用的代码实现,包括舞台、标签、按钮等组件的创建与使用,以及如何通过Gdx框架进行游戏开发的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

除了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.youkuaiyun.com/detail/caihongshijie6/6978077

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值