gpt4 book ai didi

java - LibGDX 在更新方法期间执行一次操作

转载 作者:行者123 更新时间:2023-11-30 07:40:58 25 4
gpt4 key购买 nike

我对 Java 和 LibGDX 有点陌生,我正在开发一个基于点的游戏。我面临的一个问题是更新方法不断运行我想要及时运行的东西。在我的更新方法中,我有代码在获得分数时增加分数,并在玩家失败时显示失败状态。无需赘述,这是我的伪代码:

 protected void update(float dt){

for(Thing x : a list) {

x.update(dt);

//continue
if (you complete the objective of the game) {
score++;
//do other stuff
}


//lost
if (you fail to complete the objective){
make lose state come up
}
}

//I want something like this to run:
if(score >=0 && score <=10){
do something ONCE(ie. print to console once)
}
if(score >= 11 && score <=15){
do something once
}
if(ect...){
do something else once
}
.
.
.

这里的问题是,如果满足 IF 条件,我注意到 IF block 会很快执行多次(即多次打印到控制台)。我已将依赖于分数的代码的详细信息包含在一个单独的类中,我只想能够从此更新方法调用该方法,并根据分数条件运行一次(即,如果分数满足另一个 IF 语句)

最佳答案

我也遇到了同样的问题,但是我使用以下方法解决了它。在 Libgdx 中,更新方法的运行时间为 1/60 秒。这就是该方法连续渲染并且 if 条件将执行多次的原因。要解决此问题,只需在 create 方法中声明一个整数变量,例如 count

public static int count;
public static void create()
{
count =0;
// do other stuffs
}
protected void update(float dt){
// just increment this count value in your update method
for(Thing x : a list) {
count++
x.update(dt);
//continue
if (you complete the objective of the game) {
score++;
//do other stuff
}
//lost
if (you fail to complete the objective){
make lose state come up
}
}
// and make a small condition to run the if condition only once by using the declared variable "count".
if(count%60==0){
// here the condition only executes when the count%60==0.that is only once in 1/60 frames.
if(score >=0 && score <=10){
}
if(score >= 11 && score <=15){
}
if(ect...){
}
}

。。。这就是我解决同样问题的方法。

关于java - LibGDX 在更新方法期间执行一次操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34702282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com