控制和创建多个精灵阵列的Java Libgdx [英] Controlling and creating multiple sprites array Java Libgdx
本文介绍了控制和创建多个精灵阵列的Java Libgdx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个游戏,有精灵和每秒另一个是催生,我试图用这个作为基础:的 https://github.com/libgdx/libgdx/wiki/A-simple-game 然而,当新的派生于它破坏了旧的,他们开始产卵越来越快。 下面是相应和code:
ownCreate方式:
阵列<矩形>鸡;
鸡=新的Array<矩形>();
spawnChicken();
新的产卵鸡的方法:
私人无效spawnChicken(){
INT选择;
选择= MathUtils.random(0,3);
开关(选择){
情况下0:
chicken.x = MathUtils.random(0,1920-85);
chicken.y = 0;
打破;
情况1:
chicken.x = MathUtils.random(0,1920-85);
chicken.y = 1080至85年;
打破;
案例2:
chicken.x = 0;
chicken.y = MathUtils.random(0,1080-66);
打破;
案例3:
chicken.x = 1920年至1985年;
chicken.y = MathUtils.random(0,1080-66);
打破;
}
chicken.height = 66;
chicken.width = 85;
chickens.add(鸡);
runningChickens ++;
lastSpawnTime = TimeUtils.nanoTime();
Render方法:
如果(TimeUtils.nanoTime() - lastSpawnTime> 10亿)
spawnChicken();
迭代器<矩形> ITER = chickens.iterator();
而(iter.hasNext()){
矩形鸡= iter.next();
//运动
非常相似,维基:
私人阵列<矩形>雨滴;
专用长lastDropTime;私人无效spawnRaindrop(){
矩形雨滴=新的Rectangle();
raindrop.x = MathUtils.random(0,800-64);
raindrop.y = 480;
raindrop.width = 64;
raindrop.height = 64;
raindrops.add(雨滴);
lastDropTime = TimeUtils.nanoTime();
}
雨滴=新的Array<矩形>();
spawnRaindrop();
如果(TimeUtils.nanoTime() - lastDropTime>十亿)spawnRaindrop();
迭代器<矩形> ITER = raindrops.iterator();
而(iter.hasNext()){
矩形雨滴= iter.next();
raindrop.y - = 200 * Gdx.graphics.getDeltaTime();
如果(raindrop.y + 64℃的)iter.remove();
}
解决方案
把
矩形鸡=新的Rectangle();
在 spawnChicken()
方法的开始。
原因
- 如果您保持
鸡
一员,它会在每次spawnChicken()
方法被调用时更换。 - 此外,相同的对象每次尝试产卵鸡时添加到阵列中。
I am trying to create a game which has sprites and every second another is spawned, i tried using this as a base: https://github.com/libgdx/libgdx/wiki/A-simple-game However when the new one spawns in it destroys the old one and they start to spawn faster and faster. Here is the relevent code:
ownCreate method:
Array<Rectangle> chickens;
chickens = new Array<Rectangle>();
spawnChicken();
New Spawn Chicken method:
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();
Render method:
if(TimeUtils.nanoTime() - lastSpawnTime > 1000000000)
spawnChicken();
Iterator<Rectangle> iter = chickens.iterator();
while(iter.hasNext()){
Rectangle chicken = iter.next();
//movement
Very similar to the 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();
}
解决方案
Put
Rectangle chicken = new Rectangle();
at the start of spawnChicken()
method.
Reasons
- If you keep
chicken
a member, it will be replaced every timespawnChicken()
method is called. - Also, the same object is added to the array every time you try to spawn a chicken.
这篇关于控制和创建多个精灵阵列的Java Libgdx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文