Page 1 of 1

Simulating button pushes

Posted: Sat Nov 16, 2013 1:17 am
by calmrr3
Hello,

I would like to simulate the pushing of a button. The button toggles the visibility of something and I would like on cardOpen for the button to act as if it being pressed at random incidents between 0.5 seconds and 2 seconds so that the thing it toggles(Line 4) automatically shows and hides at random intervals.


Here is the code for the button.

Code: Select all

on mouseUp
lock screen for visual effect
   repeat with y = 1 to 27
      put y into line y of temp
   end repeat
   sort temp by random(10000) 
   
   repeat with y = 1 to 27
   set the points of graphic "Line 4" to the loc of graphic "tCircle4" & return & the loc of graphic ("tCircle" &  line y of temp)
       end repeat

 if the visible of graphic "Line 4" is true then set the visible of graphic "Line 4" to false
   else set the visible of graphic "Line 4" to true
   unlock screen with visual effect dissolve very fast
end mouseUp
Thanks!

EDIT:
I made a start using this. It does work however it doesn't stop and I have to close Livecode to stop it. I also couldnt get the random intervals working.
I tried

Code: Select all

wait for random (500,2000) milliseconds
but i'm guessing that is incorrect.

Code: Select all

   repeat for 60 seconds
  click button "1072" at the loc of card button ID"1072"
     wait for 2 seconds
  end repeat 

Re: Simulating button pushes

Posted: Sat Nov 16, 2013 2:25 am
by makeshyft
you can send messages to objects ......

so send "mouseUp" to button "Button Name" (sends right away)

or

send "mouseUp" to button "Button Name" in 1 seconds (delayed)

Re: Simulating button pushes

Posted: Sat Nov 16, 2013 2:49 am
by dunbarx
Hi.

This is a perfect opportunity to use the "send in time" variant of the 'send" command. It has the advantage over using a "wait" command in that control is returned to the user. Read up on this, and try to get a sense of how "the pendingMessages" are working behind the scenes. Make two buttons. Name one "toggle", in the script of the other:

Code: Select all

on mouseUp
   startTiming
end mouseUp

on startTiming
   set the visible of  button "toggle" to not the visible of btn "toggle"
   If the optionKey is down then
      show btn "toggle"
      exit to top
   end if
   send "startTiming" to me in random(90) + 30 ticks
end startTiming
You still have control over all aspects of the program. Hold the optionKey down for a while to get out of it.

Craig

Re: Simulating button pushes

Posted: Sat Nov 16, 2013 3:09 am
by calmrr3
Brilliant, thanks! Got it running with this:

Code: Select all

on mouseUp
   startTiming
end mouseUp

on startTiming
   lock screen for visual effect
   repeat with y = 1 to 27
      put y into line y of temp
   end repeat
   sort temp by random(10000) 
   
   repeat with y = 1 to 27
   set the points of graphic "Line 4" to the loc of graphic "tCircle4" & return & the loc of graphic ("tCircle" &  line y of temp)
       end repeat
     
   set the visible of graphic "Line 4"  to not the visible of graphic "Line 4" 
   If the optionKey is down then
      show graphic "Line 4" 
      exit to top
   end if
   send "startTiming" to me in random(90) + 30 ticks
unlock screen with visual effect dissolve very fast
end startTiming