javalibgdx中的屏幕文本实现
我在实现显示和使用玩家用户名的功能时遇到了一些问题。我正在制作一个windows和android之间的跨平台应用程序,我试图在玩家开始键入时显示他们的姓名,可以按enter继续,也可以按级别选择按钮进入下一个屏幕。但每次我按“回车”键时,它都会崩溃。而且,它不会显示或接受玩家的名字。我知道代码不是最优的,比如我制作了多个表,但我希望有人能告诉我哪里犯了错误,这样可能会有用。提前谢谢 代码如下:javalibgdx中的屏幕文本实现,java,android,fonts,libgdx,Java,Android,Fonts,Libgdx,我在实现显示和使用玩家用户名的功能时遇到了一些问题。我正在制作一个windows和android之间的跨平台应用程序,我试图在玩家开始键入时显示他们的姓名,可以按enter继续,也可以按级别选择按钮进入下一个屏幕。但每次我按“回车”键时,它都会崩溃。而且,它不会显示或接受玩家的名字。我知道代码不是最优的,比如我制作了多个表,但我希望有人能告诉我哪里犯了错误,这样可能会有用。提前谢谢 代码如下: package com.paladinzzz.game.screens; import com.ba
package com.paladinzzz.game.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.paladinzzz.game.CrossplatformApp;
import com.paladinzzz.game.util.Constants;
import com.paladinzzz.game.audio.MusicHandler;
import static com.badlogic.gdx.Gdx.input;
import static com.paladinzzz.game.screens.MenuScreen.musicHandler;
public class LoginScreen implements Screen {
private CrossplatformApp game;
private Stage stage;
private ImageButton backButton, playButton;
private Texture backTexture, playTexture, background;
private Drawable drawableBack, drawablePlay;
private OrthographicCamera camera;
BitmapFont font = new BitmapFont();
int[] x = new int[255];
public static boolean inPlayscreen = false;
public static String playername = "";
private boolean isConverted = false;
private Table table, table2;
public LoginScreen(CrossplatformApp game) {
this.game = game;
this.camera = new OrthographicCamera();
this.stage = new Stage(new FillViewport(Constants.WIDTH, Constants.HEIGHT, camera));
this.playTexture = new Texture("Screens/LoginScreen/LvlSelection.png");
this.backTexture = new Texture("Screens/BackButton.png");
this.background = new Texture("Screens/LoginScreen/loginscreen.png");
}
@Override
public void show() {
//Give the texture of the backButton to a new ImageButton
drawableBack = new TextureRegionDrawable(new TextureRegion(backTexture));
backButton = new ImageButton(drawableBack);
backButton.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new MenuScreen(game));
MenuScreen.musicHandler.stopMusic();
}
});
//Give the texture of the playButton to a new ImageButton
drawablePlay = new TextureRegionDrawable(new TextureRegion(playTexture));
playButton = new ImageButton(drawablePlay);
playButton.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new LevelScreen(game));
inPlayscreen = true;
}
});
//Elements can be added here to the stage
input.setInputProcessor(stage);
//A table gets made with the button which leads to the level selection screen
table = new Table();
table.bottom();
table.setFillParent(true);
table.add(playButton).padBottom(40);
stage.addActor(table);
//A table gets made with the button which leads back to the main menu
table2 = new Table();
table2.bottom();
table2.setFillParent(true);
table2.add(backButton).padBottom(13).padRight(640);
table2.row();
stage.addActor((table2));
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(Gdx.input.isKeyJustPressed(Input.Keys.ENTER)){
game.setScreen(new LevelScreen(game));
inPlayscreen = true;
if (!isConverted){
playername = playername.substring(0, 1).toUpperCase() + playername.substring(1, playername.length()).toLowerCase();
isConverted = true;
}
}
game.batch.begin();
//A possibility gets made to insert the name of the player
game.batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
for (int i : x) {
if (input.isKeyJustPressed(i)){
if (i != 62 && i != 67 && i!= 66) {
playername += Input.Keys.toString(i).toLowerCase();
} else if (i == 67 & playername.length() > 0){
playername = playername.substring(0, playername.length() - 1).toLowerCase();
} else {
playername += " ";
}
}
}
font.draw(game.batch, playername, 0, 0);
game.batch.end();
stage.draw();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
stage.dispose();
}
}
这就是“登录屏幕”的外观:
这是错误消息:
Exception in thread "LWJGL Application"
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.substring(String.java:1963)
at com.paladinzzz.game.screens.LoginScreen.render(LoginScreen.java:108)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.paladinzzz.game.CrossplatformApp.render(CrossplatformApp.java:30)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
使用断点进行调试:
此线路的故障解决方案是:
playername = playername.substring(0, 1).toUpperCase() + playername.substring(1, playername.length()).toLowerCase();
您的第二个子字符串需要使用
playername.length()-1
我只是快速浏览了一下你的代码,所以我不能100%确定这是否是解决方案,但我注意到你自己构建了一个非常“有趣”的输入例程。也许你也推动了输入处理器并使用它的输入。(稍后我会仔细看一看。我现在很忙编辑:你能发布stacktrace以获取更多关于崩溃的信息吗?刚刚做了,也许这会澄清问题。谢谢。我看到“字符串超出范围:1”,但即使我键入一些内容并按enter键,它也不起作用,所以这就是我提出此问题的原因添加bre在崩溃之前点击并检查调试器中的值。你的playername值是多少?编辑:你的第二个子串从0开始计数,所以你需要长度()-1抱歉,花了这么长时间,我试图自己修复它,但显然没有真正起作用。它还说“playername”里面除了“playername”之外什么都没有。我会在帖子中放一个调试的屏幕截图。你实现了我发布在这里的解决方案吗?你的图像崩溃是因为你有一个长度为0的字符串并开始拾取索引1中的子字符串。如果操纵字符串,可能还需要确保字符串足够长;如果字符串太短,则可能未被操纵。我将其转换为“playername=playername.substring(-1,1).toUpperCase()+playername.substring(1,playername.length()-1).toLowerCase();“”因此,即使没有任何内容,它也可以减少-1,换句话说,当它为0时,但这也会使它崩溃。您无法创建负索引的子字符串。将字符串想象为数组(char数组)。你不能访问一个有意义的无效indexOkay..忘记了..但即使我将其转换为
playername=playername.substring(1,2).toUpperCase()+playername.substring(2,playername.length()-1).toLowerCase();
值也不会增加