我正在尝试创建一个具有精灵的游戏,并且每秒都会生成另一个精灵,我尝试使用它作为基础:https://github.com/libgdx/libgdx/wiki/A-simple-game https://github.com/libgdx/libgdx/wiki/A-simple-game然而,当新的生成时,它会摧毁旧的,并且它们开始生成得越来越快。
这是相关代码:
自己的创建方法:
Array<Rectangle> chickens;
chickens = new Array<Rectangle>();
spawnChicken();
新的生成鸡方法:
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
与维基百科非常相似:
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();
}
Put
Rectangle chicken = new Rectangle();
开始时spawnChicken()
method.
Reasons
- 如果你保留
chicken
会员,每次都会更换spawnChicken()
方法被调用。
- 此外,每次您尝试生成小鸡时,都会将相同的对象添加到数组中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)