简体   繁体   中英

Timer before executing action again

In a switch statement I have a case, which represents a logout button in a game. The problem is if a player repetitively clicks it over and over it executes the c.logout() method again and again causing huge amounts of lag in my game. I'm wanting to add a timer before the player can click the button again. I'm pretty new to threads and timers so I'd really appreciate some help on this. Especially if you could explain it. Thanks a lot.

This is my code

    case 9154: // Logout Button
        c.logout();
        break;

Solution thanks to Ryan

Just making a simpile boolean to keep track of the log in state.

case 9154: // Logout Button
    if (loggedIn) {
        loggedIn = false;
        c.logout();
    }
    break;

You could keep a variable of their status:

case 9154: // Logout Button
    if (loggedIn) {
        loggedIn = false;
        c.logout();
    }
    break;

Then set loggedIn back to true when they log in.

may you do not have to start a new thread, just use a lock to prevent calling logout twice.

One of the first google results as an example:

http://tutorials.jenkov.com/java-concurrency/locks.html

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