gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:27:40 27 4
gpt4 key购买 nike

我正在读一本关于游戏开发的书: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/

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