LibGDX游戏在渲染时滞后 [英] LibGDX game lags when rendering

查看:94
本文介绍了LibGDX游戏在渲染时滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个障碍物数组( Vector2 对象的 Array ),它们从屏幕的右侧向左侧移动,因此在每次调用 render时()函数我将它们的x位置递减 400 * deltaTime 值,然后使用 SpriteBatch 绘制它们.问题是,当他们向左移动时,有时会出现一些障碍,因此看起来它向右移动了一会儿或停止了,我不知道为什么.因此,他们向左移动,一些障碍物停了一段时间,然后又正确地移动,然后另一个障碍物滞后了一段时间,这种情况持续了下来.同时,屏幕上大约有4个障碍物,我回收了不再可见的障碍物.有人可以解释一下为什么会这样吗?我也在libGDX示例推力直升机"中也注意到了这个错误"(您也可以在《 LibGDX游戏开发要点》 中找到该书-我也为这个问题写了一个主题:LibGDX示例游戏Thrust Copter lags ).感谢您的回复!:)

I have an array of obstacles (Array of Vector2 objects) which move from the right side of the screen to the left so in every call of render() function I decrement their x-positions by 400*deltaTime value and then I am drawing them using SpriteBatch. The problem is when they are moving left sometimes some obstacle lags so it looks like it moves to the right for a while or it stops and I don't know why. So they are moving left and some obstacle stops for a while then they are moving correctly again and then another obstacle lags for a while and this continues. In the same time there are approximatelly 4 obstacles on the screen and I recycle obstacles which aren't visible anymore. Can somebody explain me why this is happening? I noticed this "bug" in the libGDX example Thrust Copter too (which you can find in the book LibGDX Game Development Essentials - I wrote a topic for this problem too: LibGDX example game Thrust Copter lags). Thanks for any reply! :)

public class TapToJumpGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture obstacle;
    OrthographicCamera camera;
    Array<Vector2> obstacles;
    Array<Vector2> usedObstacles;
    double obsTime;
    Vector2 lastObstacle;

    @Override
    public void create () {
        batch = new SpriteBatch();
        usedObstacles = new Array<Vector2>();
        obstacle = new Texture("obstacle.png");
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);   
        resetGame();
    }

    private void addObstacle()
    {
        if(lastObstacle != null) if((810 - (lastObstacle.x + obstacle.getWidth())) < 200) return;
        Vector2 obs; 
        if(usedObstacles.size > 0) obs = usedObstacles.pop();
        else obs = new Vector2(); 
        obs.set(810, 10);
        obstacles.add(obs);
        lastObstacle = obs;
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        updateScene();
        drawScene();
    }

    private void resetGame() {
        obstacles = new Array<Vector2>();
        obsTime = 1;
        lastObstacle = null;
    }

    private void drawScene() {
        camera.update();
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        for(Vector2 obs:obstacles)
        {
            batch.draw(obstacle, obs.x, obs.y);
        }
        batch.end();
    }

    private void updateScene() {

        float deltaTime = Gdx.graphics.getDeltaTime();

        obsTime -= deltaTime;
        if(obsTime < 0)
        {
            obsTime = MathUtils.random()*1+.5;
            addObstacle();
        }

        for(Vector2 obs:obstacles)
        {
            obs.x -= 400*deltaTime;
            if(obs.x + obstacle.getWidth() < -10) {
                usedObstacles.add(obs);
                obstacles.removeValue(obs, true);
            }
        }
    }
}

推荐答案

从当前迭代通过的Array中删除对象时,类似的滞后尖峰有时会出现.

Looks like those lag spikes appear sometime when removing a object from an Array which is currently iterated through.

将要删除的内容存储在某个位置,并在迭代后将其删除,如下所示:

Store what you want to remove somewhere and remove it after iteration like this:

for (int i = 0; i < obstacles.size; i++) {
         Vector2 obs = obstacles.get(i);
         obs.x -= 400 * deltaTime;
         if (obs.x + obstacle.getWidth() < 250) {
            usedObstacles.add(obs);
            obstaclesToRemove.add(obs);
         }
      }

      for (int i = 0; i < obstaclesToRemove.size; i++) {
         obstacles.removeValue(obstaclesToRemove.get(i), true);

      }
      obstaclesToRemove.clear();

这篇关于LibGDX游戏在渲染时滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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