简体   繁体   中英

One Time Loop in java?

I have tried making a loop in java that only runs once,

package com.notelek.programs.flat;

import java.awt.event.KeyEvent;

import com.notelek.programs.flat.input.Controller;

public class Game {
    public static int time;
    public Controller controls;

    public Game(){
        controls = new Controller();
    }

    public void tick(Boolean[] key){
        time++;
        Boolean forward = key[KeyEvent.VK_W];
        Boolean back = key[KeyEvent.VK_S];
        Boolean left = key[KeyEvent.VK_A];
        Boolean right = key[KeyEvent.VK_D];
        Boolean turnLeft = key[KeyEvent.VK_LEFT];
        Boolean turnRight = key[KeyEvent.VK_RIGHT];
        for(Boolean i = true; i == true; i = false){
            forward = true;
            back = true;
            left = true;
            right = true;
            turnLeft = true;
            turnRight = true;
        }
        controls.tick(forward,back,left,right,turnLeft,turnRight);
    }    
}

That is my code, I need to set the forward backward left right turnleft turnright variables all to true, and then back to false quickly (within a second) Does anyone know a simple way of doing this?

How quickly are we talking, here? If you want to switch it within a second (much, much faster than a second), just do:

forward = true;
back = true;
left = true;
right = true;
turnLeft = true;
turnRight = true;
forward = false;
back = false;
left = false;
right = false;
turnLeft = false;
turnRight = false;

If you're looking for a toggle that you can manipulate, I would make your own method:

public void toggle() {
    forward = !forward;
    back = !back;
    // and so on
}

... then invoke it after a certain amount of milliseconds have passed.

In the spirit of answering the question asked, though, if you really, really, really want to use a loop that only runs once, for some reason... here's the syntax for it:

for(int i = 0; i < 1; i++) {
    // do stuff
}

If you really want a loop, use this:

do {
    your code here
} while (false);

IMO, this is a bit silly...

Why do you want to use a loop? If all you want to do is make something wait a second, why not use a wait command:

Thread.sleep(int milliseconds)

You can just execute the block of code you want, wait a prescribed number of milliseconds and then execute another block of code:

int milliseconds = 1000;

forward = true;
back = true;
left = true;
right = true;
turnLeft = true;
turnRight = true;
Thread.sleep(milliseconds)
forward = false;
back = false;
left = false;
right = false;
turnLeft = false;
turnRight = false;

Possibly try something like this:

 for(int i = 0; i < 2; i++) { Boolean state = (i == 0); forwards = state; back = state; left = state; right = state; turnLeft = state; turnRight = state; // Optional sleep int milliseconds = 100; Thread.sleep(milliseconds); } 

The loop will go round once, set all to true (i == 0) then on the next repetion will set all to fasle , then quit.

The Thread.sleep() is optional depending if you need the values to stay what they are for a short period.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM