繁体   English   中英

为什么我的libgdx游戏会逐渐变慢?

[英]Why does my libgdx game progressively slow down?

我有一款游戏可以永久生成尸体,直到游戏结束。 出于某种原因,游戏一开始会很好玩而且很流畅,但是随着游戏的进行,它会开始变慢并变得不稳定。 然后,在您死亡并重新启动后,游戏会重复此过程。 我会全力以赴。 这是推动玩家身体的因素:

//in main game class
private Vector2 movement = new Vector2();
sSpeed = 200000;
switch(button)
{
        case Buttons.LEFT:
            movement.y = sSpeed * 1.2f;
            movement.x = sSpeed * 1.5f;
            table.clear();
        }
        return false;
}

//in Level Generator class
public LevelGenerator(BodyDef bDef, float topEdge, float bottomEdge, float minGap, float maxGap, float w, float h, Sprite s, World world) {
    this.bDef = bDef;
    this.topEdge = topEdge;
    this.bottomEdge = bottomEdge;
    this.minGap = minGap;
    this.maxGap = maxGap;
    width = w;
    height = h;
    this.s = s;
    this.world = world;
}

public void generate(float rightEdge) {
    if(x + MathUtils.random(minGap, maxGap) > rightEdge) {
        return;
    }
    x = rightEdge;
    float y = MathUtils.random(topEdge - height * 2f, bottomEdge + height * 2f);

    bDef = new BodyDef();
    bDef.type = BodyType.DynamicBody;

    PolygonShape ast = new PolygonShape();
    ast.setAsBox(width, height, new Vector2(x + width, y + height), 0);

    item = world.createBody(bDef);
    Fixture fix = item.createFixture(ast, 0);
}

//in main game class
generator.generate(camera.position.x + camera.viewportWidth / 2 + 10);
generator = new LevelGenerator(ballD3, 120, -125, 58, 63, 12.5f, 12.5f, aSprite1 , world);

可悲的是,避免这种内存泄漏的最佳方法是自己跟踪对象。

我通过创建一个名为bodies的数组来解决这个问题,该数组包含所有主体。 然后运行for循环,如下所示(对不起,JavaScript :)

_.each(bodies,function(body){
  var pos = body.GetPosition();
  if (pos.y > 500) world.destroyBody(body);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM