创建处理一个简单的倒计时(Create a simple countdown in processi

2019-06-18 04:35发布

我已经搜索在谷歌这么多的网站,试图得到这个工作,但似乎没有人有这样的任何地方,如果他们这样做,它只是不符合我的计划工作......我想实现是有一个球员反冲,当玩家被击中,他的击中了第一次和第二次之间的时间“X”的金额。

所以我有一个Boolean "hit" = false ,当他被击中,它变成true 。 这意味着他可以直到它再次更改为false不会被再次袭来。

所以我想建立一个功能在我的程序设置“计时器”秒“X”量IF hit = true ,一旦该计时器点击“X”的秒数,严重的将得到切换回假。

有人有主意吗?

谢谢!!

Answer 1:

一个简单的选择是使用手动跟踪时间米利斯() 。

你可以使用两个变量:

  1. 一个存储经过时间
  2. 一个用于存储你所需要的等待/延迟时间

在draw()方法,你会检查当前时间之间的差(以毫秒)和先前存储的时间大于(或等于)的延迟。

如果是这样,这将在你的提示做任何的延迟给出更新存储时间:

int time;
int wait = 1000;

void setup(){
  time = millis();//store the current time
}
void draw(){
  //check the difference between now and the previously stored time is greater than the wait interval
  if(millis() - time >= wait){
    println("tick");//if it is, do something
    time = millis();//also update the stored time
  }
}

这里有一个细微变化更新屏幕上的“针”:

int time;
int wait = 1000;

boolean tick;

void setup(){
  time = millis();//store the current time
  smooth();
  strokeWeight(3);
}
void draw(){
  //check the difference between now and the previously stored time is greater than the wait interval
  if(millis() - time >= wait){
    tick = !tick;//if it is, do something
    time = millis();//also update the stored time
  }
  //draw a visual cue
  background(255);
  line(50,10,tick ? 10 : 90,90);
}

根据您的设置/需求,你可以选择包装这样的成可重复使用的类。 这是一个基本的方法,并应与Android和JavaScript版本的工作,以及(虽然在JavaScript中你得到的setInterval())。

如果您有兴趣使用Java的实用程序,如FrankieTheKneeMan建议,有一个TimerTask的类可用,我敢肯定有很多的资源/例子在那里。

您可以波纹管运行演示:

 var time; var wait = 1000; var tick = false; function setup(){ time = millis();//store the current time smooth(); strokeWeight(3); } function draw(){ //check the difference between now and the previously stored time is greater than the wait interval if(millis() - time >= wait){ tick = !tick;//if it is, do something time = millis();//also update the stored time } //draw a visual cue background(255); line(50,10,tick ? 10 : 90,90); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.4/p5.min.js"></script> 

更新有许多方法可以使计时器。 下面是一个使用一个线程和调用的名字草图定义的运作版本。 这不是一个简单的倒计时。 直线上升millis()如上所述是很简单的,只是不是很灵活:

Timer timer;

void setup(){
  noStroke();

  //textSize(12);
  timer = new Timer(this,"onTimerTick","onTimerComplete");
  // start a timer for 10 seconds (10 * 1000 ms) with a tick every second (1000 ms) 
  timer.reset(10 * 1000,1000);
}

void draw(){
  background(0);
  drawTimer();
  //rect(0,0,timer.progress * width,height);
  //blendMode(DIFFERENCE);
  text("'1' = reset"+
     "\n'2' = cancel"+
     "\n'3' = pause"+
     "\n'4' = resume"+
     "\n"+(int)(timer.progress * 100)+"%",10,15);
}

void drawTimer(){
  pushStyle();
  noFill();
  stroke(255);
  strokeWeight(3);
  ellipse(450, 54,90,90);
  fill(192,0,0);
  noStroke();
  pushMatrix();
  translate(50,50);
  rotate(radians(-90));
  arc(0, 0, 90, 90, 0, timer.progress * TWO_PI, PIE);
  popMatrix();
  popStyle();
}

void keyPressed(){
  if(key == '1'){
    timer.reset(3000,10);
  }
  if(key == '2'){
    timer.cancel();
  }
  if(key == '3'){
    timer.pause();
  }
  if(key == '4'){
    timer.resume();
  }
}

public void onTimerTick(){
  println("tick",(int)(timer.progress * 100),"%");
}

public void onTimerComplete(){
  println("complete");
}

import java.lang.reflect.Method;
// utility timer class
class Timer implements Runnable{
  // is the timer still ticking or on hold ?
  boolean isPaused = false;
  // is the thread still running ?
  boolean isRunning = true;

  // how close are we to completion (0.0 = 0 %, 1.0 = 100%)
  float progress = 0.0;
  // a reference to the time in ms since the start of the timer or reset
  long now;
  // default duration
  long duration = 10000;
  // default tick interval
  long tickInterval = 1000;
  // time at pause
  long pauseTime;

  // reference to the main sketch
  PApplet parent;
  // function to call on each tick
  Method onTick;
  // function to call when timer has completed
  Method onComplete;

  Timer(PApplet parent,String onTickFunctionName,String onCompleteFunctionName){
    this.parent = parent;
    // try to store a reference to the tick function based on its name
    try{
      onTick = parent.getClass().getMethod(onTickFunctionName);
    }catch(Exception e){
      e.printStackTrace();
    }

    // try to store a reference to the complete function based on its name
    try{
      onComplete = parent.getClass().getMethod(onCompleteFunctionName);
    }catch(Exception e){
      e.printStackTrace();
    }
    // auto-pause
    isPaused = true;
    // get millis since the start of the program
    now = System.currentTimeMillis();
    // start the thread (processes run())
    new Thread(this).start();
  }

  // start a new stop watch with new settings
  void reset(long newDuration,long newInterval){
    duration = newDuration;
    tickInterval = newInterval;
    now = System.currentTimeMillis();
    progress = 0;
    isPaused = false;
    println("resetting for ",newDuration,"ticking every",newInterval);
  } 

  // cancel an existing timer
  void cancel(){
    isPaused = true;
    progress = 0.0;
  }

  // stop this thread
  void stop(){
    isRunning = false;
  }

  void pause(){
    isPaused = true;
    pauseTime = (System.currentTimeMillis() - now); 
  }
  void resume(){
    now = System.currentTimeMillis() - pauseTime;
    isPaused = false;
  }

  public void run(){
    while(isRunning){

      try{
          //sleep per tick interval
          Thread.sleep(tickInterval);
          // if we're still going
          if(!isPaused){
            // get the current millis
            final long millis = System.currentTimeMillis();
            // update how far we're into this duration
            progress = ((millis - now) / (float)duration);
            // call the tick function
            if(onTick != null){
              try{
                onTick.invoke(parent);
              }catch(Exception e){
                e.printStackTrace();
              }
            }
            // if we've made, pause the timer and call on complete
            if(progress >= 1.0){
              isPaused = true;
              // call on complete
              if(onComplete != null){
              try{
                  onComplete.invoke(parent);
                }catch(Exception e){
                  e.printStackTrace();
                }
              }
            }
          }
        }catch(InterruptedException e){
          println(e.getMessage());
        }
      }
    }

}

此外,您可以使用Java的TimerTask的 类 。



文章来源: Create a simple countdown in processing