处理 P5 - millis() 的计时器问题

发布于 2024-12-18 10:43:12 字数 1861 浏览 9 评论 0原文

我正在 Processing 中制作一个小游戏,类似于那些吉他英雄风格的游戏,我正在尝试做两件事

  1. :游戏加载,停止时间移动
  2. 在游戏期间,允许暂停功能

现在,我知道我无法停止时间,因为 millis() 返回自应用程序启动以来的毫秒数,因此我的计时器需要为 millis ()- millis() 在开始处等于 0,因此当用户按下 START 时,他们显然可以从开始处开始。游戏在开始时读取一个文件,类似于字幕文件,其中包含要播放的音符以及它应该出现在屏幕上的时间(以毫秒为单位)。

我的问题是,当我暂停游戏时,计时器继续计时,当我取消暂停游戏时,由于我的逻辑,所有笔记都会“聚在一起”,正如您从我的代码中看到的那样。

有人可以建议一种比我正在使用的算法更好的算法吗?已经很晚了,我已经日夜不停地在做这件事了。我认为问题出在下面的 for() 上:

public void draw()
{
    if (gameInProgress)
    {
        currentTimerValue = millis(); // Update the timer with the current milliseconds
        // Check to see if the note times falls between the current time, or since the last loop (difficult to match exact millisecond)
        for(int i=0 ; i<songNotes.length ; i++)
        {
             if( songNotes[i].getStartTime() > previousTimerValue && songNotes[i].getStartTime() <=currentTimerValue)
                notes.add(songNotes[i]);
        }

        noStroke();
        textFont(f,18);
        drawButtons();  //Draws coloured buttons relating to Button presses on the controller
        drawHighScoreBox(); // Draws high score box up top right
        drawLines();  // Draws the strings
        moveNotes();  // Moves the notes across from right to left
        //Now set the cutoff for oldest note to display
        previousTimerValue=currentTimerValue;  //Used everytime on the following loop
    }
    else
    {
        drawMenu(); // Draw the Main/Pause menu
    }
}

注意:当用户按下暂停按钮(例如“P”)时,布尔值 gameInProgress 在下面设置,并且 songNotes 是我自己编写的 Note 类型的对象数组。它有 2 个成员变量,noteToBePlayedtimeToBePlayed。方法getStartTime()返回timeToBePlayed,它是一个毫秒值。

任何帮助表示赞赏。 谢谢

I am making a little game in Processing which is similar to those Guitar Hero style games and I am trying to do 2 things:

  1. When the game loads, stop the time from moving
  2. During the game, allow for Pause functionality

Now, I know I cant stop the time since the millis() returns the milliseconds since the application launched, so my timer will need to be millis() - millis() at the start to equal zero, so when the user presses START, they can obviously start at the start. The game reads a file at the start, similar to a subtitles file, that has the note to be played and the time in milliseconds that it should appear on screen.

My problem is, when I pause the game, the timer keeps going and when I unpause the game, all the notes get "bunched up" due to my logic, as you'll see from my code.

Can someone suggest a better algorithm than the one I'm using? Its late and I've been working on this all day and night. I think the problem is with the for() below:

public void draw()
{
    if (gameInProgress)
    {
        currentTimerValue = millis(); // Update the timer with the current milliseconds
        // Check to see if the note times falls between the current time, or since the last loop (difficult to match exact millisecond)
        for(int i=0 ; i<songNotes.length ; i++)
        {
             if( songNotes[i].getStartTime() > previousTimerValue && songNotes[i].getStartTime() <=currentTimerValue)
                notes.add(songNotes[i]);
        }

        noStroke();
        textFont(f,18);
        drawButtons();  //Draws coloured buttons relating to Button presses on the controller
        drawHighScoreBox(); // Draws high score box up top right
        drawLines();  // Draws the strings
        moveNotes();  // Moves the notes across from right to left
        //Now set the cutoff for oldest note to display
        previousTimerValue=currentTimerValue;  //Used everytime on the following loop
    }
    else
    {
        drawMenu(); // Draw the Main/Pause menu
    }
}

NOTE: The boolean gameInProgress is set below when the users presses the pause button, eg "P", and songNotes is an array of objects of type Note that I wrote myself. It has 2 member variables, noteToBePlayed and timeToBePlayed. The method getStartTime()returns timeToBePlayed which is a millisecond value.

Any help is appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戈亓 2024-12-25 10:43:13

当您暂停时使用另一个整数来存储时间并使用它来抵消游戏计时器怎么样?

因此,在“gameInProgress”模式下,您更新 currentTimerValuepreviousTimerValue,在“paused/menu”模式下,您更新 pausedTimerValue,您可以使用它来偏移“currentTimerValue”。我希望这是有道理的,它听起来更复杂,这就是我的意思:

boolean gameInProgress = true;
int currentTimerValue,previousTimerValue,pausedTimerValue;
void setup(){

}
void draw(){
  if(gameInProgress){
    currentTimerValue = millis()-pausedTimerValue;
    println("currentTimerValue: " + currentTimerValue + " previousTimerValue: " + previousTimerValue);  
    previousTimerValue=currentTimerValue;
  }else{
    pausedTimerValue = millis()-currentTimerValue;
  }
}
void mousePressed(){
  gameInProgress = !gameInProgress;
  println("paused: " + (gameInProgress ? "NO" : "YES"));
}

单击草图以切换模式并在控制台中查看时间。您会注意到切换之间只损失了几毫秒,这是可以接受的。

How about having another integer to store time when you pause and use that to offset the game timer ?

So, in 'gameInProgress' mode you update currentTimerValue and previousTimerValue and in 'paused/menu' mode you update a pausedTimerValue, which you use to offset the 'currentTimerValue'. I hope this makes sense, it sounds more complicated in words, here's what I mean:

boolean gameInProgress = true;
int currentTimerValue,previousTimerValue,pausedTimerValue;
void setup(){

}
void draw(){
  if(gameInProgress){
    currentTimerValue = millis()-pausedTimerValue;
    println("currentTimerValue: " + currentTimerValue + " previousTimerValue: " + previousTimerValue);  
    previousTimerValue=currentTimerValue;
  }else{
    pausedTimerValue = millis()-currentTimerValue;
  }
}
void mousePressed(){
  gameInProgress = !gameInProgress;
  println("paused: " + (gameInProgress ? "NO" : "YES"));
}

Click the sketch to toggle modes and look in the console for times. You'll notice that you only loose a few millis between toggles, which is acceptable.

酷遇一生 2024-12-25 10:43:13

不使用系统计时器,而是使用具有暂停功能的特殊计时器类。我相信自己实现这样的课程并不难。我知道java有Timer类,但不幸的是它不支持暂停功能。

Use not system timer but special timer class with pause functionality. I'm sure it is not hard to implement such class by yourself. I know that java has Timer class but unfortunately it not support pause functionality.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文