Page 1 of 1

Restricting button movement

Posted: Thu Mar 06, 2014 4:42 am
by KennyR
Okay last post for the evening....I swear!!

I would like to restrict the movement of a button to only the x and y coordinates and prevent the grab command from allowing any diagonal movement. I want to have the buttons on the card move like tiles and only allow movement along the vertical and horizontal axis. I will attach a pic of what I am doing and you will understand what I mean. (hopefully). I am attaching the code I have in each button to allow the buttons to switch places. All of this works fine with the exception to the restriction of movement I am looking for....any suggestions would be greatly appreciated!

Code: Select all

global vOldLoc
local vX,vY,vArea
on mouseDown
   grab me
   set the layer of me to top
   --Record the location of the button that was chosen to drag--
   put the loc of me into vOldLoc
end mouseDown

on mouseStillDown
   --Track the location of the grabbed button--
   put the loc of me into vArea
   --X location
   put item 1 of vArea into vX
   --Y location
   put item 2 of vArea into vY
   
   --Evaluate x,y movement--
   switch
      case  item 2 of vOldLoc < item 2 of vArea 
         repeat with x=1 to the number of btns on this cd
      if the intersect(me,btn x) then 
         put the short name of btn x  into vName
         put the loc of btn x  into vLoc
           move me to vLoc in 10 ticks
        exit repeat
      else
         end if
      end repeat
       move btn vName to vOldLoc in 10 ticks
       break
       
      case  item 2 of vOldLoc > item 2 of vArea 
       repeat with x=1 to the number of btns on this cd
      if the intersect(me,btn x) then 
         put the short name of btn x  into vName
         put the loc of btn x  into vLoc
         move me to vLoc in 10 ticks
        exit repeat
      else
         end if
      end repeat
      move btn vName to vOldLoc in 10 ticks
      break
      
      case item 1 of vOldLoc > item 1 of vArea
      repeat with x=1 to the number of btns on this cd
      if the intersect(me,btn x) then 
         put the short name of btn x  into vName
         put the loc of btn x  into vLoc
           move me to vLoc in 10 ticks
        exit repeat
      else
         end if
      end repeat
       move btn vName to vOldLoc in 10 ticks
         break
      case  item 1 of vOldLoc < item 1 of vArea
         repeat with x=1 to the number of btns on this cd
      if the intersect(me,btn x) then 
         put the short name of btn x  into vName
         put the loc of btn x  into vLoc
           move me to vLoc in 10 ticks
        exit repeat
      else
         end if
      end repeat
      --Move the button that was intersected to the grabbed buttons first location--
       move btn vName to vOldLoc in 10 ticks
         break
         default
   end switch
end mouseStillDown
[img]
screen grab01.jpg
[/img]

Re: Restricting button movement

Posted: Thu Mar 06, 2014 6:03 am
by dunbarx
Well, it is late for me too. This works, but needs smoothing. But maybe it will give you an idea.

I made a new button and put this into its script

Code: Select all

global hloc,vloc

on mouseMove
   if the mouse is down then
      if item 1 of the mouseLoc - hloc > item 2 of the mouseLoc - vloc then
         repeat until the mouse is up
            set the loc of me to item 1 of the mouseLoc & "," &  item 2 of the loc of me
         end repeat
         exit to top
      else
         repeat until the mouse is up
            set the loc of me to item 1 of the loc of me & "," &  item 2 of the mouseLoc
         end repeat
         exit to top
      end if
   end if
end mouseMove

on mouseEnter
   put item 1 of the clickloc into hLoc
     put item 2 of the clickLoc into vLoc
end mouseEnter
Click on the button, whichever way you drag closer to one of the axes, the button will track.

Craig Newman

Re: Restricting button movement

Posted: Thu Mar 06, 2014 1:35 pm
by Klaus
Hi guys,

"mousemove" comes with 2 very convenient parameters: x,y
So no need to check the mouseloc later in that handler (doesn't hurt, of course 8) ):

Code: Select all

on mousemove x,y
  ## do something with x and y
end mousemove
Best

Klaus

Re: Restricting button movement

Posted: Thu Mar 06, 2014 1:55 pm
by KennyR
Thanks Craig for your help! Could you elaborate a bit on your repeat loops so I can get a better idea of what your looking for? It seems the handler works fine on the y axis but is spotty when you attempt the x axis. I know you said it needs to be smoothed, but I'm having a tough time understanding the loops....I was also thinking maybe I could give the illusion the user is controlling movement by tracking the location of the mouse and sending a move command to either the x or y axis. That way tiles are only exchanges one at a time and skipping over them would not be possible. Maybe a touchMove handler would be appropriate? Is this the same thing as the mouseEnter command?

Re: Restricting button movement

Posted: Thu Mar 06, 2014 2:56 pm
by KennyR
Klaus! Nice! Working on it now...

Re: Restricting button movement

Posted: Thu Mar 06, 2014 3:36 pm
by dunbarx
Klaus is right, as usual, about the x,y.

Losing loops is always better in favor of native functionality.

My offering was more geared toward detecting the initial direction of movement, either left/right or up/down, in order to lock all subsequent movement to the axis indicated. This is the heart of the task.

Craig

Re: Restricting button movement

Posted: Thu Mar 06, 2014 3:48 pm
by KennyR
Hey guys....I have been fooling around with the touchMove handler since this will be on an iOS device...having luck with the x axis but not the Y.....any ideas?

Code: Select all

global vX,vY
on touchStart tID
   put item 1 of the loc of me into vX
   put item 2 of the loc of me into vY
end touchStart

on touchEnd
   
end touchEnd

on touchMove tID, pX,pY
   if pX > vX or pX<vX then
      put vY into pY
      
   else
   end if
   
   if pY>vY or pY<vY then
      put vX into pX
      
   else
   end if
   set the location of the target to pX,pY
   
end touchMove