我正在尝试创建一个具有精灵的游戏,每一秒都会产生,我尝试将其作为基础:https://github.com/libgdx/libgdx/wiki/A-simple-game 然而,当新的产生它时,它会破坏旧的,并且它们开始产生越来越快的速度。 以下是相关代码:
ownCreate方法:
Array<Rectangle> chickens;
chickens = new Array<Rectangle>();
spawnChicken();
New Spawn Chicken方法:
private void spawnChicken() {
int choice;
choice = MathUtils.random(0, 3);
switch (choice){
case 0:
chicken.x = MathUtils.random(0,1920-85);
chicken.y = 0;
break;
case 1:
chicken.x = MathUtils.random(0,1920-85);
chicken.y = 1080-85;
break;
case 2:
chicken.x = 0;
chicken.y = MathUtils.random(0,1080-66);
break;
case 3:
chicken.x = 1920-85;
chicken.y = MathUtils.random(0,1080-66);
break;
}
chicken.height = 66;
chicken.width = 85;
chickens.add(chicken);
runningChickens ++;
lastSpawnTime = TimeUtils.nanoTime();
渲染方法:
if(TimeUtils.nanoTime() - lastSpawnTime > 1000000000)
spawnChicken();
Iterator<Rectangle> iter = chickens.iterator();
while(iter.hasNext()){
Rectangle chicken = iter.next();
//movement
与wiki非常相似:
private Array<Rectangle> raindrops;
private long lastDropTime; private void spawnRaindrop() {
Rectangle raindrop = new Rectangle();
raindrop.x = MathUtils.random(0, 800-64);
raindrop.y = 480;
raindrop.width = 64;
raindrop.height = 64;
raindrops.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
}
raindrops = new Array<Rectangle>();
spawnRaindrop();
if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();
Iterator<Rectangle> iter = raindrops.iterator();
while(iter.hasNext()) {
Rectangle raindrop = iter.next();
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
if(raindrop.y + 64 < 0) iter.remove();
}
答案 0 :(得分:0)
把
Rectangle chicken = new Rectangle();
在spawnChicken()
方法的开头。
<强>原因强>
chicken
成员,则每次调用spawnChicken()
方法时都会替换该成员。