我当前正在使用Libgdx Java框架创建游戏,并且我试图创建一个库存系统,在其中使用Json文件创建表。
我正在使用的JSON文件称为uiskin.json。
当我创建Inventory类的实例(使用uiskin.json文件)时,它会给出异常
没有注册名称为“ default”的com.badlogic.gdx.scenes.scene2d.ui.List $ ListStyle
Inventory类中的错误发生在第28行,该行使用外观文件,该外观文件使用json文件。
这是JSON文件:
{
"com.badlogic.gdx.graphics.g2d.BitmapFont": {
"default-font": {
"file": "default.fnt"
}
},
"com.badlogic.gdx.graphics.Color": {
"green": {
"a": 1,
"b": 0,
"g": 1,
"r": 0
},
"white": {
"a": 1,
"b": 1,
"g": 1,
"r": 1
},
"red": {
"a": 1,
"b": 0,
"g": 0,
"r": 1
},
"black": {
"a": 1,
"b": 0,
"g": 0,
"r": 0
}
},
"com.badlogic.gdx.scenes.scene2d.ui.Skin$TintedDrawable": {
"dialogDim": {
"name": "white",
"color": {
"r": 0,
"g": 0,
"b": 0,
"a": 0.45
}
}
},
"com.badlogic.gdx.scenes.scene2d.ui.Button$ButtonStyle": {
"default": {
"down": "default-round-down",
"up": "default-round"
},
"toggle": {
"down": "default-round-down",
"checked": "default-round-down",
"up": "default-round"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle": {
"default": {
"down": "default-round-down",
"up": "default-round",
"font": "default-font",
"fontColor": "white"
},
"toggle": {
"down": "default-round-down",
"up": "default-round",
"checked": "default-round-down",
"font": "default-font",
"fontColor": "white",
"downFontColor": "red"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.ScrollPane$ScrollPaneStyle": {
"default": {
"vScroll": "default-scroll",
"hScrollKnob": "default-round-large",
"background": "default-rect",
"hScroll": "default-scroll",
"vScrollKnob": "default-round-large"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.SelectBox$SelectBoxStyle": {
"default": {
"font": "default-font",
"fontColor": "white",
"background": "default-select",
"scrollStyle": "default",
"listStyle": {
"font": "default-font",
"selection": "default-select-selection"
}
}
},
"com.badlogic.gdx.scenes.scene2d.ui.SplitPane$SplitPaneStyle": {
"default-vertical": {
"handle": "default-splitpane-vertical"
},
"default-horizontal": {
"handle": "default-splitpane"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.Window$WindowStyle": {
"default": {
"titleFont": "default-font",
"background": "default-window",
"titleFontColor": "white"
},
"dialog": {
"titleFont": "default-font",
"background": "default-window",
"titleFontColor": "white",
"stageBackground": "dialogDim"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.ProgressBar$ProgressBarStyle": {
"default-horizontal": {
"background": "default-slider",
"knob": "default-slider-knob"
},
"default-vertical": {
"background": "default-slider",
"knob": "default-round-large"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.Slider$SliderStyle": {
"default-horizontal": {
"background": "default-slider",
"knob": "default-slider-knob"
},
"default-vertical": {
"background": "default-slider",
"knob": "default-round-large"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle": {
"default": {
"font": "default-font",
"fontColor": "white"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.TextField$TextFieldStyle": {
"default": {
"selection": "selection",
"background": "textfield",
"font": "default-font",
"fontColor": "white",
"cursor": "cursor"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.CheckBox$CheckBoxStyle": {
"default": {
"checkboxOn": "check-on",
"checkboxOff": "check-off",
"font": "default-font",
"fontColor": "white"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.List$ListStyle": {
"default": {
"fontColorUnselected": "white",
"selection": "selection",
"fontColorSelected": "white",
"font": "default-font"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.Touchpad$TouchpadStyle": {
"default": {
"background": "default-pane",
"knob": "default-round-large"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.Tree$TreeStyle": {
"default": {
"minus": "tree-minus",
"plus": "tree-plus",
"selection": "default-select-selection"
}
},
"com.badlogic.gdx.scenes.scene2d.ui.TextTooltip$TextTooltipStyle": {
"default": {
"label": {
"font": "default-font",
"fontColor": "white"
},
"background": "default-pane",
"wrapWidth": 150
}
}
}
这是使用uiskin.json文件的类清单。
库存类的第28行发生异常。
package com.sps.game.Inventory2;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Payload;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Source;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Target;
import com.sps.game.Utility;
public class Inventory extends ScreenAdapter {
private Stage stage = new Stage();
public String UISKIN_PATH = "core/assets/Inventory/uiskin.json";
public Inventory() {
final Skin skin = new Skin();
skin.add("uiskin", UISKIN_PATH);
stage.setDebugAll(true);
final List<String> inventory = new List<String>(skin);
final List<String> sell = new List<String>(skin);
inventory.setItems("Axe", "Fuel", "Helmet", "Flux Capacitor", "Shoes", "Hamster", "Hammer", "Pirates Eye", "Cucumber");
Table table = new Table(skin);
table.setFillParent(true);
stage.addActor(table);
table.defaults();
table.add(inventory);
table.add("Merchant").row();
table.add(inventory).expand().fill();
table.add(sell).expand().fill();
DragAndDrop dnd = new DragAndDrop();
dnd.addSource(new Source(inventory) {
final DragAndDrop.Payload payload = new Payload();
@Override
public Payload dragStart(InputEvent event, float x, float y, int pointer) {
String item = inventory.getSelected(); //get the item from the inventory
payload.setObject(item); //set the item to the payload
inventory.getItems().removeIndex(inventory.getSelectedIndex()); //remove the selected item from the inventory
payload.setDragActor(new Label(item, skin)); //set the drag actor to be the payload.
payload.setInvalidDragActor(new Label(item + " (\"No thanks!\")", skin));
payload.setValidDragActor(new Label(item + " (\"I'll buy this!\")", skin));
return payload;
}
@Override
public void dragStop(InputEvent event, float x, float y, int pointer, Payload payload, Target target) {
if (target == null)
inventory.getItems().add((String) payload.getObject());
}
});
dnd.addTarget(new Target(sell) {
@Override
public boolean drag(Source source, Payload payload, float x, float y, int pointer) {
return !"Cucumber".equals(payload.getObject());
}
@Override
public void drop(Source source, Payload payload, float x, float y, int pointer) {
sell.getItems().add((String) payload.getObject());
}
});
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
@Override
public void dispose() {
stage.dispose();
}
}
答案 0 :(得分:0)
我已修复它。 IDE并未将其读取为JSON文件。我安装了libgdx插件,它检测到JSON文件。