【发布时间】:2014-03-16 06:34:21
【问题描述】:
我有一个游戏,它会在游戏结束之前永远生成身体。出于某种原因,游戏一开始会很流畅,但随着您继续玩游戏,它会开始变慢并变得波涛汹涌。然后,在您死亡并重新启动后,游戏会重复此过程。我尽我所能。以下是推动玩家身体的因素:
//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);
【问题讨论】:
-
这里的代码不会大大降低任何速度。我的猜测是你在某个地方有一个 O(n) 或 O(n^2) 算法,它在不断增长。但是您需要显示更多代码,它不在这里。
-
你也销毁尸体吗?
-
@noone 我不会像你现在看到的那样摧毁尸体。我添加了代码
-
@GabeSechan 我添加了代码供您现在查看。它向我展示了生成多个身体,然后将它们放入游戏世界
-
你需要对你不再使用的所有身体做
world.destroyBody(body),不在屏幕上或其他什么。否则 Box2D 将永远模拟这些物体,添加的物体越多,它就会变得越慢。
标签: java android performance libgdx box2d