每次碰撞迭代libGDX的渐进滞后

时间:2018-06-23 23:14:54

标签: memory-leaks libgdx collision-detection aide

您好,我在业余时间使用AIDE和libGDX开发游戏。尽管AIDE缺少libGDX API的某些方法,但我不得不创建一些替代方法来弥补这些方法的不足。 所以我的问题是,每次发生碰撞时,应用程序都会变得越来越迟钝。没有新的纹理被绘制且不会消失。只是一个糟糕的实现,意在将您从上述碰撞块中挤出。它运行良好,直到发生一些冲突。我的想法是,每次冲突它都会创建一个新变量,并将其存储到内存中,从而导致泄漏。但是我真的无法确定是否是这种情况。哦,我想补充一点,我没有电脑,只有我的手机。这是我的运动课

move.java

public class move
{
    float posX;
    float posY;
    float touchedOnScreenX;
    float touchedOnScreenY;
    float centerOfScreenX;
    float centerOfScreenY;
    float posXSetter;
    float posYSetter;
    float Speed = 150;
    float mapBoundsWidth;
    float mapBoundsHeight;
    boolean upperBounds;
    boolean lowerBounds;
    boolean rightBounds;
    boolean leftBounds;
    boolean tileCollition;



    public move() {

    }

    public void renderMove(float delta) {


        if(Gdx.input.isTouched()) {
            screenAndTouchInfo();
            checkBoundsBoolean();
            checkForCollision();
        }
    }

    //slows game down
    private void checkForCollision()
    {
        if (upperBounds == false)
        {
            if (lowerBounds == false)
            {
                if (rightBounds == false)
                {
                    if (leftBounds == false)
                    {
                        if (tileCollition == false)
                        {
                            movement();
                        }
                        else
                        {
                            collitionSide();
                        }
                    }
                    else
                    {
                        posX++;
                    }
                }
                else
                {
                    posX--;
                }
            }   
            else
            {
                posY++;
            }
        }
        else
        {
            posY --;
        }
    }

    private void movement()
    {
        posYSetter = posY;
        posXSetter = posX;
        if (touchedOnScreenX < centerOfScreenX)
        {
            posX -= Gdx.graphics.getDeltaTime() * Speed;

        }
        else
        {
            posX += Gdx.graphics.getDeltaTime() * Speed;

        }
        if (touchedOnScreenY < centerOfScreenY)
        {
            posY -= Gdx.graphics.getDeltaTime() * Speed;

        }
        else
        {
            posY += Gdx.graphics.getDeltaTime() * Speed;

        }
        if (touchedOnScreenY < centerOfScreenY + 64 && touchedOnScreenY > centerOfScreenY - 64)
        {
            posY = posYSetter;

        }
        if (touchedOnScreenX < centerOfScreenX + 64 && touchedOnScreenX > centerOfScreenX - 64)
        {
            posX = posXSetter;

        }
    }

    //buggy and slows game down. Can push you into tile if input is the opposite of the side
    public void collitionSide() {
        if (tileCollition == true){
            if (touchedOnScreenX < centerOfScreenX)
            {
                posX = posX +10;
            }
            else
            {
                posX = posX - 10;

            }
            if (touchedOnScreenY < centerOfScreenY)
            {
                posY = posY +10;
            }
            else
            {
                posY = posY -10;
            }
        }
    }

    private void screenAndTouchInfo()
    {
        touchedOnScreenX = Gdx.input.getX();
        touchedOnScreenY = (Gdx.graphics.getHeight() - Gdx.input.getY());
        centerOfScreenX = Gdx.graphics.getWidth() / 2;
        centerOfScreenY = Gdx.graphics.getHeight() / 2;
    }
    //slows game down
    private void checkBoundsBoolean()
    {
        if (posX > mapBoundsWidth)
        {
            rightBounds = true;
        }
        else {
            rightBounds = false;
        }
        if (posY > mapBoundsHeight)
        {
            upperBounds = true;
        }
        else {
            upperBounds = false;
        }
        if (posX < mapBoundsWidth - mapBoundsWidth)
        {
            leftBounds = true;
        }
        else {
            leftBounds = false;
        }
        if (posY < mapBoundsHeight - mapBoundsHeight)
        {
            lowerBounds = true;
        }
        else {
            lowerBounds = false;
        }
    }

    public void setTileCollision(boolean tileCollision) {
        this.tileCollition = tileCollision;
    }

    public float getPosX() {
        return posX;
    }

    public float getPosY() {
        return posY;
    }

    public float getTouchedOnScreenX() {
        return touchedOnScreenX;
    }

    public float getTouchedOnScreenY() {
        return touchedOnScreenY;
    }

    public float getCenterOfScreenX() {
        return centerOfScreenX;
    }

    public float getCenterOfScreenY() {
        return centerOfScreenY;
    }

    public void setMapboundsWidth(float width) {
        this.mapBoundsWidth = width;
    }

    public void setMapboundsHeight(float height) {
        this.mapBoundsHeight = height;
    }
}

我知道有很多代码需要梳理,如果不能始终清楚发生了什么,我感到抱歉。我将其重构到更容易理解的地方。哦,如果有人能告诉我为什么AIDE缺少一些libGDX的方法也很好,最著名的方法是Cell.setTile()。我知道它在libGDX的API中,可以在其文档中找到。但是,当我实现它时,它将引发类Cell错误的未知方法。我已经研究了如何使用该方法,但无济于事。必须创建int [] []地图和一个for循环以使用另一个Texture []绘制地图。有用。大声笑。谢谢那些甚至花时间看我冗长的双重问题的人。希望有人能告诉我为什么这种延迟是由上述碰撞造成的。

1 个答案:

答案 0 :(得分:1)

我建议将冲突代码移到单独的线程中。这样可以显着提高性能:

Executor executor = Executors.newSingleThreadExecutor();
  executor.execute(new Runnable() {
     @Override
     public void run() {
        // Physics loop goes here
     }
});

在处理屏幕时,请确保关闭执行程序。