我是 libdgx 的新手..我想创建一个带有背景图像的按钮,我用 gdx-texturepacker.jar 制作了图像包,然后将其加载到 TextureAtlus 中,但现在无论我运行我的程序,它都会出现此错误
没有使用名称注册 com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle:默认
我的代码
package com.me.mygdxgame;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
public class Desperate implements ApplicationListener {
SpriteBatch batch;
int soundcheck=0;
Music music;
Actor background;
Actor red_player;
Button b;
TextureAtlas atlas;
Skin skin;
int x;
int y;
@Override
public void create() {
x = Gdx.app.getGraphics().getWidth();
y = Gdx.app.getGraphics().getHeight();
batch = new SpriteBatch();
music = Gdx.audio.newMusic(Gdx.files.internal("data/open.wav"));
background = new Actor("data/green_surface.png",
Gdx.app.getGraphics().getWidth(),
Gdx.app.getGraphics().getHeight(),0,0);
red_player = new Actor("data/red2.png", 100,150,230,120);
atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack"));
skin = new Skin(atlas);
b = new Button(skin);
}
@Override
public void dispose() {
red_player.dispose();
batch.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
if(Gdx.input.isTouched()){
red_player.position.x+=1f;
red_player.actor.setPosition(red_player.position.x,red_player.position.y);
}
batch.begin();
//batch.draw(mario,0,0);
background.actor.draw(batch);
red_player.actor.draw(batch);
b.draw(batch, 0);
//front.draw(batch);
batch.end();
if(soundcheck==0)
{
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
//music.play();
//sound.play(1f);
soundcheck=1;
System.out.println("Sound played!");
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
Run Code Online (Sandbox Code Playgroud)
请告诉我应该做什么,或者如果我没有走上正确的道路,请告诉我在屏幕上绘制按钮的最简单方法是什么
b = new Button(skin);
Run Code Online (Sandbox Code Playgroud)
是相同的
b = new Button(skin,"default");
Run Code Online (Sandbox Code Playgroud)
所以它从 .json皮肤文件中寻找样式定义,而您似乎没有
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
default: { up: btn_default_normal, down: btn_default_pressed, over: btn_default_focused, disabled: btn_default_disabled, font: default-font, fontColor: text-<col> , downFontColor: text-<col>, overFontColor: text-<col>, disabledFontColor: text-<col>},
Run Code Online (Sandbox Code Playgroud)
看来如果你像这里一样在没有SkinFile参数的情况下初始化你的皮肤......
skin = new Skin(atlas);
Run Code Online (Sandbox Code Playgroud)
...那么您将无法将 Skin 作为参数传递给 scene2d.ui 元素构造函数。那些具有 Skin 参数的构造函数假设您有此 SkinFile,其只是为了方便在初始化时使用“默认”样式或其他样式。
如果您的皮肤仅包含TextureAtlas,您仍然可以执行以下操作:
skin.getDrawable("mypackedimage");
Run Code Online (Sandbox Code Playgroud)
大多数情况下,我认为它的目的是在运行时动态构建您的皮肤,而不是持久的 .json 皮肤方法。
你应该谷歌一下如何制作你自己的皮肤文件,LibGDX 论坛上也有一些免费的。
| 归档时间: |
|
| 查看次数: |
2959 次 |
| 最近记录: |