mercoledì 14 settembre 2016

Animazione modificata in tempo reale

Mi è stato chiesto di spiegare il codice di questo video che ho fatto qualche tempo fa:



Si tratta di un pulsante che segue un tracciato fatto e modificato dall'utente continuamente dall'utente.
Per prima cosa creiamo una grafica a mano libera cliccando sul pulsante degli strumenti di disegno, clicchiamo su freehand:
Ora nella card mettiamo il seguente codice per far disegnare l'utente:

on MouseMove newH, NewV
   if the mouse is down then MouseDown
end MouseMove

On MouseDown
   put the MouseLoc into temp
   if temp is within "0,0,382,316" then      
      put the points of graphic 1 into temp2
      if temp2 is empty then
         set the points of graphic 1 to temp & return & temp
      else
         set the points of graphic 1 to (temp2 & return & temp)
      end if   
   end if
end MouseDown

Notate che if the mouse is down serve per capire se l'utente mantiene cliccato il pulsante. Mentre ho impostato che il rettangolo del disegno dell'utente sia sempre dentro il rettangolo "0,0,382,316".
Ora l'utente può divertirsi a disegnare, mettiamo un pulsante col sole e in basso un pulsante con la scritta GO e un pulsante con Clear.


Ora nel pulsante GO mettete questo codice:

on mouseUp
   if the label of me is "GO" then
      set the label of me to "STOP"
   else
      set the label of me to "GO"
   end if
   moveSun
end mouseUp

on moveSun
   if the label of me is "STOP" then
      put the points of graphic 1 into mypoints
      put the angolo of me into temp
      if temp is empty or temp > the number of lines of mypoints then put 1 into temp #this is useful for the first launch or redrawing
      if mypoints is empty then put the loc of button "Sun" into mypoints
      put line temp of mypoints into newloc
      set the loc of button "Sun" to newLoc
      add 1 to temp      
      set the angolo of me to temp
      send "movesun" to me in 0.02 sec
   end if
end moveSun


La prima parte serve solo a far partire e fermare l'animazione, il messaggio successivo serve a spostare il sole lungo i punti del disegno. La proprietà personalizzata serve solo a riprendere da dove si è fermata l'animazione.
Se cambiate il valore dei secondi dell'ultima riga, cambierete la velocità dell'animazione.
Per finire nel pulsante Clear:
on mouseUp
   set the label of button "GO" to "GO"
   set the points of graphic "Curve" to empty
end mouseUp

Nessun commento:

Posta un commento