Stopping a "run away train"

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Stopping a "run away train"

Post by urbaud » Sun Aug 29, 2010 1:27 am

I have been experimenting with various Rev commands as part of my learning and today I was experimenting with the rectangle property and developed the following code. It displays a label with text (no border) moving across the stack window. My problem is I can’t figure out a way to use the mouse to stop it. At the moment I have to use Command + . to stop it.

I have tried:
if the mouseClick is true then exit repeat - doesn’t work
If the mouseClick is true then exit mouseUp - doesn’t work

Even tried setting the wait command to 0 with another button, with no luck. Any suggestions?

Code: Select all

Global milsec
on mouseUp
put 1 into milsec
 repeat with i = -10 to 600  --the 600 is the width of the stack window
    repeat with t = -10 to 600
         set the rectangle of fld "t3" to i,100,t,125
		Wait milsec millisecond with messages
        end repeat
      end repeat
end mouseUp
urbaud

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Stopping a "run away train"

Post by mwieder » Sun Aug 29, 2010 2:17 am

Right - you do *not* want that repeat loop inside your mouseup handler. Try something like the following (untested): that will let the mouseUp handler finish before starting the repeat loops. Otherwise the fact that you're still in a mouseUp handler will block any further mouse commands until it's done.

Code: Select all

on mouseUp
  send "DisplayTheMessage" to me in 0 milliseconds
end mouseUp

on DisplayTheMessage
  repeat with i = -10 to 600
    repeat with t= -10 to 600
      set the rectangle of fld "t3" to i,100,t,125
      if the mouse is down then
        exit DisplayTheMessage
      end if
    end repeat
  end repeat
end DisplayTheMessage

urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Re: Stopping a "run away train"

Post by urbaud » Sun Aug 29, 2010 6:52 am

Hi mwieder,

Thanks for your suggested script. I tried it, but it didn't work. What I did was place it in a new button. When I clicked on the button, it would stop momentarily, but as soon as I let up the mouse button it started again. So, I placed another button and inserted the following code. Clicking on this new button stopped it. So, thanks for the help.

Code: Select all

on mouseUp
   put 0 into i
   put 0 into t
end mouseUp
urbaud

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: Stopping a "run away train"

Post by Regulae » Sun Aug 29, 2010 9:14 am

Hi urbaud,

This works in a button script:

Code: Select all

global repeatEnable

on mouseUp
   if repeatEnable = "yes" then
      put "no" into repeatEnable
      pass mouseUp
      end if
   if repeatEnable <> "yes" then
      send "DisplayTheMessage" to me in 0 milliseconds
      put "yes" into repeatEnable
   end if
end mouseUp

on DisplayTheMessage
   repeat with i = -10 to 600
      repeat with t= -10 to 600
         set the rectangle of fld "t3" to i,100,t,125
         if the mouse is down then
            exit DisplayTheMessage
         end if
      end repeat
   end repeat
end DisplayTheMessage
Regards,
Michael

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Re: Stopping a "run away train"

Post by Janschenkel » Sun Aug 29, 2010 10:18 am

A while back I wrote up a blog post about the 'wait' command to improve the user experience in tight loops: But the waiting makes me curious

HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Re: Stopping a "run away train"

Post by urbaud » Sun Aug 29, 2010 9:42 pm

Hi Michael and Jan,

Thanks for your suggestions. It's amazing how quickly the Rev developer community comes to the aid of a fellow "developer", albeit a beginner and hobbyist only. As one of the other developers said in another posting, "You can do the same function in Rev in several different ways" (I'm paraphrasing). Your suggestions are the first time I've seen a "on mouseUp" handler "influence" another handler i.e. the "on DisplayTheMessage", or perhaps it's the other way around, the "on DisplayTheMessage" function is "influencing" the "on mouseUp" handler. .

Just a question about custom functions. Am I correct in assuming that the "on DisplayTheMessage" handler is in fact a custom function? If that's correct, then mwieder could have called it anything he wanted, correct? or is the "onDisplayTheMessage" function built into Rev?

Dan
urbaud

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Stopping a "run away train"

Post by mwieder » Sun Aug 29, 2010 11:25 pm

I could indeed have called it anything (and did).

Well, I warned you it was untested, and sure enough I forgot to put in a semaphore to flag the fact that we were already looping. And Michael fixed that one up. And your method of setting the two global variables from a second button obviously also works. It's a bit of a brute force method to my way of thinking, but as you mentioned, there are any number of ways to handle any given problem.

Check out Jan's writeup if you haven't yet.

Post Reply