【问题标题】:How to add a delay in a fire(new MessageEvent)如何在火灾中添加延迟(新 MessageEvent)
【发布时间】:2015-11-17 08:09:03
【问题描述】:

我对 Java 和 Libgdx 真的很陌生

我创建了一个名为 LevelQuizDialog 的界面,其中的按钮将触发一个事件,我希望在触发该事件之前有 3 秒的延迟。

代码如下:

    //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);
            }
            // this is the delay I had a problem with... 
             float delay = 3; // seconds

             Timer.schedule(new Task(){
                 @Override
                 public void run() {
                     fire(new MessageEvent(ON_CLOSE));
                 }
             }, delay);

        }
    });
}

我在 com.badlogic.gdx.utils.Timer 中学习了很多教程 但我有很多错误..

我在 Timer.schedule(new Task() 中遇到错误

说 Timer 不适用于参数

感谢任何可以提供帮助的人。

【问题讨论】:

    标签: java android eclipse libgdx


    【解决方案1】:

    一个可移植的 LibGDX 解决方案可能是使用带有消息 api 的 gdx-ai 库,允许您使用专门的事件总线发布延迟消息:

    https://github.com/libgdx/gdx-ai/wiki/Message-Handling#dispatching-a-message

      messageDispatcher.dispatchMessage(
            delay,               // Immediate message if <= 0; delayed otherwise
            sender,              // It can be null
            recipient,           // It can be null, see the "Multiple Recipients" section below
            messageType,         // Any user-defined int code
            extraInfo,           // Optional data accompanying the message; it can be null
            needsReturnReceipt); // Whether the sender needs the return receipt or not
    

    【讨论】:

      【解决方案2】:

      这可能会有所帮助

      new Handler().postDelayed(new Runnable() {
      
          @Override
          public void run() {
              fire(new MessageEvent(ON_CLOSE));       
          }
      }, delayMillis);
      

      【讨论】:

      • 您可能使用了错误的处理程序类。 android.os.Handler().postDelayed(runnable, int) 试试这个。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 2018-01-19
      • 2021-12-06
      • 2021-05-30
      相关资源
      最近更新 更多