Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java LibGDX android翻译spritebatch有时向前跳1-2像素(滞后)?_Java_Android_Libgdx - Fatal编程技术网

Java LibGDX android翻译spritebatch有时向前跳1-2像素(滞后)?

Java LibGDX android翻译spritebatch有时向前跳1-2像素(滞后)?,java,android,libgdx,Java,Android,Libgdx,我正在android上使用LibGDX制作我的第一个游戏 我有一个背景是288*511,现在在我的游戏中,我需要重复这个背景,然后在上面翻译。我用Slick2D为桌面制作了这个游戏,也遇到了同样的问题,只是比我的LG G2手机(4.0.4)稍微低了一点,是有办法解决这个问题还是我做错了什么 问题是,当它进行平移时,移动很精细,有时向前跳1-2个像素,或者停留0.5秒左右 这是我的班级: public class Background { private class RepeatedBa

我正在android上使用LibGDX制作我的第一个游戏

我有一个背景是288*511,现在在我的游戏中,我需要重复这个背景,然后在上面翻译。我用Slick2D为桌面制作了这个游戏,也遇到了同样的问题,只是比我的LG G2手机(4.0.4)稍微低了一点,是有办法解决这个问题还是我做错了什么

问题是,当它进行平移时,移动很精细,有时向前跳1-2个像素,或者停留0.5秒左右

这是我的班级:

public class Background {

    private class RepeatedBackground implements GameObject {

        private int x;
        private int y = 0;
        Sprite background;

        public RepeatedBackground(int x, Sprite s) {
            this.x = x;
            this.background = s;
        }

        @Override
        public void render() {

        }

        public float getPreferedWidth() {
            int w = Gdx.graphics.getWidth();
            return (float) (w / 1.15);
        }

        public float getPreferedHeight() {
            int h = Gdx.graphics.getHeight();
            return (float) (h / 1.15);      
        }   

        @Override
        public int getX() {
            return this.x;
        }

        @Override
        public int getY() {
            return this.y;
        }

        public Sprite getSprite() {
            return this.background;
        }

    }

    private int tra = 0;
    private long traTime = 0;
    private List<RepeatedBackground> backgrounds = new ArrayList<RepeatedBackground>();
    private Level level;
    private SpriteBatch backgroundRenderer;

    public Background(Level level) {
        this.level = level;
        this.backgroundRenderer = new SpriteBatch();
    }

    public void generateBackgrounds() {
        int x = 0;
        Sprite background = this.level.getFactory().createBackgroundSprite();
        for (int i = 0; i < LevelConfig.MAX_BACKGROUND_REPEATS; i++) {
            this.backgrounds.add(new RepeatedBackground(x, background));
            x += background.getWidth();
        }
    }

    public void render() {
        this.backgroundRenderer.getTransformMatrix().translate(-6, 0, 0);
        this.backgroundRenderer.begin();
        this.backgroundRenderer.setProjectionMatrix(this.level.getInstance().getCamera().combined);
        Iterator<RepeatedBackground> itr = this.backgrounds.iterator();
        while (itr.hasNext()) {
            RepeatedBackground b = itr.next();
            Sprite s = b.getSprite();
            if (b.getX() - b.getPreferedHeight() < this.level.getInstance().getCamera().viewportWidth) { // this doesn't work properly, but it doesn't load all backgorunds at once but still lags..
                this.backgroundRenderer.draw(s, b.getX(), b.getY(), b.getPreferedWidth(), b.getPreferedHeight());
            }
        }
        this.backgroundRenderer.end();
    }
}

你可以在这里使用视差背景类

它不取决于图像或相机的大小。它只是不断地转换你的背景

资料来源:

    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    camera = new OrthographicCamera(w,h);
    camera.setToOrtho(false);

    batch = new SpriteBatch();
    Texture.setEnforcePotImages(false);