gpt4 book ai didi

java - Libgdx 视口(viewport)不拉伸(stretch)

转载 作者:行者123 更新时间:2023-12-01 09:14:10 25 4
gpt4 key购买 nike

我目前在 libgdx 中创建拉伸(stretch)ViewPort 时遇到问题。正如您在图像中看到的,我添加了游戏屏幕并没有拉伸(stretch)到整个屏幕,并且有黑色空间。我想知道我做错了什么。我提供了下面代码的相关部分。

此外,我还尝试在 oncreate 函数中将视口(viewport)分配到舞台中,如下所示: m_stage = new Stage(m_viewport);但在这种情况下,我遇到了黑屏并且没有渲染任何内容(尽管没有编译错误)。

有人可以指出我做错了什么或者我错过了什么吗?提前致谢!

亲切的问候,卡瓦斯塔

Example

package com.block.it;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.StretchViewport;

public class GameMenu implements Screen {

private BlockIt m_blockIt;
private Texture m_btexture = new Texture(Gdx.files.internal("mainmenu1.png"));
private Image m_background = new Image(m_btexture);
private Texture m_starttexture = new Texture(Gdx.files.internal("buttons/c_start.png"));
private Image m_startGame = new Image(m_starttexture);
private Texture m_optionstexture = new Texture(Gdx.files.internal("buttons/c_controls.png"));
private Image m_options = new Image(m_optionstexture);
private Texture m_highscoretexture = new Texture(Gdx.files.internal("buttons/c_highscores.png"));
private Image m_highscore = new Image(m_highscoretexture);
private Texture m_continueTexture = new Texture(Gdx.files.internal("buttons/c_resume_game.png"));
private Image m_continue = new Image(m_continueTexture);
private Texture m_playerIcon = new Texture(Gdx.files.internal("player/player_move_front.png"));
private Image m_player = new Image(m_playerIcon);
private int m_buttonPressed = -1;

private boolean m_gameInProgress = false;

private StretchViewport m_viewport;
private PerspectiveCamera m_camera;
private Stage m_stage = new Stage();

public GameMenu(BlockIt blockIt, boolean gameInProgress) {
m_blockIt = blockIt;

m_camera = new PerspectiveCamera();
m_viewport = new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), m_camera);
m_viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
m_stage = new Stage();

}

@Override
public void show() {

if (!m_gameInProgress) {
m_startGame.setX(227);
m_startGame.setY(270);
m_startGame.addListener(new ClickListener() {
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
// m_blockIt.startNewGame();
}

@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
// switch to a new texture
/*Texture newTexture = new Texture(Gdx.files.internal("buttons/c_new_game.png"));
m_startGame.setDrawable(new SpriteDrawable(new Sprite(newTexture)));
*/
m_player.setX(-64);
m_player.setY(300);
m_stage.addActor(m_player);
m_buttonPressed = 0;
return true;
}
});

} else {
m_continue.setX(227);
m_continue.setY(270);
m_continue.addListener(new ClickListener() {
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
}


});

}

m_stage.addActor(m_background); //adds the image as an actor to the stage

if (m_gameInProgress) {
m_stage.addActor(m_continue);
} else {
m_stage.addActor(m_startGame);
}

Gdx.input.setInputProcessor(m_stage);

}

@Override
public void render(float delta) {
m_camera.update();

Gdx.gl.glClearColor(0, 0, 0, 1); //sets clear color to black
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); //clear the batch

m_stage.act(); //update all actors
m_stage.draw(); //draw all actors on the Stage.getBatch()
}



@Override
public void resize(int width, int height
) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void hide() {

}

@Override
public void dispose() {
}

}

最佳答案

您必须将相机和视口(viewport)设置为您用于对世界进行编程的值:例如。如果你的世界是800*480:

camera = new OrthographicCamera(800,480);
fitViewport = new FitViewport(800, 480, camera);

关于java - Libgdx 视口(viewport)不拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40697635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com