AutoJs implementation of energy collection in ant forest (precision optimization version)

Keywords: Javascript


  it's coming, it's coming, your stolen energy is back!
  in May, I wrote an article on AutoJs to save energy (this is a link) In the twinkling of an eye, five months have passed. The previous save energy script can only ensure that you can click on energy well in the first 10 seconds, but it will be wildly misplaced and unable to click accurately in the last 5 seconds. Because I'm not a person addicted to stealing energy, I haven't cared about that problem since my last article was sent out, but I'm really idle and bored recently. I clicked on the data of CSDN and found that the reading volume of this article has been increasing, and occasionally I'll have more fans. So today I opened this script and debugged the last five seconds of saving energy, Through my efforts, the accuracy of the script in the last five seconds has been improved. However, due to the recent double 11, some meow candy will fall when saving energy, which occasionally blocks the collection of energy, resulting in the energy collection rate not reaching 100%, but at least more than 95%.
  don't say much, just look at the effect! When collecting ordinary energy in front, the acceleration is done, and the energy saved in the back is the normal speed, otherwise it will take too long.
Link to this article:

1, Effect drawing first

2, Script ideas

   this update only optimizes the click logic for saving energy in the last five seconds. In fact, an offset is added according to the increase of the number of clicks, so that the later energy can be accurately collected. Other logic has not changed. Interested friends can see my previous two articles (AutoJs implementation of automatic energy collection in ant forest),(AutoJs implementation of ant forest rescue energy)

3, Concrete implementation

   there will be some coordinate calculations in the code (useless), but only based on my p30pro, there may be some differences in other mobile phones. The following code can be run directly. Someone has previously fed back that the logic of entering the ant forest cannot run correctly on his mobile phone. If this happens to someone, It may be necessary for you to make some minor adjustments according to the layout of your Alipay. The energy behind collection should be unaffected.


    function main(){
        console.log("Waiting for accessibility");
        console.log("Apply for screen capture permission");

        // rescueEnergy();
        console.log("Open Alipay");
        var search;
        while (!(search = findViewByClassAndText("Button","search")));

        console.log("Open the ant forest");
        while (!(search = findViewByClassAndText("TextView","search")));
        search.parent().parent().child(1).child(2).setText("Ant forest");
        click("Ant forest, plant a real tree in the desert for you");

        console.log("Energy harvester");
        while (!(findViewByClassAndText("Button","My records")));

     * To apply for screenshot permission, individual systems need to apply for screenshot permission every time. Sub threads are allowed automatically
    function requestScreenCapturePermision(){
            for (let i = 0; i < 100; i++) {
                if (textExists("Start now")) {
                    click("Start now");
            toast("Please allow screenshot permission and try again");

     * The random delay of cyclic energy acquisition is to look more human...
     * Maybe you will wonder why you didn't click to find the energy code
     * Because after my test #ffc2ff01, this color is not only on the energy ball, but also on the energy button!
     * So I just need to use a color finding cycle to realize the two operations of collecting the energy ball and entering the next page. Isn't it great? Hee hee
    function energyHarvester(){
        var image,point,errorCount;
        while (true) {
            if (!findViewByClassAndText("android.view.View","Return to the ant forest >")
                     && !findViewByClassAndText("android.view.View","Open now")) {
                image = captureScreen();
                point = findColor(image, "#ffc2ff01",{threshold: 4});
                if (point) {
                    errorCount = 0;
                    console.log("Collect energy : "+point);
  "Forest energy collection completed");

        if (findViewByClassAndText("android.view.View","Open now"))

        console.log("Quit Alipay");
        for (let i = 0; i < 3; i++) {

     * Save the lost energy ball
     * Observing the falling process of the energy ball, it is found that there are roughly five falling orbits
     * First, take a screenshot of the energy rescue page and measure the diameter of the energy ball 150px (I don't know if other mobile phones will be different)
     * The screenshot measurement shows that the energy ball falls about 678px(1513-835) in 1s, which is 1ms - > 0.67px  
     * Regional color finding within the range of five tracks
    function rescueEnergy(){"Start saving the lost green energy");
        randomClickBounds(findViewByClassAndText("android.view.View","Open now"));
        var image, point, time, offset = 0;
        var regionArray = [100, 800, 900, 150];
        var color = "#ff128900", color1 = "#fffc336a";
        do {
            image = captureScreen();
            point = findColor(image, color,{threshold: 4,region : regionArray});
        } while (!point);"Start collecting energy");
        var errorCount = 0;
        while (errorCount < 30) {
            time =;
            image = captureScreen();
            //When the 2021 double eleven meow candy detection is opened, you can click meow candy
            // point = findColor(image, color1,{region : regionArray});
            // if (!point) 
                point = findColor(image, color,{region : regionArray});
            if (point) {
                offset += 0.5;
                errorCount = 0;
                time = - time;
                click(point.x, (point.y + time + 70 + offset));
                console.log("Save energy : "+point + " " + time + " " + offset);
        }"Save energy end");

	//↓↓↓ here are some tool man methods used to obtain controls, clicks, delays, etc
    function findViewByClassAndId(name,viewId){
        return className(name).id(viewId).findOne(1000);

    function findViewByClassAndText(name,s){
        return className(name).text(s).findOne(1000);

    function randomDelayClick(t1,t2,view){

    function randomClickBounds(view){
        if (view) {
            bounds = view.bounds();
            return click(random(bounds.left,bounds.right),random(,bounds.bottom));
        console.log("randomClickBounds view == null");
        return false;

    function delay(seconds) {
        sleep(1000 * seconds);

    function delayBack(seconds){

    function textExists(str){
        return textContains(str).exists();


  the summary is that there is no summary. If you think it's good, give me a praise. Please do not reprint without permission.
Link to this article:

Posted by parthatel on Sun, 31 Oct 2021 15:11:13 -0700