我正在尝试渲染图像,但是当我将游戏作为桌面启动器运行时,无法看到图像,但我相信图像正在渲染,因为它与另一个精灵重叠以提供输出。
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);
守则:
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);
答案 0 :(得分:1)
我在渲染方法中看到了许多问题:
render
方法中的前三行会再次向下重复。他们只应该被召唤一次。如果你没有移动你的相机,没有理由更新它,但通常如果你移动它,你会想要在处理任何可能导致它移动的触摸逻辑后移动并更新它。Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
。batch.begin
和batch.end
,并迅速成为游戏性能的瓶颈。您应该在while循环之前将batch.begin
移至batch.end
之前,并在while循环之后移至glClear
。 最有可能的情况是,在更新相机并致电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();
}
之前,鸡只因为使用精灵批量绘制而无法正确渲染。
这大致是我如何重新排序。但如果这是我的项目,我会做更多的事情来分离逻辑和渲染。例如,我会使用Sprite对象来代表鸡而不仅仅是矩形。然后我会在render方法的开头适当地移动所有的sprite,然后在所有sprite中再做一次循环,最后将它们提交给SpriteBatch。这将使您的代码更容易出错。
break
除此之外,你的switch语句有一个bug ... (float)
应该在if语句之外调用。现在不是问题,但是稍后当您添加到switch语句时。我没有纠正上面的内容。
而且仅供参考,您不必在浮动号码前输入f
。只需在它们后面输入{{1}}即可。