我的游戏几乎完全正常工作,但是,我想为我的游戏创建一个无尽的关卡,比如Flappy Bird,这是添加敌人collisionList.add(new collisionEntity(world, collsionTexture, 6, 1));
的代码,这是添加楼层的代码groundList.add(new groundEntity(world, groundTexture, overgroundTexture,overground2Texture ,10, 10 ,18));
我希望这些代码行像循环一样重复,而不是一遍又一遍地添加它们,例如
`groundList.add(new groundEntity(world, groundTexture, overgroundTexture,overground2Texture ,10, 10 ,13));`
groundList.add(new groundEntity(world, groundTexture, overgroundTexture,overground2Texture ,25, 10 ,20));
groundList.add(new groundEntity(world, groundTexture, overgroundTexture,overground2Texture ,10, 7 ,12));
groundList.add(new groundEntity(world, groundTexture, overgroundTexture,overground2Texture ,10, 10 ,16));
我该怎么做?我的游戏结果如下https://www.youtube.com/watch?v=ql-Mr81fZ0U, Go to 2:23-2:31 minutes to see what I mean
我只是想让它添加障碍并一遍又一遍地重复,这与地面代码相同。
答案 0 :(得分:0)
这完全取决于你想要什么以及你希望事物如何排列或者随机化。如果您了解每个帧都调用update()
方法,则可以在其中包含语句,以检查何时“生成”某些内容。如果您不再需要它们,那么从列表中删除项目也很重要,这样它们就可以被垃圾收集器收集以释放内存。如果你使用一次性物品,你也应该处理它们。
一种方法可以是跟踪......是的,时间的计时器。当它达到某个时间时你会产生一些东西(添加到你的列表中)。相机移过对象后,您可以将其从列表中删除。
float timer = 0;
float spawnTime = 4;
private void spawnEntity()
{
//Increment timer by the duration since the previous frame
timer += Gdx.graphics.getRawDeltaTime();
//Compare to spawntime
if (timer >= spawnTime)
{
//Spawn your object
groundList.add(new groundEntity(world, groundTexture, overgroundTexture,overground2Texture ,25, 10 ,20));
//But you will probably want to spawn something on the right, just outside of your screen view.
//This is the right side of your vp in the world. Depending how you draw you can add some more to it.
float spawnX = camera.position.x + camera.viewportWidth / 2;
//Then use this to spawn your object, since you hardcoded stuff I have no idea where to put it.
//Now reset timer
timer-= spawnTime;
//And perhaps randomize the spawnTime? (between 2 and 4 seconds)
spawnTime = random.nextFloat() * 2 + 2;
}
}
只需调用每个更新循环。目前,它将使用您的硬编码添加。 groundList.add(new groundEntity(world, groundTexture, overgroundTexture,overground2Texture ,25, 10 ,20));
因此,如果25,10或20负责定位,它将覆盖实体。将float spawnX
放在groundlist.add(...)
前面,并使用spawnX作为X定位。如果你有一个移动的摄像头,它将从右侧进入。如果您还没有移动相机,请将这两行添加到您的更新中以进行测试。
camera.position.x += 1;
camera.update();