[英]How do I make something stretch over a certain period of time
我正在嘗試在特定時間段(即1秒)內“拉伸”動作。
public static int i = 0;
public static void click() {
Robot robot;
try {
robot = new Robot();
for (i = i; i < cps; i++) {
robot.mousePress(InputEvent.BUTTON1_MASK);
Random rn = new Random();
int range = cps - 10 + 1;
int randomNum = rn.nextInt(range) + 10;
robot.delay(randomNum);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
i++;
}
if (i == cps) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
i = 0;
}
} catch (AWTException e) {
e.printStackTrace();
}
}
如您所見,這是“簡單”自動答題器的代碼。 它在此計時器內運行。
public static void getClick() {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
while (enabled) {
click();
}
}
}, 1, 1);
}
是否可以通過任何方式使此操作“拉伸”到1秒。 我真的不希望它單擊12次(示例),然后暫停1秒鍾。 如果這12次點擊在1秒鍾內“拉伸”,那就更好了。 有沒有人可以幫助我?
您可以讓它休眠1000ms / cps。 如果用戶將cps = 10設置為10,則Thread.sleep(1000/cps)
將使其在一秒鍾內單擊十次。 在cps = 0.5的情況下 ,它將在兩秒鍾內單擊一次。
(有一點點差異,因為您的代碼需要時間才能執行)。 而且您不應該在循環中創建隨機對象。
Random rn = new Random();
while(true) {
robot.mousePress(InputEvent.BUTTON1_MASK);
int range = cps - 10 + 1;
int randomNum = rn.nextInt(range) + 10;
robot.delay(randomNum);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
i++;
try {
Thread.sleep(1000/cps);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.