简体   繁体   English

再次执行动作之前的计时器

[英]Timer before executing action again

In a switch statement I have a case, which represents a logout button in a game. 在switch语句中,我有一个案例,它代表游戏中的注销按钮。 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. 问题是,如果玩家反复单击它并在其上执行c.logout()方法一次又一次导致我的游戏出现大量滞后。 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. 然后在他们登录时将loginIn设置回true。

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: 以下是第一个Google搜索结果之一:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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