问题
I am trying to render an image, however when i run the game as a a desktop launcher, the image can not be seen, however i believe the image is being rendered as it overlaps with another sprite to give an output.
if (chicken.overlaps(farmer)){
System.out.print("GameOver");
gameOver();
I get no errors.
Camera
OrthographicCamera camera;
camera = new OrthographicCamera();
camera.setToOrtho(false, 1920, 1080);
camera.update();
inputUpdate(touch, camera);
batch.setProjectionMatrix(camera.combined);

The Code:
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;
}
Farmer
game.batch.draw(FarmerAsset.currentFrame, farmerX, farmerY, (float)85, (float)85, (float)170, (float)170, (float)1, (float)1, (float)angleDegrees + 270);
回答1:
I see a number of issues in the render method:
- Updating camera and touch multiple times redundantly. Those first three lines in your
render
method are repeated further down. They should only be called once. If you aren't moving your camera, there's no reason to update it, but typically if you were moving it, you would want to move and update it after dealing with any touch logic that might cause it to move. - Not clearing the screen before drawing the chickens. You must call
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
before you use your sprite batch. - Not batching all the chickens together. You call
batch.begin
andbatch.end
for every chicken which is CPU intensive and will quickly become the bottle neck of your game's performance if you have more than a few chickens. You should movebatch.begin
to before your while loop andbatch.end
to right after your while loop. - Mixing up order of updating game state and drawing. You should handle all the touch logic and updating of object positions before drawing those objects. (This isn't causing your issue, but it is causing a one-frame lag from user input.)
Most likely, the chickens weren't rendering correctly due to drawing them with the sprite batch before updating the camera and calling glClear
.
This is roughly how I would reorder it. But if this were my project, I would do more to separate the logic and the rendering. For example, I would use Sprite objects to represent the chickens instead of just Rectangles. Then I would move all the sprites appropriately at the beginning of the render method, and then just do one more loop through all the sprites to submit them to SpriteBatch at the end. This would make your code less bug-prone.
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();
}
Aside from that, your switch statement has a bug...break
should be called outside the if statement. Not an issue now, but will be later when you add to the switch statement. I didn't correct this above.
And FYI, you don't have to keep typing (float)
before your float numbers. Just type an f
after them.
来源:https://stackoverflow.com/questions/24784624/rended-image-not-showing-java-libgdx