gpt4 book ai didi

java - 使用 libgdx 渲染游戏时出现问题

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

我正在尝试使用 libgdx 来表示一些图像。为此,我有矩形和纹理变量。问题是,当我让代码以这种方式运行时,它只绘制图像 1 次,然后立即清除它。如果我画了这些图像,我希望它们能够保留下来。

 @Override
public void render () {
int x,y;
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
if(matrixBuilt!=true){
for(x=0;x<matrix.length;x++){
for(y=0;y<matrix.length;y++){
System.out.println(matrix[x][y]);
switch(matrix[x][y]){
case 1: batch.draw(numberOne, numberOneR.x, numberOneR.y);
break;
case 2: batch.draw(numberTwo, numberTwoR.x, numberTwoR.y);
break;
case 3: batch.draw(numberThree, numberThreeR.x, numberThreeR.y);
break;
case 4: batch.draw(numberFour, numberFourR.x, numberFourR.y);
break;
case 5: batch.draw(numberFive, numberFiveR.x, numberFiveR.y);
break;
case 6: batch.draw(numberSix, numberSixR.x, numberSixR.y);
break;
case 7: batch.draw(numberSeven, numberSevenR.x, numberSevenR.y);
break;
case 8: batch.draw(numberEight, numberEightR.x, numberEightR.y);
break;
case 9: batch.draw(numberNine, numberNineR.x, numberNineR.y);
break;
case 10: batch.draw(emptySquare, emptySquareR.x, emptySquareR.y);
break;
}
}
}
matrixBuilt=true;
}


batch.end();
}

尽管如果我在batch.end();之前执行batch.draw(emptySquare,emptySquareR.x,emptySquareR.y);没关系,但我不希望这样......这会使代码变得非常广泛......有任何线索吗?谢谢。

编辑:

所以我把它们放在创建方法上:

Gdx.graphics.setContinuousRendering(false);
Gdx.graphics.requestRendering();

如果我不使用鼠标穿过屏幕,渲染只会发生一次,因为如果我这样做,它将重新绘制,而我不希望这种情况发生,我希望图像是静态的,这是我的此时的代码:

 public void render () {

int x,y,squareCounter=0;

batch.begin();
for(x=0;x<matrix.length;x++){
for(y=0;y<matrix.length;y++){
System.out.println(matrix[x][y]);
switch(matrix[x][y]){
case 1:
if(squareCounter==9) {squareCounter=0;startingX=125;startingY-=40;numberOneR.x=startingX;numberOneR.y=startingY; }
batch.draw(numberOne, numberOneR.x, numberOneR.y);
squareCounter++;
startingX+=40;
numberOneR.x=startingX;
break;
case 2:
if(squareCounter==9) {squareCounter=0;startingX=125;startingY-=40;numberTwoR.x=startingX;numberTwoR.y=startingY; }
batch.draw(numberTwo, numberTwoR.x, numberTwoR.y);
squareCounter++;
startingX+=40;
numberTwoR.x=startingX;
break;
case 3:
if(squareCounter==9) {squareCounter=0;startingX=125;startingY-=40;numberThreeR.x=startingX;numberThreeR.y=startingY; }
batch.draw(numberThree, numberThreeR.x, numberThreeR.y);
squareCounter++;
startingX+=40;
numberThreeR.x=startingX;
break;
case 4:
if(squareCounter==9) {squareCounter=0;startingX=125;startingY-=40;numberFourR.x=startingX;numberFourR.y=startingY; }
batch.draw(numberFour, numberFourR.x, numberFourR.y);
squareCounter++;
startingX+=40;
numberFourR.x=startingX;
break;
case 5:
if(squareCounter==9) {squareCounter=0;startingX=125;startingY-=40;numberFiveR.x=startingX;numberFiveR.y=startingY; }
batch.draw(numberFive, numberFiveR.x, numberFiveR.y);
squareCounter++;
startingX+=40;
numberFiveR.x=startingX;
break;
case 6:
if(squareCounter==9) {squareCounter=0;startingX=125;startingY-=40;numberSixR.x=startingX;numberSixR.y=startingY; }
batch.draw(numberSix, numberSixR.x, numberSixR.y);
squareCounter++;
startingX+=40;
numberSixR.x=startingX;
break;
case 7:
if(squareCounter==9) {squareCounter=0;startingX=125;startingY-=40;numberSevenR.x=startingX;numberSevenR.y=startingY; }
batch.draw(numberSeven, numberSevenR.x, numberSevenR.y);
squareCounter++;
startingX+=40;
numberSevenR.x=startingX;
break;
case 8:
if(squareCounter==9) {squareCounter=0;startingX=125;startingY-=40;numberEightR.x=startingX;numberEightR.y=startingY; }
batch.draw(numberEight, numberEightR.x, numberEightR.y);
squareCounter++;
startingX+=40;
numberEightR.x=startingX;
break;
case 9:
if(squareCounter==9) {squareCounter=0;startingX=125;startingY-=40;numberNineR.x=startingX;numberNineR.y=startingY; }
batch.draw(numberNine, numberNineR.x, numberNineR.y);
squareCounter++;
startingX+=40;
numberNineR.x=startingX;
break;
case 10:
if(squareCounter==9) {squareCounter=0;startingX=125;startingY-=40;emptySquareR.x=startingX;emptySquareR.y=startingY; }
batch.draw(emptySquare, emptySquareR.x, emptySquareR.y);
squareCounter++;
startingX+=40;
emptySquareR.x=startingX;
break;
}
}
}
batch.end();
}

最佳答案

这里的问题是,在您的 render 方法中,您调用:

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

首先设置ClearColor,然后用它清除屏幕,例如将整个屏幕设置为这种颜色。
可能的解决方案是:

  1. 删除 2 个调用,这将停止清算。但这也意味着,您绘制的所有内容都会保留在屏幕上,直到您再次调用这些方法。
  2. 每帧绘制所有(可见)事物,就像大多数游戏那样。在您的情况下,删除 if(!matrixBuilt) 条件就足够了。
  3. 将这些内容绘制到 FrameBuffer 一次,然后使用此 FrameBuffer 将内容绘制到屏幕上,如 @Tenfour04 提到的。
  4. 使用非连续渲染,如 @Tenfour04 提到的,这会阻止游戏循环始终调用 render() 方法。相反,您需要调用 Gdx.graphics.requestRendering(); 来调用 render()

我想,在这种情况下,最后一个解决方案是最好的,因为显示的内容或多或少是静态的,并且非连续渲染可以节省电池。
您可以在用户输入等事件上调用 Gdx.graphics.requestRendering();
对于其他游戏,更多的东西在屏幕上移动(例如平台游戏等),在大多数情况下第二种方法是最好的。

关于java - 使用 libgdx 渲染游戏时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27903844/

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