简体   繁体   English

我如何使某物在一定时间内伸展

[英]How do I make something stretch over a certain period of time

I'm trying to make an action "stretch" over a certain period of time, being 1 second. 我正在尝试在特定时间段(即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();
    }

} }

As you can see, this is the code for a "simple" auto clicker. 如您所见,这是“简单”自动答题器的代码。 It runs within this timer. 它在此计时器内运行。

public static void getClick() {
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        while (enabled) {
            click();
        }
    }
}, 1, 1);
}

Is it in any way possible to make this action "stretch" it's action to 1 second. 是否可以通过任何方式使此操作“拉伸”到1秒。 I really don't want it to click 12 times (Example), and then pause for 1 second. 我真的不希望它单击12次(示例),然后暂停1秒钟。 It'd be way nicer if these 12 clicks were "stretched" over that 1 second. 如果这12次点击在1秒钟内“拉伸”,那就更好了。 Is it possible anyone can help me? 有没有人可以帮助我?

You could just let it sleeps for 1000ms/cps. 您可以让它休眠1000ms / cps。 If the user set cps=10 then 10 Thread.sleep(1000/cps) would let it click ten times in one sec. 如果用户将cps = 10设置为10,Thread.sleep(1000/cps)将使其在一秒钟内单击十次。 With cps=0.5 it would click once in two seconds. cps = 0.5的情况下 ,它将在两秒钟内单击一次。
(with a little discrepancy, because your code needs time to execute). (有一点点差异,因为您的代码需要时间才能执行)。 And you should not create your random object in your loop. 而且您不应该在循环中创建随机对象。

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.

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