我刚刚开始使用 LibGdx 创建游戏,但是当我在窗口的顶部或右侧渲染纹理时,它不会显示在屏幕上。就像在纹理上绘制了两个黑框。 ()
这是代码:
public class SpaceInvaders extends ApplicationAdapter {
SpriteBatch batch;
//MainUI mUI;
Player p;
@Override
public void create () {
batch = new SpriteBatch();
Gdx.graphics.setResizable(false);
Gdx.graphics.setWindowedMode(672, 768);
//mUI = new MainUI(batch);
p = new Player(batch);
}
@Override
public void render () {
// Update
//mUI.update();
p.update();
// Render
batch.begin();
ScreenUtils.clear(0, 0, 0, 1);
//mUI.render();
p.render();
batch.end();
}
@Override
public void dispose () {
batch.dispose();
}
}
public class Player {
SpriteBatch batch;
Texture playerImg;
Sprite sprite;
//Projectile p;
public int x = 0, y = Gdx.graphics.getHeight() - 300f;
public Player(SpriteBatch batch) {
this.batch = batch;
playerImg = new Texture("res/player.png");
sprite = new Sprite(playerImg);
}
public void update() {
updateKeys();
/*
if(p != null) {
p.update();
if(p.y+p.image.getHeight() > Gdx.graphics.getHeight()) {
p = null;
}
}
*/
}
private void updateKeys() {
// D
if(Gdx.input.isKeyPressed(32)) {
x+=3;
if(x+playerImg.getWidth() > Gdx.graphics.getWidth()) x = Gdx.graphics.getWidth()-playerImg.getWidth();
}
// A
if(Gdx.input.isKeyPressed(29)) {
x-=3;
if(x < 0) x = 0;
}
// Space
/*
if(Gdx.input.isKeyPressed(62)) {
if(p == null) {
p =new Projectile(batch, x+playerImg.getWidth()/2-2, y+playerImg.getHeight()+1, 5);
}
}
*/
sprite.setX((float) x);
sprite.setY(Gdx.graphics.getHeight() - 300f);
}
public void render() {
sprite.draw(batch);
/*
if(p != null) {
p.render();
}
*/
}
}
我尝试删除除播放器之外的所有内容,但仍然不起作用。
我测试了你的代码,你是对的。我也是初学者。修复它的方法是删除您的:
Gdx.graphics.setResizable(false);
Gdx.graphics.setWindowedMode(672, 768);
转到您的桌面 > src > com.mygdxgame > DesktopLauncher 并添加以下内容:
config.setResizable(false);
config.setWindowedMode(672, 768);