And*_*rew 5 java libgdx scene2d
我已经制作了我的RPG的alpha版本,现在我想实现一个带有多个标签的菜单.当你在游戏中并按下tab或escape时,我想显示一个用户界面,它将有一个带有3个按钮的条形顶部,如主菜单,技能树和库存.问题是当你切换标签时,我需要停止绘制之前选择的标签并从中禁用演员,这样当您从当前标签中点击某些内容时,它也不会单击未绘制的标签上的按钮但是它就在那里......我如何禁用演员,按钮以便我可以绘制一个新标签而不必担心当前标签与前一个标签重叠?
编辑:我想避免做多个阶段.我想有一个阶段,每个选项卡都定义在其上.
nEx*_*are 11
隐形演员不接收触摸事件,这是完成标签界面的最简单方法,只需适当地设置标签内容演员的可见性.
以下是我在libgdx中实现选项卡式界面的方法:
package com.badlogic.gdx.tests.lwjgl;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ButtonGroup;
import com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Stack;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
class Game3 extends ApplicationAdapter {
Skin skin;
Stage stage;
@Override
public void create () {
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
stage = new Stage(new ScreenViewport());
Table main = new Table();
main.setFillParent(true);
// Create the tab buttons
HorizontalGroup group = new HorizontalGroup();
final Button tab1 = new TextButton("Tab1", skin, "toggle");
final Button tab2 = new TextButton("Tab2", skin, "toggle");
final Button tab3 = new TextButton("Tab3", skin, "toggle");
group.addActor(tab1);
group.addActor(tab2);
group.addActor(tab3);
main.add(group);
main.row();
// Create the tab content. Just using images here for simplicity.
Stack content = new Stack();
final Image content1 = new Image(skin.newDrawable("white", 1,0,0,1));
final Image content2 = new Image(skin.newDrawable("white", 0,1,0,1));
final Image content3 = new Image(skin.newDrawable("white", 0,0,1,1));
content.addActor(content1);
content.addActor(content2);
content.addActor(content3);
main.add(content).expand().fill();
// Listen to changes in the tab button checked states
// Set visibility of the tab content to match the checked state
ChangeListener tab_listener = new ChangeListener(){
@Override
public void changed (ChangeEvent event, Actor actor) {
content1.setVisible(tab1.isChecked());
content2.setVisible(tab2.isChecked());
content3.setVisible(tab3.isChecked());
}
};
tab1.addListener(tab_listener);
tab2.addListener(tab_listener);
tab3.addListener(tab_listener);
// Let only one tab button be checked at a time
ButtonGroup tabs = new ButtonGroup();
tabs.setMinCheckCount(1);
tabs.setMaxCheckCount(1);
tabs.add(tab1);
tabs.add(tab2);
tabs.add(tab3);
stage.addActor(main);
Gdx.input.setInputProcessor(stage);
}
@Override
public void render () {
stage.act();
stage.draw();
}
@Override
public void dispose () {
stage.dispose();
skin.dispose();
}
public static void main (String[] args) {
new LwjglApplication(new Game3(), new LwjglApplicationConfiguration());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4658 次 |
| 最近记录: |