我试图用libgdx表示一些图像。为此,我有矩形和纹理变量。问题是当我让代码以这种方式运行时,它只绘制一次图像,然后立即清除它。如果我画它们,我希望图像留下来。
@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);
,那么该怎么办?没关系,但我不想那样......它会让代码变得非常宽大......任何线索?感谢。
编辑:
所以我把它们放在create方法上:
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();
}
答案 0 :(得分:1)
问题在于,在您的render
方法中,您可以致电:
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
首先设置ClearColor
,然后用它清除屏幕,例如将整个屏幕设置为此颜色
可能的解决方案是:
if(!matrixBuilt)
条件就足够了。FrameBuffer
一次,然后使用此FrameBuffer
将内容绘制到屏幕上,如@ Tenfour04所述。render()
方法。相反,您需要致电Gdx.graphics.requestRendering();
以获取render()
来电。我想,在这种情况下,最后的解决方案将是最好的,因为显示的内容或多或少是静态的,非连续渲染可以节省电池。
您可以在用户输入等类似事件上调用Gdx.graphics.requestRendering();
对于其他游戏,屏幕上有更多东西在移动(比如平台游戏等),第二种方法在大多数情况下是最好的。