2012-09-14 3 views
7

Google에서 너무 많은 사이트를 검색하여 작동하도록 설정했지만 아무데도이 사이트가있는 것 같습니다. 내 프로그램과 작동하지 않는 경우 ... 달성을 위해 노력하는 것은 플레이어가 공격 당했을 때 첫 번째와 두 번째에 닿는 데 "x"시간이 걸리는 것을 플레이어가 되 찾을 수 있도록하는 것입니다.가공에서 간단한 카운트 다운 만들기

그래서 나는 Boolean "hit" = false을 얻었고 그가 맞았을 때 true으로 바뀝니다. 다시 거짓으로 바뀔 때까지 다시 공격 할 수 없다는 의미입니다.

그래서 내 프로그램에서 "x"분량의 "타이머"를 설정하려고합니다. IF hit = true 타이머가 "x"분에 도달하면 히트가 false로 다시 전환됩니다 .

누구든지 아이디어가 있습니까?

감사합니다.

+0

... 어떤 언어를? – FrankieTheKneeMan

+0

나는 처리를 사용하고있다. (Java) – D34thSt4lker

답변

14

간단한 옵션은 millis()을 사용하여 수동으로 시간을 추적하는 것입니다.

두 변수를 사용합니다 :

  1. 일이 경과 된 시간을 저장하는
  2. 한 당신이 있는지 확인 할 것 무승부() 메소드에서

필요로하는 대기/지연 시간을 저장하는 현재 시간 (밀리 초)과 이전에 저장된 시간 간의 차이가 지연보다 크거나 같아야합니다.

:

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

설정/필요에 따라 재사용 할 수있는 클래스로이를 포장하도록 선택할 수 있습니다. 이것은 기본 접근 방식이며 자바 스크립트에서 setInterval()을 사용하고 있지만 Android 및 JavaScript 버전에서도 작동해야합니다.

FrankieTheKneeMan이 제안한대로 Java 유틸리티를 사용하고 싶다면 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>

+0

고마워, 실제로 다른 누군가가 이미 클래스를 사용하여 처리 할 수있는 방법을 찾았다. 귀하의 의견을 보내 주셔서 감사합니다. 많이 고맙습니다. – D34thSt4lker

+0

걱정할 필요가 없으므로 기꺼이 처리해 드리겠습니다. –