我正在做一个libgdx游戏,有一个特定的问题。我试图不断地生成随机坐标(屏幕上方),并为每个坐标绘制一个圆。然后加上它们的y坐标,这样它们就会有下降的效果。一旦一个圆圈离开屏幕,它就会被删除。
在这里,我创建了二维列表。它包含屏幕中的随机坐标数组。
List<List<Integer>> lst = new ArrayList<List<Integer>>();
public void makePoints() {
Random generator = new Random();
for (int i = 0; i < 10; i++) {
List<Integer> lst1 = new ArrayList<Integer>();
int randX = randInt(10,Gdx.graphics.getWidth()-10);
int randY = randInt(Gdx.graphics.getHeight()/5,Gdx.graphics.getHeight()-10);
lst1.add(randX);
lst1.add(randY);
lst.add(lst1);
}
}在游戏循环之前,我调用该函数。
然后在游戏圈里,我做这个。(请记住,这是一遍又一遍)
//Draw a circle for each coordinate in the arraylist
for (int i=0; i<lst.size();i++){
shapeRenderer.circle((lst.get(i)).get(0), (lst.get(i)).get(1), 30);
}
//Add 1 to the y value of each coordinate
for (int i=0; i<lst.size();i++){
lst.get(i).set(1, i + 1);
}目前,它画了10个圆圈,并迅速下降。我需要能够不断地生成点数并减缓下降速度。
非常感谢!
发布于 2015-05-16 23:10:31
您应该考虑使用which循环,它在游戏还活着的时候运行。删除for i< 10循环并实现while循环,它将继续为线程的生存期创建球。至于减慢球的创建速度--调用thread.sleep()函数并实现类似于此的方法:
public void run()
{
while (alive)
{
createNewCircle();
updateGame();
repaint();
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}有关使用线程编程的更多信息,请检查这 out。
https://stackoverflow.com/questions/30281608
复制相似问题