在处理中创建简单的倒计时 [英] Create a simple countdown in processing

查看:63
本文介绍了在处理中创建简单的倒计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Google上搜索了很多网站,试图使它正常工作,但是似乎没有人可以在任何地方使用它,如果这样做的话,则与我的程序不兼容...我要实现的目标是让玩家后坐,当玩家被击中时,他在第一次击中与第二次击中之间有 x的时间。



所以我有一个布尔值 hit = false ,当他被命中时,它会更改为 true



所以我试图在我的程序中设置一个函数来设置计时器在 x秒内 IF hit = true ,并且一旦计时器达到 x秒,命中将切换回false。



有人有什么想法吗?



谢谢!

解决方案

一个简单的选项是使用



此外,您还可以使用Java的 TimerTask


I have searched up so many sites on Google to try and get this to work but NO ONE seems to have this anywhere , and if they do it's just NOT working with my program... What I am trying to achieve is to have a player recoil that when the player gets hit, he has a "x" amount of time between getting hit the first time and the second time.

So I have a Boolean "hit" = false and when he gets hit, it changes to true. Which means he can't get hit again until it's changed to false again.

So I'm trying to set up a function in my program to set a "timer" for "x" amount of seconds IF hit = true and once that timer hits "x" amount of seconds, hit will get switched back to false.

Anyone have any ideas?

Thanks!!

解决方案

A simple option is to manually keep track of time using millis().

You would use two variables:

  1. one to store elapsed time
  2. one to store the wait/delay time you need

In the draw() method you would check if the difference between the current time (in millis.) and the previously stored time is greater(or equal) to the delay.

If so, this would the you're cue to do whatever for the delay given and update the stored time:

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
  }
}

Here's a slight variation the updates a 'needle' on screen:

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);
}

Depending on your setup/needs, you may choose to wrap something like this into a class that can be reused. This is a basic approach and should work with the Android and JavaScript versions as well (although in javascript you've got setInterval()).

If you're interested in using Java's utilities, as FrankieTheKneeMan suggested, there is a TimerTask class available and I'm sure there are plenty of resources/examples out there.

You can run a demo bellow:

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>

Update There many ways to make timers. Here's a version that uses a Thread and calls a functioned defined in the sketch by name. This is not a simple countdown. Straight up millis() as explained above is simple enough, just not very flexible:

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());
        }
      }
    }

}

Additionally you can use Java's TimerTask class.

这篇关于在处理中创建简单的倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆