image click on multiple appearances

Asked by Alias

On the webpage that I'm trying to automate, there are three images in total that need to be clicked. First, an attack image. Second, a continue image. Third, a restart image. Sikuli should click the attack image, then move on to the continue image. Once the continue image is clicked, it needs to attack again. Here's a broken down, simplified order of what needs done.

Attack
Continue
Attack
Continue
Attack
Continue
Attack
Continue
Attack
Continue
Attack
Restart

Here is a sample script that I came up with to execute the above example. This, however, was quite slow. Each image click took approximately 1.5 seconds, which is extremely slow.
https://paste.ee/p/u6AiT

When I tried to modify the sample script to loop, it wouldn't work for me. It would only click one of the images, and then stop without error. Here is a sample script that I came up with to execute a looped script that should do the same as above.
https://paste.ee/p/aOEva

This brings me to my question, and goal. I'd like the script to click the AttackImage whenever it's on screen, and when it's not on screen, click the ContinueImage. When neither of those are on screen, it should click the RestartImage, which would restart the loop of the script to go back to AttackImage, then ContinueImage, etc. How would I manage to do this successfully?

Oh, and this is sort of related to the question, but why does a crosshair appear before each click, which I run the script?

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Eugene Maslov (emaslov1) said :
#3

Hi Alias,
I suggest, using observe functions is better in your case:

===8<===

myGame=Region(0,0,800,600)

def myChange(event, region=myGame):

    myattack=region.exists("attack.png",0)
    mycontinue=region.exists("continue.png",0)
    if myattack is not None:
        myattack.click()
    elif mycontinue is not None:
        mycontinue.click()
    else:
        myrestart=region.exists("restart.png",0)
        myrestart.click()

myGame.onChange(50,myChange)
myGame.observe(30,False)

===8<==

For other click logic, modify if-else conditions in myChange procedure.

Revision history for this message
Eugene Maslov (emaslov1) said :
#4

A quicker one:

myGame=Region(0,0,800,600)

def myChange(event, region=myGame):

    myattack=region.exists("attack.png",0)
    if myattack is not None:
        myattack.click()
    else:
        mycontinue=region.exists("continue.png",0)
        if mycontinue is not None:
            mycontinue.click()
        else:
            myrestart=region.exists("restart.png",0)
            myrestart.click()

myGame.onChange(50,myChange)
myGame.observe(30,False)

Can you help with this problem?

Provide an answer of your own, or ask Alias for more information if necessary.

To post a message you must log in.