在libgdx上运行app时保持图像

时间:2015-08-08 09:28:51

标签: android libgdx

我是LibGdx的新手,但是要学习。这就是我的问题。 我有心脏图标代表用户的生活。它启动了并排放置的3个心脏图标。我的问题是,我无法在App运行时显示这些图标。它只会在游戏开始时突然显示并突然消失。我怎样才能在运行应用程序时保持游戏中的生活图标(心脏图标)。 这是我的代码。

游戏结构

GameAreaScreen(final MyGdxGame game){

        camera = new OrthographicCamera();
        camHeight = Gdx.graphics.getHeight();//400
        camWidh = Gdx.graphics.getWidth();//800
        cany=camHeight;
        camera.setToOrtho(false, camWidh, camHeight);
        iconSize=64f;
        canlar= new Array<Can>();
        canlar.add(new Can());canlar.add(new Can());canlar.add(new Can());
        _game= game;
        create(); 
    }

和我的渲染

@Override
    public void render(float delta) {

        //render
        GL20 gl = Gdx.graphics.getGL20();
         gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
        camera.update();

        // tell the SpriteBatch to render in the
        // coordinate system specified by the camera.
        _game.batch.setProjectionMatrix(camera.combined);
        _game.batch.begin();
        _game.font.setColor(Color.BLACK);
        _game.batch.draw(pointsbgImage,camWidh-iconSize, camHeight-iconSize);
        _game.font.draw(_game.batch,String.valueOf(dropsGathered), camWidh-iconSize/2f,camHeight-iconSize/2f );
        _game.batch.end();
        _game.batch.begin();
        for (Can eleman : canlar) {
               _game.batch.draw(eleman.canImageTexture, canx+iconSize,cany-iconSize);
                drawRect2(canx+iconSize, cany-iconSize, iconSize/2f, iconSize/2f, 1, eleman.canImageTexture);
                canx+=iconSize;

            }
        _game.batch.end();
   .......
   .......

}

和我的Can类代表生命

public class Can  {

    public Texture canImageTexture;
    public Can(){
        canImageTexture=(new Texture(Gdx.files.internal("data/heart.png")));
    }
}

1 个答案:

答案 0 :(得分:1)

canx+=iconSize;

是问题所在。它增加到无穷大,将图标移到屏幕右侧。将canx重置为render()方法中的某个起始值