我正在读一本叫做《学习LIBGDX游戏开发》的书,但我遇到了问题,因为自从出版以来,已经发生了很多变化。问题在于Select Box
api已更改,不允许我按照书中的说明去做。
如何在选择框中实现与本书所述相同的行为?
错误就在网上:
selCharSkin = new SelectBox(CharacterSkin.values(), skinLibgdx);
它说:构造函数SelectBox(CharacterSkin [],Skin)是未定义的
快速修复:删除参数以匹配SelectBox(Skin)
MenuScree.java
package com.packtub.libgdx.canyonbunny.screens;
import javax.swing.plaf.ComboBoxUI;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
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.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.packtub.libgdx.canyonbunny.game.Assets;
import com.packtub.libgdx.canyonbunny.util.CharacterSkin;
import com.packtub.libgdx.canyonbunny.util.Constants;
import com.packtub.libgdx.canyonbunny.util.GamePreferences;
public class MenuScreen extends AbstractGameScreen{
private static final String TAG = MenuScreen.class.getName();
private Stage stage;
private Skin skinCanyonBunny;
private Skin skinLibgdx;
//Menu
private Image imgBackground;
private Image imgLogo;
private Image imgInfo;
private Image imgCoin;
private Image imgBunny;
private Button btnMenuPlay;
private Button btnMenuOptions;
//Opciones
private Window winOptions;
private TextButton btnWinOptSave;
private TextButton btnwinOptCancel;
private CheckBox chkSound;
private Slider sldSound;
private CheckBox chkMusic;
private Slider sldMusic;
private SelectBox selCharSkin;
private Image imgCharSkin;
private CheckBox chkShowFpsCounter;
//Debug
private final float DEBUG_REBUILD_INTERVAL = 5.0f;
private boolean debugEnabled = false;
private float debugRebuildStage;
ShapeRenderer debugRenderer;
public MenuScreen(Game game) {
super(game);
}
private void rebuildStage () {
skinCanyonBunny = new Skin(Gdx.files.internal(Constants.SKIN_CANYONBUNNY_UI),
new TextureAtlas(Constants.TEXTURE_ATLAS_UI));
skinLibgdx = new Skin(Gdx.files.internal(Constants.SKIN_LIBGDX_UI),
new TextureAtlas(Constants.TEXTURE_ATLAS_LIBGDX_UI));
//Creacion de las layers
Table layerBackground = buildBackgroundLayer();
Table layerObjects = buildObjetsLayer();
Table layerLogos = buildLogosLayer();
Table layerControl = buildControlsLayer();
Table layerOptionsWindow = buildOptionsWindowLayer();
//Prepara el stage para la pantalla del menu
stage.clear();
Stack stack = new Stack();
stage.addActor(stack);
stack.setSize(Constants.VIEWPORT_GUI_WIDTH, Constants.VIEWPORT_GUI_HEIGHT);
stack.add(layerBackground);
stack.add(layerObjects);
stack.add(layerLogos);
stack.add(layerControl);
stage.addActor(layerOptionsWindow);
}
private void loadSettings(){
GamePreferences prefs = GamePreferences.instance;
prefs.load();
chkSound.setChecked(prefs.sound);
sldSound.setValue(prefs.volSound);
chkMusic.setChecked(prefs.music);;
sldMusic.setValue(prefs.volMusic);
//selCharSkin.getSelection().set(prefs.charSkin);
selCharSkin.setSelected(prefs.charSkin);
onCharSkinSelected(prefs.charSkin);
chkShowFpsCounter.setChecked(prefs.showFpsCounter);
}
private void saveSettings(){
GamePreferences prefs = GamePreferences.instance;
prefs.sound = chkSound.isChecked();
prefs.volSound = sldSound.getValue();
prefs.music = chkMusic.isChecked();
prefs.volMusic = sldMusic.getValue();
prefs.charSkin = selCharSkin.getSelectedIndex();
prefs.showFpsCounter = chkShowFpsCounter.isChecked();
prefs.save();
}
private void onCharSkinSelected(int index) {
CharacterSkin skin = CharacterSkin.values()[index];
imgCharSkin.setColor(skin.getColor());
}
private void onSaveClicked(){
saveSettings();
onCancelClicked();
}
private void onCancelClicked() {
btnMenuPlay.setVisible(true);
btnMenuOptions.setVisible(true);
winOptions.setVisible(false);
}
private Table buildOptWinAudioSettings(){
Table tbl = new Table();
//Titulo:Audio
tbl.pad(10,10,0,10);
tbl.add(new Label("Audio", skinLibgdx, "default-font", Color.ORANGE)).colspan(3);
tbl.row();
tbl.columnDefaults(0).padRight(10);
tbl.columnDefaults(1).padRight(10);
//CheckBox: Titulo Sound, sonido volumen
chkSound = new CheckBox("", skinLibgdx);
tbl.add(chkSound);
tbl.add(new Label("Sonido",skinLibgdx));
sldSound = new Slider(0.0f, 1.0f, 0.1f, false, skinLibgdx);
tbl.add(sldSound);
tbl.row();
//CheckBox:Titulo Musica, sonido musica
chkMusic = new CheckBox("", skinLibgdx);
tbl.add(chkMusic);
tbl.add(new Label("Musica", skinLibgdx));
sldMusic = new Slider(0.0f, 1.0f, 0.1f, false, skinLibgdx);
tbl.add(sldMusic);
tbl.row();
return tbl;
}
private Table buildOptWinSkinSelection(){//* Checkear
Table tbl = new Table();
//Titulo: Skin del Personaje
tbl.pad(10, 10, 0, 10);
tbl.add(new Label("Skin del Personaje", skinLibgdx, "default-font",
Color.ORANGE)).colspan(2);
tbl.row();
//Selector de skin
selCharSkin = new SelectBox(CharacterSkin.values(), skinLibgdx);
selCharSkin.addListener(new ChangeListener(){
@Override
public void changed(ChangeEvent event, Actor actor){
onCharSkinSelected(((SelectBox)actor).getSelectedIndex());
}
});
tbl.add(selCharSkin).width(120).padRight(20);
//Muestra del skin
imgCharSkin = new Image(Assets.instance.bunny.head);
tbl.add(imgCharSkin).width(50).height(50);
return tbl;
}
private Table buildOptWinDebug(){
Table tbl = new Table();
//Titulo: "Debug"
tbl.pad(10,10,0,10);
tbl.add(new Label("Debug", skinLibgdx, "default-font", Color.RED)).colspan(3);
tbl.row();
tbl.columnDefaults(0).padRight(10);
tbl.columnDefaults(1).padRight(10);
//CheckBox: Mostrar FPS
chkShowFpsCounter = new CheckBox("", skinLibgdx);
tbl.add(new Label("Mostrar FPS", skinLibgdx));
tbl.add(chkShowFpsCounter);
tbl.row();
return tbl;
}
private Table buildOptWinButtons(){
Table tbl = new Table();
//Separador
Label lbl = null;
lbl = new Label("", skinLibgdx);
lbl.setColor(0.75f,0.75f,0.75f,1);
lbl.setStyle(new LabelStyle(lbl.getStyle()));
lbl.getStyle().background = skinLibgdx.newDrawable("white");
tbl.add(lbl).colspan(2).height(1).width(220).pad(0,0,0,1);
tbl.row();
lbl = new Label("", skinLibgdx);
lbl.setColor(0.5f,0.5f,0.5f,1);
lbl.setStyle(new LabelStyle(lbl.getStyle()));
lbl.getStyle().background = skinLibgdx.newDrawable("white");
tbl.add(lbl).colspan(2).height(1).width(220).pad(0,1,5,0);
tbl.row();
//Boton Salvar
btnWinOptSave = new TextButton("Guardar", skinLibgdx);
tbl.add(btnWinOptSave).padRight(30);
btnWinOptSave.addListener(new ChangeListener(){
@Override
public void changed (ChangeEvent event, Actor actor){
onSaveClicked();
}
});
//Boton Cancelar
btnwinOptCancel = new TextButton("Cancelar", skinLibgdx);
tbl.add(btnwinOptCancel);
btnwinOptCancel.addListener(new ChangeListener(){
@Override
public void changed (ChangeEvent event, Actor actor){
onCancelClicked();
}
});
return tbl;
}
private Table buildOptionsWindowLayer() {
winOptions = new Window("Opciones", skinLibgdx);
//Audio Settings
winOptions.add(buildOptWinAudioSettings()).row();
//Personaje Settings
winOptions.add(buildOptWinSkinSelection()).row();
//Debug
winOptions.add(buildOptWinDebug()).row();
//Separador y botones
winOptions.add(buildOptWinButtons()).pad(10, 0, 10, 0);
//Ventana opciones un poco transparente
winOptions.setColor(1,1,1,0.8f);
//Ocultar la ventana por default
winOptions.setVisible(false);
if(debugEnabled)winOptions.debug();
//Permitir a tablelayout recalcular el tamaño y posicion de los widgets
winOptions.pack();
//Mueve la ventana opciones para la parte derecha abajo
winOptions.setPosition(Constants.VIEWPORT_GUI_WIDTH - winOptions.getWidth() - 50,
50);
return winOptions;
}
private Table buildControlsLayer() {
Table layer = new Table();
layer.right().bottom();
//Boton de Jugar
btnMenuPlay = new Button(skinCanyonBunny, "play");
layer.add(btnMenuPlay);
btnMenuPlay.addListener(new ChangeListener(){
@Override
public void changed(ChangeEvent event, Actor actor){
onPlayClicked();
}
});
layer.row();
//Boton de Opciones
btnMenuOptions = new Button(skinCanyonBunny, "options");
layer.add(btnMenuOptions);
btnMenuOptions.addListener(new ChangeListener(){
@Override
public void changed(ChangeEvent event, Actor actor) {
onOptionsClicked();
}
});
if (debugEnabled)layer.debug();
return layer;
}
private Table buildLogosLayer() {
Table layer = new Table();
layer.left().top();
//Logo del juego
imgLogo = new Image(skinCanyonBunny, "logo");
layer.add(imgLogo);
layer.row().expandY();
//Info Logo
imgInfo = new Image (skinCanyonBunny, "info");
layer.add(imgInfo).bottom();
if(debugEnabled) layer.debug();
return layer;
}
private Table buildObjetsLayer() {
Table layer = new Table();
//Monedas
imgCoin = new Image(skinCanyonBunny, "coins");
layer.add(imgCoin);
imgCoin.setPosition(135,80);
//Conejo
imgBunny = new Image(skinCanyonBunny, "bunny");
layer.add(imgBunny);
imgBunny.setPosition(355, 40);
return layer;
}
private Table buildBackgroundLayer() {
Table layer = new Table();
//Background
imgBackground = new Image(skinCanyonBunny, "background");
layer.add(imgBackground);
return layer;
}
private void onPlayClicked() {
game.setScreen(new GameScreen(game));
}
private void onOptionsClicked() {
loadSettings();
btnMenuPlay.setVisible(false);
btnMenuOptions.setVisible(false);
winOptions.setVisible(true);
}
@Override
public void render(float deltaTime) {
Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (debugEnabled){
debugRebuildStage -= deltaTime;
if (debugRebuildStage <= 0){
debugRebuildStage = DEBUG_REBUILD_INTERVAL;
rebuildStage();
}
}
stage.act(deltaTime);
stage.draw();
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(Math.round(Constants.VIEWPORT_GUI_WIDTH),
Math.round(Constants.VIEWPORT_GUI_HEIGHT));
}
@Override
public void show() {
stage = new Stage(new StretchViewport(Constants.VIEWPORT_GUI_WIDTH, Constants.VIEWPORT_GUI_HEIGHT));
Gdx.input.setInputProcessor(stage);
rebuildStage();
}
@Override
public void hide() {
stage.dispose();
skinCanyonBunny.dispose();
skinLibgdx.dispose();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
}
GamePreferences.java
package com.packtub.libgdx.canyonbunny.util;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.math.MathUtils;
public class GamePreferences {
public static final String TAG = GamePreferences.class.getName();
public static final GamePreferences instance = new GamePreferences();
public boolean sound;
public boolean music;
public float volSound;
public float volMusic;
public int charSkin;
public boolean showFpsCounter;
private Preferences prefs;
//singleton
private GamePreferences (){
prefs = Gdx.app.getPreferences(Constants.PREFERENCES);
}
public void load (){
sound = prefs.getBoolean("sound", true);
music = prefs.getBoolean("music", true);
volSound = MathUtils.clamp(prefs.getFloat("volSound", 0.5f), 0.0f, 1.0f);
volMusic = MathUtils.clamp(prefs.getFloat("volMusic", 0.5f), 0.0f, 1.0f);
charSkin = MathUtils.clamp(prefs.getInteger("charSkin", 0), 0, 2);
showFpsCounter = prefs.getBoolean("showFpsCounter", false);
}
public void save (){
prefs.putBoolean("sound", sound);
prefs.putBoolean("music", music);
prefs.putFloat("volSound", volSound);
prefs.putFloat("volMusic", volMusic);
prefs.putInteger("charSkin", charSkin);
prefs.putBoolean("showFpsCounter", showFpsCounter);
prefs.flush();
}
CharacterSkin.java
package com.packtub.libgdx.canyonbunny.util;
import com.badlogic.gdx.graphics.Color;
public enum CharacterSkin {
WHITE("WHITE", 1.0f, 1.0f, 1.0f),
GRAY("Gray", 0.7f, 0.7f, 0.7f),
BROWN("Brown", 0.7f, 0.5f, 0.3f);
private String name;
private Color color = new Color();
private CharacterSkin(String name, float r, float g, float b){
this.name = name;
color.set(r, g, b, 1.0f);
}
@Override
public String toString(){
return name;
}
public Color getColor(){
return color;
}
}
最佳答案
现在,CharacterSkin将泛型用于项目类型。因此,在声明引用和实例化引用时必须指定项目类型。并且您必须使用setItems()
方法,因为构造函数不再采用项目列表。
private SelectBox<CharacterSkin> selCharSkin;
和
selCharSkin = new SelectBox<CharacterSkin>(skinLibgdx);
selCharSkin.setItems(CharacterSkin.values());