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

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

问题描述

我在谷歌上搜索了很多网站来尝试让它工作,但似乎没有人在任何地方都有这个,如果他们这样做,它只是不适合我的程序......我想要实现的是让玩家后坐力,当玩家被击中时,他在第一次和第二次被击中之间有x"的时间间隔.

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.

所以我有一个Boolean "hit" = false,当他被击中时,它会变成true.这意味着他不能再次被击中,直到再次将其更改为 false.

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.

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

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.

有人有什么想法吗?

谢谢!!

推荐答案

一个简单的选择是使用 millis().

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

您将使用两个变量:

  1. 一个存储经过的时间
  2. 一个存储您需要的等待/延迟时间

在 draw() 方法中,您将检查当前时间(以毫秒为单位)与先前存储的时间之间的差异是否大于(或等于)延迟.

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

根据您的设置/需要,您可以选择将这样的内容包装到一个可以重用的类中.这是一种基本方法,应该也适用于 Android 和 JavaScript 版本(尽管在 JavaScript 中您有 setInterval()).

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()).

如果您对使用 Java 的实用程序感兴趣,正如 FrankieTheKneeMan 建议的那样,有一个 TimerTask 类可用,我相信那里有很多资源/示例.

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>

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

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