(libgdx小结)window(游戏对话框的使用)

package com.example.groupactiontest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
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.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class MyGame implements ApplicationListener {

	
	Stage stage;
	
	Window window;
	ImageButton btn_show;
	ImageButton btn_ok;
	ImageButton btn_cancel;
	
	
	@Override
	public void create() {
		//创建按钮
		Texture texture = new Texture(Gdx.files.internal("control.png"));
		TextureRegion[][] split = TextureRegion.split(texture, 64, 64);
		
	    TextureRegionDrawable showDrawableUp = new TextureRegionDrawable(split[0][0]);
	    TextureRegionDrawable showDrawableDown = new TextureRegionDrawable(split[0][1]);
		 
	    TextureRegionDrawable okDrawableUp = new TextureRegionDrawable(split[0][2]);
	    TextureRegionDrawable okDrawableDown = new TextureRegionDrawable(split[0][3]);
	    
	    TextureRegionDrawable cancelDrawableUp = new TextureRegionDrawable(split[1][0]);
	    TextureRegionDrawable cancelDrawableDown = new TextureRegionDrawable(split[1][1]);
	    
	    btn_show = new ImageButton(showDrawableUp, showDrawableDown);
	    btn_ok = new ImageButton(okDrawableUp, okDrawableDown);
	    btn_cancel = new ImageButton(cancelDrawableUp, cancelDrawableDown);
	    
	  //创建window(在这里也就是游戏对话框...)
	    BitmapFont font = new BitmapFont(Gdx.files.internal("Potato.fnt"), Gdx.files.internal("Potato.png"), false);
	    Texture backTexture = new Texture(Gdx.files.internal("dialog.png"));
	    TextureRegionDrawable backDrawable = new TextureRegionDrawable(new TextureRegion(backTexture));
	    WindowStyle style = new WindowStyle(font, font.getColor(), backDrawable);
	    window = new Window("Hello libgdx game", style);
	    
	    window.setWidth(Gdx.graphics.getWidth()/2);
	    window.setHeight(Gdx.graphics.getHeight()/3);
	    window.setPosition(400, 200);
	    window.setModal(true);
	    
	    //给按钮添加点击事件
	    btn_show.addListener(new InputListener(){
	    	@Override
	    	public boolean touchDown(InputEvent event, float x, float y,
	    			int pointer, int button) {

	    		stage.addActor(window);
	    		
	    		return true;
	    	}
	    });
	    
	    btn_ok.addListener(new InputListener(){
	    	@Override
	    	public boolean touchDown(InputEvent event, float x, float y,
	    			int pointer, int button) {
	    		
	    		Gdx.app.exit();
	    		
	    		return true;
	    	}
	    });
	    
        btn_cancel.addListener(new InputListener(){
        	@Override
        	public boolean touchDown(InputEvent event, float x, float y,
        			int pointer, int button) {
        		
        		window.remove();
        		
        		return true;
        	}
        });
        
        //给按钮设置位置
        btn_ok.setPosition(50, 50);
        btn_cancel.setPosition(100, 50);
        
        //给window添加演员
        window.addActor(btn_ok);
        window.addActor(btn_cancel);
        
        
        stage = new Stage();
        stage.addActor(btn_show);
        
        Gdx.input.setInputProcessor(stage);
        
	}

	@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);
		
		stage.act();
		stage.draw();
		
	}

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

	}

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

	}

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

帅气的东哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值