Libgdx按钮

本文介绍了一个简单的Android游戏应用开发过程,包括游戏界面布局、玩家角色控制及按钮响应等核心功能实现。通过使用LibGDX框架,文章展示了如何创建游戏活动类、初始化游戏组件,并实现了基本的触摸事件监听。

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

package com.xuefei.mygame;

import android.os.Bundle;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.android.AndroidApplication;

public class MainActivity extends AndroidApplication {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialize((ApplicationListener) new GameActivity(), false); 
    }
}


package com.xuefei.mygame;

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.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;

public class GameActivity implements ApplicationListener {
	Stage stage;
	float width;
	float height;
	TextureAtlas atlas;
	TextureAtlas buttons;
	Image player;
	ImageButton up;
	ImageButton down;
	ImageButton left;
	ImageButton right;
	@Override
	public void create() {
		// TODO Auto-generated method stub
		//获取设备宽高
		width =Gdx.graphics.getWidth();
		height = Gdx.graphics.getHeight();
		stage = new Stage(width,height, true);
		// 创建一个Label样式,使用默认黑色字体
		LabelStyle labelStyle = new LabelStyle(new BitmapFont(), Color.BLACK); 
		// 创建标签,显示的文字是FPS:
		Label label = new Label("FPS:", labelStyle); 
		// 设置标签名称为fpsLabel
		label.setName("fpsLabel"); 
		// 设置Y为0,即显示在最下面
		label.setY(0);
		// 设置X值,显示在屏幕最左侧
		label.setX(label.getTextBounds().width); 
		// 将标签添加到舞台
		stage.addActor(label); 
		
		atlas =new TextureAtlas("role0.pack");
		buttons = new TextureAtlas("buttons.pack");
		player = new Image(atlas.findRegion("image 5"));
		player.setName("player");
		player.setX(0);
		player.setY(height/2-player.getHeight()/2);
		stage.addActor(player);
		Texture upTexture = new Texture(Gdx.files.internal("up.png"));
		SpriteDrawable upDraw = new SpriteDrawable(new Sprite(upTexture));
		up = new ImageButton(upDraw);
		up.setPosition(64f, 128f);
		up.addListener(new ClickListener(){
			public void clicked(InputEvent event,float x,float y){
				if(event.getTarget()==up){
					System.out.println("click:"+x+","+y);
				}
			}
		});
		stage.addActor(up);
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		stage.dispose();
	}

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

	@Override
	public void render() {
		// TODO Auto-generated method stub
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		stage.act();
		stage.draw();

		// 获取名为fpsLabel的标签
		Label label = (Label) stage.getRoot().findActor("fpsLabel"); 
		label.setText("FPS:" + Gdx.graphics.getFramesPerSecond());
		// 更新X值以保证显示位置正确性
		label.setX(label.getTextBounds().width); 
	}

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

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

地狱为王

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

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

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

打赏作者

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

抵扣说明:

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

余额充值