onVanish not working on specific region

Asked by Jonathan

I am relatively new to Sikuli and python.I am running Sikuli on a MAC 10.9.5.
I am having trouble getting onVanish to work. I have extracted the relevant code to highlight the problem. The code is monitoring a game. As part of the game there is an image that moves periodically from one position to another. First consider the following

def imageMoved(event):
 self.monitor = False
 event.region.stopObserver()

self.monitor = True
image = Image()
currentImageRegion = image.region() # currentImageRegion redundant in this first version
if currentImageRegion.exists(imageScreenShot):

 onVanish(imageScreenShot, imageMoved) # THE CHANGED LINE

        observe(background = True)
        while self.monitor == True:
         currentImageRegion.highlight(1)
  # monitoring stuff

Here currentImageRegion is redundant. Here I assume "onVanish(imageScreenShot, imageMoved)" is applied to the whole screen as the region. While running the game, as the image moves, onVanish is not triggered which I sort of would have expected.If I then minimise the game onVanish is triggered which I also would expect.

Now for the problem:-

def imageMoved(event):
 self.monitor = False
 event.region.stopObserver()

self.monitor = True
image = Image()
currentImageRegion = image.region()
if currentImageRegion.exists(imageScreenShot):

 currentImageRegion.onVanish(imageScreenShot, imageMoved) # THE CHANGED LINE

        observe(background = True)
        while self.monitor == True:
         currentImageRegion.highlight(1)
  # monitoring stuff

In this piece of code onVanish is applied to the region currentImageRegion. This is a small region around the initial position of the image. I know that it is as I can see it due to the "currentImageRegion.highlight(1)" statement. However, when the image moves out of this region the onVanish is not triggered (this is what I want). Further, if I minimise the game the onVanish is still not triggered.

In conclusion it seems that onVanish is only working when applied to the whole screen and not a more specific region of the screen. Am I doing something wrong? Thanks in anticipation of any help.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Jonathan (jonathan-clark404) said :
#1

Sorry about the indentation in the code. Its fine before I post it.

Revision history for this message
Best RaiMan (raimund-hocke) said :
#2

I cannot really judge what you are doing there

... especially:
image = Image()
currentImageRegion = image.region()

... and the observe has to be for the same region as the defined expected events:

onVanish(imageScreenShot, imageMoved) # THE CHANGED LINE
observe(background = True)

... works on the screen

currentImageRegion.onVanish(imageScreenShot, imageMoved) # THE CHANGED LINE
observe(background = True)

... do not fit together - different regions

... but I have tested, that this works:

switchApp("Safari")

# the image to vanish
img = "img.png"
if not exists(img): exit(1)

# the region, the image currently is inside
reg = Region(getLastMatch())
reg.highlight(1)

hasMoved = False

def moved(e):
  global hasMoved
  print "moved"
  e.stopObserver()
  hasMoved = True

Debug.on(3)
reg.onVanish(img, moved)
reg.observe(FOREVER, True)
while not hasMoved:
  wait(1)
print "stopped"

when I now move the Safari widow, the handler is visited in that moment.

Revision history for this message
Jonathan (jonathan-clark404) said :
#3

Thanks for the time and effort you have put in for this.

Firstly I was not aware of the syntax "reg = Region(getLastMatch())" this is much more elegant than how I was doing it.

However, it still did not solve the problem and then by studying your code I saw my mistake. I had failed to put the observe on the region. i.e.

reg.observe(FOREVER, True)

and not

observe(FOREVER, True)

Thanks again
Jonathan