我的 LibGDX 游戏项目仅渲染背景图像

问题描述 投票:0回答:1

我正在使用此参考制作 2D 跑步游戏。我将 GameActor 类扩展到所有游戏角色类(例如背景和跑步者),并重写每个类的绘制方法。但是当我运行该项目时,只有背景正在渲染

public class Runner extends GameActor{
...
public Runner(Body body) {
        super(body);
        TextureAtlas textureAtlas = new TextureAtlas(Constants.CHARACTERS_ATLAS_PATH);
        TextureRegion[] runningFrames = new TextureRegion[Constants.RUNNER_RUNNING_REGION_NAMES.length];
        for (int i = 0; i < Constants.RUNNER_RUNNING_REGION_NAMES.length; i++) {
            String path = Constants.RUNNER_RUNNING_REGION_NAMES[i];
            runningFrames[i] = textureAtlas.findRegion(path);
        }
        runningAnimation = new Animation(0.1f, runningFrames);
        stateTime = 0f;
        jumpingTexture = textureAtlas.findRegion(Constants.RUNNER_JUMPING_REGION_NAME);
        dodgingTexture = textureAtlas.findRegion(Constants.RUNNER_DODGING_REGION_NAME);
        hitTexture = textureAtlas.findRegion(Constants.RUNNER_HIT_REGION_NAME);
    }
...
@Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        if (dodging) {
            batch.draw(dodgingTexture, screenRectangle.x, screenRectangle.y + screenRectangle.height / 4, screenRectangle.width,
                    screenRectangle.height * 3 / 4);
        } else if (hit) {
            // When he's hit we also want to apply rotation if the body has been rotated
            batch.draw(hitTexture, screenRectangle.x, screenRectangle.y, screenRectangle.width * 0.5f,
                    screenRectangle.height * 0.5f, screenRectangle.width, screenRectangle.height, 1f, 1f,
                    (float) Math.toDegrees(body.getAngle()));
        } else if (jumping) {
            batch.draw(jumpingTexture, screenRectangle.x, screenRectangle.y, screenRectangle.width,
                    screenRectangle.height);
        } else {
            // Running
            stateTime += Gdx.graphics.getDeltaTime();
            batch.draw((TextureRegion) runningAnimation.getKeyFrame(stateTime, true), screenRectangle.x, screenRectangle.y,
                    screenRectangle.getWidth(), screenRectangle.getHeight());
        }
        System.out.println("Runner");
public class Background extends Actor {
...
public Background() {
        textureRegion = new TextureRegion(new Texture(Gdx.files.internal(Constants.BACKGROUND_IMAGE_PATH)));
        textureRegionBounds1 = new Rectangle(0 - Constants.WIN_WIDTH / 2, 0, Constants.WIN_WIDTH, Constants.WIN_HEIGHT);
        textureRegionBounds2 = new Rectangle(Constants.WIN_WIDTH / 2, 0, Constants.WIN_WIDTH, Constants.WIN_HEIGHT);
    }
...
@Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        batch.draw(textureRegion, textureRegionBounds1.x, textureRegionBounds1.y,
                Constants.WIN_WIDTH, Constants.WIN_HEIGHT);
        batch.draw(textureRegion, textureRegionBounds2.x, textureRegionBounds2.y,
                Constants.WIN_WIDTH, Constants.WIN_HEIGHT);
        System.out.println("Background");
    }

我的 GameStage 课程

public class GameStage extends Stage implements ContactListener {
...
public GameStage(){
        super(new ScalingViewport(Scaling.stretch, VIEWPORT_WIDTH, VIEWPORT_HEIGHT,
                new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT)));
        setupWorld();
        setupCamera();
    }

    public void setupWorld(){
        world = WorldUtils.createWorld();
        setupRunner();
        createEnemy();
        setupBackground();
        world.setContactListener(this);
    }
public void setupRunner(){
        runner = new Runner(WorldUtils.createRunner(world));
        addActor(runner);
    }
public void setupBackground(){
        addActor(new Background());
    }

我通过打印角色名称来检查这两种方法。两个单词同时打印,但只有背景在渲染。

我注释掉了 setupBackground() 方法来停止背景渲染并查看我的跑步者是否加载,但不幸的是我得到了一个黑屏窗口 我感觉这个问题与 Rectangle 的 screenRectangle 字段有关,该字段使用 UserData 类中的数据

public class UserData {
    public UserDataType userdataType;
    protected float width;
    protected float height;
    public UserData(){

    }
    public UserData(float width, float height) {
        this.width = width;
        this.height = height;
    }
    public UserDataType getUserDataType() {
        return userdataType;
    }
    public float getWidth() {
        return width;
    }

    public void setWidth(float width) {
        this.width = width;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }
}

RunnerUserData 类扩展了 UserData

public class RunnerUserData extends UserData {

    private Vector2 jumpingLinearImpulse;
    private final Vector2 runningPosition = new Vector2(Constants.RUNNER_X, Constants.RUNNER_Y);
    private final Vector2 dodgePosition = new Vector2(Constants.RUNNER_DODGE_X, Constants.RUNNER_DODGE_Y);

    public RunnerUserData() {
        super();
        jumpingLinearImpulse = Constants.RUNNER_JUMPING_LINEAR_IMPULSE;
        userdataType = UserDataType.RUNNER;
    }
...
}

但我不知道该怎么办

java intellij-idea libgdx game-physics box2d
1个回答
0
投票

您有一个 Stage 类..但是您没有共享创建 GameStage 的类文件、游戏/应用程序主渲染方法等的类文件...

您应该有一个 ApplicationAdapter 或 Game Class.. 就像下面给出的那样..

public class MyGame extends Game {


@Override
public void create () {

    stage = new GameStage();

}

@Override
public void render () {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.draw(); 
}

public void dispose() {
    stage.dispose();
}

}

  1. 您需要有一个扩展 Game 的主类。
  2. 在该类中,您应该使用... new Stage()...创建Stage...另外,您应该有一个render()方法,您可以在其中调用stage.draw()
© www.soinside.com 2019 - 2024. All rights reserved.