gpt4 book ai didi

java - 渲染图像不显示 Java Libgdx

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

我正在尝试渲染图像,但是当我作为桌面启动器运行游戏时,看不到图像,但是我相信图像正在渲染,因为它与另一个 Sprite 重叠以提供输出。

if (chicken.overlaps(farmer)){
System.out.print("GameOver");
gameOver();

我没有收到任何错误。

相机

OrthographicCamera camera;
camera = new OrthographicCamera();
camera.setToOrtho(false, 1920, 1080);
camera.update();
inputUpdate(touch, camera);
batch.setProjectionMatrix(camera.combined);

enter image description here

代码:

public void render(float delta) {


camera.update();
inputUpdate(touch, camera);
batch.setProjectionMatrix(camera.combined);



Iterator<Rectangle> iter = chickens.iterator();
while(iter.hasNext()){
Rectangle chicken = iter.next();
if(farmerX < (chicken.x - 85/2)) chicken.x -= 2.5;
if(farmerX > (chicken.x - 85/2))chicken.x += 2.5;
if (farmerY < (chicken.y - 66/2))chicken.y -= 2.5;
if(farmerY > (chicken.y - 66/2))chicken.y += 2.5;

float diffYchick;
float diffXchick;
float angleDegreeschick;

diffYchick = (float) (farmerY - chicken.y);
diffXchick = (float) (farmerX - chicken.x);

angleDegreeschick = (float) Math.toDegrees(Math.atan2(diffYchick, diffXchick));


game.batch.begin();
game.batch.draw(chickenImage, (float)chicken.x, (float)chicken.y, (float)42.5, (float)33, (float)85, (float)66, (float)1, (float)1, (float)angleDegreeschick);

game.batch.end();


int stop;
stop = 0;
switch(stop){
case 0:
if (chicken.overlaps(farmer)){
System.out.print("GameOver");
gameOver();

stop +=1;
break;
}
}



}

float diffY;
float diffX;
diffY = (float) (touch.y - farmerY);
diffX = (float) (touch.x - farmerX);

float angleDegrees;

angleDegrees = (float) Math.toDegrees(Math.atan2(diffY, diffX));

if(TimeUtils.nanoTime() - lastSpawnTime > 1000000000) spawnChicken();

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

camera.update();
inputUpdate(touch, camera);
batch.setProjectionMatrix(camera.combined);





FarmerAsset.stateTime += Gdx.graphics.getDeltaTime();
FarmerAsset.currentFrame = FarmerAsset.walkAnimation.getKeyFrame(FarmerAsset.stateTime, true);

game.batch.begin();
game.batch.draw(FarmerAsset.currentFrame, farmerX, farmerY, (float)85, (float)85, (float)170, (float)170, (float)1, (float)1, (float)angleDegrees + 270);
game.font.draw(game.batch, "Chickens Running: " + runningChickens, 0, 1080);
game.batch.end();

if(Gdx.input.isTouched()){
touch.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touch);


if(farmerY <= touch.y && touch.y<= farmerY + 170 &&
farmerX <= touch.x && touch. x <= farmerX + 170){
}else{
if (touch.x - 85 > farmerX){
farmerX +=5;
}
if (touch.x - 85 < farmerX){
farmerX -=5;
}
if (touch.y - 85 > farmerY){
farmerY +=5;
}
if (touch.y - 85 < farmerY){
farmerY -=5;
}
}


}

if(Gdx.input.isKeyPressed(Keys.A)||Gdx.input.isKeyPressed(Keys.LEFT)){
farmerX -= 10;
}
else if(Gdx.input.isKeyPressed(Keys.D)||Gdx.input.isKeyPressed(Keys.RIGHT)){
farmerX += 10;
}
else if(Gdx.input.isKeyPressed(Keys.W)||Gdx.input.isKeyPressed(Keys.UP)){
farmerY += 10;
}
else if(Gdx.input.isKeyPressed(Keys.S)||Gdx.input.isKeyPressed(Keys.DOWN)){
farmerY -= 10;
}

if(farmerX < 0) farmerX = 0;
if(farmerX > 1920-170) farmerX = 1920-170;
if(farmerY < 0) farmerY = 0;
if(farmerY > 1080-170) farmerY = 1080-170;
farmer.x = farmerX;
farmer.y = farmerY;



}

农民

game.batch.draw(FarmerAsset.currentFrame, farmerX, farmerY, (float)85, (float)85, (float)170, (float)170, (float)1, (float)1, (float)angleDegrees + 270);

最佳答案

我在渲染方法中发现了一些问题:

  • 多次重复更新相机和触摸。 render 方法中的前三行将进一步向下重复。他们应该只被调用一次。如果您不移动相机,则没有理由更新它,但通常如果您要移动它,则在处理可能导致其移动的任何触摸逻辑后,您会希望移动并更新它。
  • 在画小鸡之前没有清除屏幕。在使用 Sprite 批处理之前,您必须调用 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  • 不将所有鸡分批在一起。您为每只鸡调用 batch.beginbatch.end,这是 CPU 密集型操作,如果鸡数量较多,很快就会成为游戏性能的瓶颈。您应该将 batch.begin 移至 while 循环之前,将 batch.end 移至 while 循环之后。
  • 混淆了更新游戏状态和绘图的顺序。在绘制这些对象之前,您应该处理所有触摸逻辑和对象位置更新。 (这不会导致您的问题,但会导致用户输入出现一帧延迟。)

最有可能的是,由于在更新相机和调用 glClear 之前使用 Sprite 批处理绘制鸡,因此鸡未正确渲染。

这大致就是我重新排序的方式。但如果这是我的项目,我会做更多的工作来分离逻辑和渲染。例如,我会使用 Sprite 对象来表示鸡,而不仅仅是矩形。然后,我会在渲染方法开始时适当移动所有 Sprite ,然后再对所有 Sprite 进行一次循环,以在最后将它们提交给 SpriteBatch。这将使您的代码不易出现错误。

public void render(float delta) {

inputUpdate(touch, camera);

if(TimeUtils.nanoTime() - lastSpawnTime > 1000000000) spawnChicken();

if(Gdx.input.isTouched()){
touch.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touch);


if(farmerY <= touch.y && touch.y<= farmerY + 170 &&
farmerX <= touch.x && touch. x <= farmerX + 170){
}else{
if (touch.x - 85 > farmerX){
farmerX +=5;
}
if (touch.x - 85 < farmerX){
farmerX -=5;
}
if (touch.y - 85 > farmerY){
farmerY +=5;
}
if (touch.y - 85 < farmerY){
farmerY -=5;
}
}


}

if(Gdx.input.isKeyPressed(Keys.A)||Gdx.input.isKeyPressed(Keys.LEFT)){
farmerX -= 10;
}
else if(Gdx.input.isKeyPressed(Keys.D)||Gdx.input.isKeyPressed(Keys.RIGHT)){
farmerX += 10;
}
else if(Gdx.input.isKeyPressed(Keys.W)||Gdx.input.isKeyPressed(Keys.UP)){
farmerY += 10;
}
else if(Gdx.input.isKeyPressed(Keys.S)||Gdx.input.isKeyPressed(Keys.DOWN)){
farmerY -= 10;
}

if(farmerX < 0) farmerX = 0;
if(farmerX > 1920-170) farmerX = 1920-170;
if(farmerY < 0) farmerY = 0;
if(farmerY > 1080-170) farmerY = 1080-170;
farmer.x = farmerX;
farmer.y = farmerY;

camera.update();
batch.setProjectionMatrix(camera.combined);

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

game.batch.begin();

Iterator<Rectangle> iter = chickens.iterator();
while(iter.hasNext()){
Rectangle chicken = iter.next();
if(farmerX < (chicken.x - 85/2)) chicken.x -= 2.5;
if(farmerX > (chicken.x - 85/2))chicken.x += 2.5;
if (farmerY < (chicken.y - 66/2))chicken.y -= 2.5;
if(farmerY > (chicken.y - 66/2))chicken.y += 2.5;

float diffYchick;
float diffXchick;
float angleDegreeschick;

diffYchick = (float) (farmerY - chicken.y);
diffXchick = (float) (farmerX - chicken.x);

angleDegreeschick = (float) Math.toDegrees(Math.atan2(diffYchick, diffXchick));

game.batch.draw(chickenImage, (float)chicken.x, (float)chicken.y, (float)42.5, (float)33, (float)85, (float)66, (float)1, (float)1, (float)angleDegreeschick);

int stop;
stop = 0;
switch(stop){
case 0:
if (chicken.overlaps(farmer)){
System.out.print("GameOver");
gameOver();

stop +=1;
break;
}
}

}

game.batch.end();

float diffY = (float) (touch.y - farmerY);
float diffX = (float) (touch.x - farmerX);

float angleDegrees = (float) Math.toDegrees(Math.atan2(diffY, diffX));

FarmerAsset.stateTime += Gdx.graphics.getDeltaTime();
FarmerAsset.currentFrame = FarmerAsset.walkAnimation.getKeyFrame(FarmerAsset.stateTime, true);

game.batch.begin();
game.batch.draw(FarmerAsset.currentFrame, farmerX, farmerY, (float)85, (float)85, (float)170, (float)170, (float)1, (float)1, (float)angleDegrees + 270);
game.font.draw(game.batch, "Chickens Running: " + runningChickens, 0, 1080);
game.batch.end();

}

除此之外,您的 switch 语句有一个错误...break 应该在 if 语句之外调用。现在不是问题,但稍后当您添加到 switch 语句时就会出现问题。我没有更正上面的问题。

仅供引用,您不必在 float 字之前继续输入 (float) 。只需在它们后面输入 f 即可。

关于java - 渲染图像不显示 Java Libgdx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24784624/

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