java - 我的程序无法绘制/渲染它所需要的内容

标签 java libgdx game-engine

我正在读一本关于游戏开发的书:Beginning Java Game Development with LibGDX。

我从书中复制了一个名为 CheesePlease3 的类,它提供了 Stage 类和 Actor 类,我必须从 Actor 类创建一个子类,命名为 BaseActor

我做的一切都是正确的,基本上复制粘贴了整个内容,并且它没有绘制任何对象。

所以我的问题是为什么?怎么了?

也许代码本身有点长,但很容易阅读。

这是 CheesePlease3 类:

public class CheesePlease3 extends Game {

public Stage mainStage;
private BaseActor mouse;
private BaseActor cheese;
private BaseActor floor;
private BaseActor winText;

@Override
public void create () {

    mainStage = new Stage();

    floor = new BaseActor();
    floor.setTexture(new Texture("floor.png"));
    floor.setPosition(0, 0);
    mainStage.addActor(floor);

    cheese = new BaseActor();
    cheese.setTexture(new Texture("cheese.png"));
    cheese.setPosition(300, 300);
    mainStage.addActor(cheese);

    mouse = new BaseActor();
    mouse.setTexture(new Texture("mouse.png"));
    mouse.setPosition(200, 200);
    mainStage.addActor(mouse);

    winText = new BaseActor();
    winText.setTexture(new Texture("youWon.png"));
    winText.setPosition(150, 150);
    winText.setVisible(false);
    mainStage.addActor(winText);

}

@Override
public void render(){
    // process input

    mouse.velocityX = 0;
    mouse.velocityY = 0;

    if (Gdx.input.isKeyPressed(Input.Keys.LEFT))
        mouse.velocityX -= 100;
    if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))
        mouse.velocityX += 100;
    if (Gdx.input.isKeyPressed(Input.Keys.UP))
        mouse.velocityY -= 100;
    if (Gdx.input.isKeyPressed(Input.Keys.DOWN))
        mouse.velocityY += 100;

    // update

    float dt = Gdx.graphics.getDeltaTime();
    mainStage.act(dt);

    // check win condition: Mouse must be overlapping cheese

    Rectangle mouseRectangle = mouse.getBoundingRectangle();
    Rectangle cheeseRectangle = cheese.getBoundingRectangle();

    if (mouseRectangle.contains(cheeseRectangle))
        winText.setVisible(true);

    // draw graphics

    Gdx.gl.glClearColor(0.8f, 0.8f, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


    mainStage.draw();

}

这是 BaseActor 类:

public class BaseActor extends Actor {

public TextureRegion region;
public Rectangle boundary;
public float velocityX;
public float velocityY;

public BaseActor(){
    super();
    region = new TextureRegion();
    boundary = new Rectangle();
    velocityX = 0;
    velocityY = 0;
}

public void setTexture (Texture t){
    int w = t.getWidth();
    int h = t.getHeight();
    setWidth(w);
    setHeight(h);
    region.setRegion(t);
}

public Rectangle getBoundingRectangle(){
    return boundary.set(getX(), getY(), getWidth(), getHeight());
}

@Override
public void act (float dt){
    super.act(dt);
    moveBy(velocityX * dt, velocityY * dt);
}

public void drawBatch (Batch batch, float parentAlpha){
    Color c = getColor();
    batch.setColor(c.r, c.g, c.b, c.a);

    if (isVisible())
        batch.draw(region, getX(), getY(), getOriginX(), getOriginY(),
                   getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
}

这是桌面启动器:

public static void main (String[] arg) {
    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    new LwjglApplication(new CheesePlease3(), config);
    config.title = "Mouse - Cheese";
}

最佳答案

BaseActor 必须重写 draw 方法。
我假设 drawBatch 方法应该重命名为 draw

诗: 上下运动是相反的。

关于java - 我的程序无法绘制/渲染它所需要的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56648201/

相关文章:

java - Spark从对象的RDD创建Dataframe,列顺序

java - 如果启动画面打开时中断,游戏会崩溃 - LIBGDX

c++ - 在文本冒险中实现实时?

java - 从 LoZ Oracle 游戏中创建 secret 系统

c++ - WM_KEYDOWN如何处理不同的按键?

java - 为什么扩展 String 的参数类型不能像 String 一样使用

java - 有没有办法从 Visual Studio Code 调试 Tomcat Java 应用程序

java - 如何将 gdx-bullet 合并到我的游戏引擎中?

java - 为什么我们在 stats1 类中使用这个静态?这是一个静态方法,以便我们可以直接调用它吗?

eclipse - 如何在 Eclipse 中将 “Other Linker Flags” 设置为 Xcode?