Scroll 在Libgdx中的一个阶段中滚动似乎很慢

Scroll 在Libgdx中的一个阶段中滚动似乎很慢,scroll,libgdx,stage,Scroll,Libgdx,Stage,因此,我尝试以实现滚动作为起点 我正在桌面上运行测试 我在stage中添加了一个actiongestureelistener,这样我就可以调用pan(…)函数了。它可以工作,但当我尝试滚动时,它看起来非常滞后,有点像“振动” 我错过了什么 以下是我到目前为止的情况: public class Store extends ScreenAdapter { SpriteBatch spritebatch; Stage stage; Texture texture;

因此,我尝试以实现滚动作为起点

我正在桌面上运行测试

我在stage中添加了一个
actiongestureelistener
,这样我就可以调用
pan(…)函数了。它可以工作,但当我尝试滚动时,它看起来非常滞后,有点像“振动”

我错过了什么

以下是我到目前为止的情况:

public class Store extends ScreenAdapter {


    SpriteBatch spritebatch;
    Stage stage;
    Texture texture;
    Image image;
    Group background, foreground;


    @Override
    public void show() {
        spritebatch = new SpriteBatch();
        stage = new Stage();
        background = new Group();
        foreground = new Group();
        texture = new Texture("badlogic.jpg");
        image = new Image(texture);

        //Building background/foreground.
        background.addActor(image);

        //Adding to stage.
        stage.addActor(background);
        stage.addActor(foreground);

        stage.addListener(new ActorGestureListener() {
            // TODO Auto-generated method stub

            @Override
                public void pan(InputEvent event, float x, float y, float deltaX, float deltaY) {

                stage.getCamera().translate(-deltaX, -deltaY, 0);
                stage.getCamera().update();
                //System.out.println(x + ", " + y + ", " + "      " + deltaX + ", " + deltaY);
            }
        });
        Gdx.input.setInputProcessor(stage);
    }


    @Override
    public void render(float delta){

        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        spritebatch.setProjectionMatrix(stage.getCamera().combined);

        spritebatch.begin();
        stage.draw();
        spritebatch.end();
    }

    @Override
    public void resize(int width, int height){
        System.out.println("resize() executed.");
        //stage.getViewport().update(width, height, true);
    }


    @Override
    public void dispose(){
        stage.dispose();
        texture.dispose();
        spritebatch.dispose();
    }
}

在show方法中,不应为字段对象重新分配内存。这将使垃圾收集器发疯,并可能导致一些延迟,如您所见。将所有新语句从show()移动到类构造函数中,或直接移动到字段成员中,如下所示:

public class Store extends ScreenAdapter {


SpriteBatch spritebatch = new SpriteBatch();
Stage stage = new Stage();
// ETC...  
这样,这些对象的内存在对象创建时只分配一次

**编辑:我只是看了看,发现show方法不应该像我假设的那样被重复调用,所以上面的方法可能不起作用。

我最终通过编辑
translate
调用摆脱了“振动”

stage.getCamera().translate(-deltaX/2,-deltaY/2,0)

它可以是3或任何你想要的增量,而不是除以2。我觉得这是一个解决方案,就像把一个创可贴放在一个问题上,而不是实际解决问题。但是它有一种工作的外观,所以我会选择它