libGDX游戏开发之弹窗(五)
libGDX系列 ,游戏开发有unity3D巴拉巴拉的,为啥还用java开发?因为我是Java程序员emm…国内用libgdx比较少,多数情况需要去官网和google找资料,相互学习的可以加我联系方式。
弹窗简单的代码如下:
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.ScreenUtils;
public class MyWindow extends ApplicationAdapter {
private Dialog dialog;
private Stage stage;
@Override
public void create() {
stage=new Stage();
Skin skin = new Skin(Gdx.files.internal("sk/uiskin.json"));
dialog = new Dialog("Info", skin);
dialog.text("hello ?");
dialog.button("Yes", true);
dialog.button("No", false);
dialog.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
System.out.println(event);
dialog.hide();
}
});
Gdx.input.setInputProcessor(stage);
}
@Override
public void render() {
ScreenUtils.clear(225f, 225f, 225f, 0.4f);
if (Gdx.input.isKeyPressed(Input.Keys.UP) || Gdx.input.isKeyPressed(Input.Keys.W)) {
dialog.show(stage);
}
stage.draw();
stage.act();
}
}
效果如下 也可以在这里找到所有的皮肤字体等等: https://github.com/rafaskb/awesome-libgdx#readme 那么我们也可以自定一个窗口
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
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.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.utils.ScreenUtils;
public class MyWindow extends ApplicationAdapter {
class MessageWindow extends Window {
public MessageWindow(String title, Skin skin) {
super(title, skin.get(WindowStyle.class));
setSkin(skin);
}
}
private Stage stage;
private MessageWindow messageWindow;
@Override
public void create() {
stage = new Stage();
Skin skin = new Skin(Gdx.files.internal("sk/uiskin.json"));
messageWindow = new MessageWindow("custom window", skin);
messageWindow.add("custom window");
Button button = new Button(skin);
button.add(new Label("OK", skin));
button.setSize(40, 20);
messageWindow.add(button);
messageWindow.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
messageWindow.setWidth(100);
messageWindow.setHeight(100);
messageWindow.setSize(200, 200);
button.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
messageWindow.setVisible(false);
return super.touchDown(event, x, y, pointer, button);
}
});
messageWindow.setVisible(false);
stage.addActor(messageWindow);
Gdx.input.setInputProcessor(stage);
}
@Override
public void render() {
ScreenUtils.clear(225f, 225f, 225f, 0.4f);
if (Gdx.input.isKeyPressed(Input.Keys.UP) || Gdx.input.isKeyPressed(Input.Keys.W)) {
messageWindow.setVisible(false);
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)) {
messageWindow.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
messageWindow.setVisible(true);
}
stage.draw();
stage.act();
}
}
按上下键显示关闭
|