我不知道为什么,但我似乎看不到 LibGDX 中的随机数生成是如何工作的,也没有我能找到的使用它的好例子。只是想知道如何随机选择一个数字 1-3 然后 System.out 它。奖励 - 您如何每秒随机生成一个新数字?
最佳答案
您可以使用标准 Java 来做到这一点。
Random random = new Random();
int oneTwoThree = random.nextInt(3) + 1;
这将生成一个随机整数(0、1 或 2),然后加上 1,得到 1、2 或 3。
如果你想每秒切换一次,那么你需要在你的render(float)
方法中跟踪时间
private float countDown;
private int randomNumber;
public void render(float deltaTime) {
countDown -= deltaTime;
if (countDown <= 0) {
Random random = new Random();
randomNumber= random.nextInt(3) + 1;
countDown += 1000; // add one second
}
}
关于android - 在 LibGDX 中生成一个随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21903114/