gpt4 book ai didi

java - 如何在导致运行时的 libgdx 中编写计时器

转载 作者:行者123 更新时间:2023-11-30 10:53:41 25 4
gpt4 key购买 nike

我最近用这个 QuizDialog 界面创建了一个 libgdx 游戏并且我添加了 CountDownTimer,这是我从这里的不同教程中遵循的,但是当我执行游戏时,它告诉

Exception in thread "LWJGL Application" java.lang.RuntimeException: Stub!
at android.os.CountDownTimer.<init>(CountDownTimer.java:4)
at com.boontaran.games.SICT.LevelQuizDialog$2$1.<init>(LevelQuizDialog.java:184)
at com.boontaran.games.SICT.LevelQuizDialog$2.clicked(LevelQuizDialog.java:184)
at com.badlogic.gdx.scenes.scene2d.utils.ClickListener.touchUp(ClickListener.java:89)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:57)
at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:346)
at com.boontaran.games.StageGame.touchUp(StageGame.java:300)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:325)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:199)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

到目前为止,这是我的代码,

package com.boontaran.games.SICT;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;
import android.os.CountDownTimer;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.boontaran.MessageEvent;
public class LevelQuizDialog extends Group {
public static final int ON_CLOSE = 1;
public static final int ON_OPEN = 2;
public static final int LevelCompleted2 = 1;
public static final int LevelFailed2 = 2;

//private Timer timer;





public LevelQuizDialog( int score) {

// final int score2 = score;

NinePatch patch = new NinePatch(SICT.atlas.findRegion("dialog_bg"),60,60,60,60);
Image bg = new Image(patch);
bg.setSize(980, 670);
setSize(bg.getWidth() , bg.getHeight());
addActor(bg);


final Image title = new Image(SICT.atlas.findRegion("quiz"));
addActor(title);
title.setX((getWidth() - title.getWidth())/2);
title.setY(getHeight() - title.getHeight() - 100);

final Image title2 = new Image(SICT.atlas.findRegion("level_completed"));
//addActor(title2);
title2.setX((getWidth() - title2.getWidth())/2);
title2.setY(getHeight() - title2.getHeight() - 100);

final Image title3 = new Image(SICT.atlas.findRegion("level_failed"));
// addActor(title3);
title3.setX((getWidth() - title3.getWidth())/2);
title3.setY(getHeight() - title3.getHeight() - 100);

Image image2 = new Image(SICT.atlas.findRegion("border"));
image2.setSize(300,120);
addActor(image2);
image2.setPosition(390,300);
//score label
LabelStyle style = new LabelStyle();
style.font = SICT.font1;
style.fontColor = new Color(0x624601ff);


Label label = new Label("Score :", style);
addActor(label);
label.setPosition(590, 600);
LabelStyle style2 = new LabelStyle();
style2.font = SICT.font2;
style2.fontColor = new Color(0x624601ff);

//the score
Label scoreLabel = new Label(String.valueOf(score) , style2);
addActor(scoreLabel);
scoreLabel.setPosition(800, 600);


//Label array1 = new Label("", array1); and the checking

Random random = new Random();
int array = random.nextInt(10) + 1;
int array2 = random.nextInt(10) +1;
final int answer = random.nextInt(20) +5;
final int checker = array + array2;



Label StringArray = new Label(String.valueOf(array), style2);
addActor(StringArray);
StringArray.setPosition(417,325);

Label StringArray2 = new Label(String.valueOf(array2), style2);
addActor(StringArray2);
StringArray2.setPosition(520, 325);

Label StringArray3 = new Label(String.valueOf(answer), style2);
addActor(StringArray3);
StringArray3.setPosition(620, 325);

//timer
//setPosition(20, 600);



final ImageButton CheckBtn = new ImageButton(
new TextureRegionDrawable(SICT.atlas.findRegion("check1")),
new TextureRegionDrawable(SICT.atlas.findRegion("check2")));

addActor(CheckBtn);
//CheckBtn.setSize(120, 120);
CheckBtn.setX(400);
CheckBtn.setY(70);

final ImageButton WrongBtn = new ImageButton(
new TextureRegionDrawable(SICT.atlas.findRegion("wrong1")),
new TextureRegionDrawable(SICT.atlas.findRegion("wrong2")));

addActor(WrongBtn);
//WrongBtn.setSize(120, 120);
WrongBtn.setX(550);
WrongBtn.setY(70);




CheckBtn.addListener(new ClickListener(){


@Override
public void clicked(InputEvent event, float x, float y) {
WrongBtn.clearListeners();
if(checker == answer )
{
removeActor(title);
addActor(title2);
}

if(checker != answer)
{
removeActor(title);
addActor(title3);
}




}
});

//fire event on button click
WrongBtn.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
CheckBtn.clearListeners();
if(checker != answer )
{
removeActor(title);
addActor(title2);

}

if(checker == answer)
{
removeActor(title);
addActor(title3);
}

new CountDownTimer(4000, 4000) {
public void onTick(long millisUntilFinished) {

}

public void onFinish() {
fire(new MessageEvent(ON_CLOSE));
}
}.start();

}

});

}

最佳答案

您不需要任何花哨的东西。您可以从 LibGDX 中的任何位置访问帧时间,它称为增量或 Gdx.graphics.getRawDeltaTime() 因为 Gdx.graphics.getDeltaTime() 提供了平滑版本。因此,您需要做的就是拥有一个按此增量递增的计数器和一个固定值(例如 10 秒)。

既然你可能想倒数,我们应该减少计数器:

float time, counter = 10f;

public void update(float delta)
{
counter -= Gdx.graphics.getRawDeltaTime();
//if you have delta passed as a overload you can do
counter -= delta; //This is smoothed but should not matter for a quiz game

if (counter <= 3)
{
//Play annoying sound to make you stress even more
}

if (counter <= 0)
{
//Time is up!

//You can just reset the counter by setting it equal to time again.
//But there will almost always be left over (counter will be less then 0)
//So if this is important (probably not for a quiz game)
counter = time + -counter; //Add the time this round took too long.
}
}

这就是它的全部。如果您想以秒为单位绘制时间,您应该将计数器四舍五入。

关于java - 如何在导致运行时的 libgdx 中编写计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33916368/

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