Libgdx 下降圈
Libgdx falling circles
我正在开发 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 个圆圈并以极快的速度放下它们。
我需要能够不断地产生积分并减慢掉落速度。
非常感谢!
您应该考虑使用 while 循环,它会在游戏仍然存在时运行。删除 for i less than 10 循环并实现 while 循环,它将在线程的生命周期内继续创建球。至于减慢球的创建速度 - 调用 thread.sleep() 函数并实现与此类似的方法:
public void run()
{
while (alive)
{
createNewCircle();
updateGame();
repaint();
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
有关使用线程编程的更多信息 - 查看 this
我正在开发 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 个圆圈并以极快的速度放下它们。 我需要能够不断地产生积分并减慢掉落速度。
非常感谢!
您应该考虑使用 while 循环,它会在游戏仍然存在时运行。删除 for i less than 10 循环并实现 while 循环,它将在线程的生命周期内继续创建球。至于减慢球的创建速度 - 调用 thread.sleep() 函数并实现与此类似的方法:
public void run()
{
while (alive)
{
createNewCircle();
updateGame();
repaint();
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
有关使用线程编程的更多信息 - 查看 this