首页 > 解决方案 > 当我创建一个舞台时,它不允许我在 libgdx 中绘制精灵

问题描述

我在 libgdx 中制作了一个游戏,其中一只乌龟必须收集海星,但我使用的是演员,所以我必须使用舞台。创建阶段后,我无法在屏幕上绘制精灵,并且游戏崩溃

编辑:我忘了把 batch.end() 放在最后,但它仍然没有绘制精灵

这是我的代码

@Override
public void render()
{
    for (int i=0; i<5; i++)
    {
        if (Gdx.input.isTouched(i)) 
        {
            Vector3 touchPos = new Vector3(Gdx.input.getX(i), Gdx.input.getY(i), 0);
            camera.unproject(touchPos);
            Rectangle touch = new Rectangle(touchPos.x-16, touchPos.y-16, 32, 32);

            if (touch.overlaps(leftButton))
            {
                moveX-=Gdx.graphics.getDeltaTime()*80;
                System.out.println("left touched");
            }
            if (touch.overlaps(rightButton))
            {
                moveX+=Gdx.graphics.getDeltaTime()*80;
                System.out.println("right touched");
            }
            if (touch.overlaps(jumpButton))
            {
                moveY+=Gdx.graphics.getDeltaTime()*80;
                System.out.println("up touched");
            }
        }
    }
    
    // check user input
    mainStage.act(1/60f);
    // check win condition: turtle must be overlapping starfish
    if (turtle.overlaps(starfish))
    {
        starfish.remove();
        winMessage.setVisible(true);
    }
    
    // clear screen
    Gdx.gl.glClearColor(0,0,0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    // draw graphics
    mainStage.draw();
    
    batch.setProjectionMatrix(camera.combined);
    batch.begin();

    leftSpriteButton.draw(batch);
    rightSpriteButton.draw(batch);
    jumpSpriteButton.draw(batch);
    
    
}

标签: javalibgdxspritebatch-processing

解决方案


推荐阅读