giovedì 2 febbraio 2017

ControlAtLoc e ControlAtScreenLoc

Oggi vediamo due funzioni che ci dicono cosa c'è in un punto della finestra del nostro programma ControlAtLoc() e ControlAtScreenLoc().
Entrambi vi dicono cosa c'è in una posizione, ad esempio:

if controlAtLoc("10,10") is not empty then
   put the name of controlAtLoc("10,10") into temp
   answer "In posizione 10,10 c'è " & temp
end if

controlla cosa c'è alla posizione 10,10 della finestra corrente e se c'è qualcosa vi avverte.
La funzione ControlAtScreenLoc() è simile ma misura partendo dall'angolo in alto a sinistra dello schermo.
Possiamo sfruttare questa funzione per creare un gioco del puzzle, perchè basta controllare che i pezzi siamo in determinati punti. Se prendiamo ad esempio il baricentro della posizione corretta di un pezzo come punto di controllo, abbiamo un controllo preciso, ma non eccessivamente, giusto per capire se l'untente ha messo in ordine i pezzi.
Ad esempio per fare questo programma:


Basta creare 6 immagini vuote, un gruppo che le contenga, una cornice "qq" attorno al gruppo, un pulsante per caricare una nuova immagine e un pulsante come behavior per tutte e 6 le immagini.
Codice pulsante per caricare:
on mouseUp
   posizionacartella
   lock screen
   resetta
   answer file "scegli immagine"
   put it into temp
   create image
   set the filename of the last image to temp
   set the rect of the last image to the rect of group 1
   put "imm1,imm2,imm3,imm4,imm5,imm6" into lista      
   repeat for each item tItem in lista      
      export snapshot from rect ( the rect of image titem ) of this card to file tItem AS JPEG
      set the filename of image tItem to empty
      set the filename of image tItem to tItem
      set the loc of image titem to (random(383), random(257))
   end repeat   
   set the rect of group 1 to the rect of graphic "qq"
   delete the last image   
   unlock screen
end mouseUp

on resetta
   repeat with i=1 to 6
      set the rect of image i to the inizio of image i
   end repeat
end resetta

on posizionacartella
   set itemDel to "/"
   set the defaultFolder to item 1 to -2 of (the effective fileName of this stack)
end posizionacartella


Codice del behavior per tutte le immagini:

on MouseDown
   grab me   
end MouseDown

On MouseUp
   controlla
end MouseUp

on controlla
   put "imm1,imm2,imm3,imm4,imm5,imm6," into lista   
   if controlatloc("79,73") is not empty then    put the short name of controlatloc("79,73") & comma after posizione
   if controlatloc("199,73") is not empty then put the short name of controlatloc("199,73") & comma after posizione
   if controlatloc("319,73") is not empty then put the short name of controlatloc("319,73") & comma after posizione
   if controlatloc("79,193") is not empty then put the short name of controlatloc("79,193") & comma after posizione
   if controlatloc("199,193") is not empty then put the short name of controlatloc("199,193") & comma after posizione
   if controlatloc("319,193")is not empty then put the short name of controlatloc("319,193") & comma after posizione
   if lista = posizione then
      answer "Vinto"
   end if
end controlla


Fine, non serve altro.

Nessun commento:

Posta un commento