渲染的图像未显示Java Libgdx [英] Rended image not showing Java Libgdx

查看:94
本文介绍了渲染的图像未显示Java Libgdx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试渲染图像,但是当我以桌面启动器的形式运行游戏时,无法看到该图像,但是我认为该图像正在渲染,因为它与另一个精灵重叠以提供输出.

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();

我没有错误.

相机

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);

推荐答案

我在render方法中看到了很多问题:

I see a number of issues in the render method:

  • 更新相机并多次触摸. render方法中的前三行在后面重复.它们只能被调用一次.如果您不移动相机,则没有理由对其进行更新,但是通常,如果您正在移动相机,则在处理可能导致其移动的任何触摸逻辑之后,便希望对其进行移动和更新.
  • 在绘制小鸡之前不清除屏幕.您必须先调用Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);,然后才能使用Sprite批处理.
  • 不将所有的鸡都混在一起.对于每只占用大量CPU的小鸡,您都调用batch.beginbatch.end,如果多于几只鸡,它们将很快成为游戏性能的瓶颈.您应该将batch.begin移至while循环之前,将batch.end移至while循环之后.
  • 混合更新游戏状态和绘图的顺序.在绘制这些对象之前,应处理所有触摸逻辑和对象位置的更新. (这不会引起您的问题,但是会导致用户输入滞后一帧.)
  • 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 and batch.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 move batch.begin to before your while loop and batch.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.)

最有可能的原因是,在更新相机并调用glClear之前,使用精灵批次绘制了这些小鸡,因此渲染不正确.

Most likely, the chickens weren't rendering correctly due to drawing them with the sprite batch before updating the camera and calling glClear.

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

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();

    }

除此之外,您的switch语句还有一个错误... break应该在if语句之外调用.现在不是问题,但是稍后添加到switch语句中时将成为问题.我没有在上面纠正这个问题.

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.

而且仅供参考,您不必在浮点数前继续输入(float).只需在其后键入f.

And FYI, you don't have to keep typing (float) before your float numbers. Just type an f after them.

这篇关于渲染的图像未显示Java Libgdx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆