Simultaneous moving images

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
minorpayne10
Posts: 4
Joined: Thu Dec 10, 2015 12:17 pm

Simultaneous moving images

Post by minorpayne10 » Sat Dec 12, 2015 9:09 pm

answer "Hello World"

The dilemma: 3 images are moving simultaneously, a scrollbar that configures movespeed, a start/stop button.

My ideas:
start/stop: use lock moves and unlock moves, or interrupts the if loops I create to animate the images (see below)

Animation and speed: use an if loop to move images incrementally, using a Counter variable to control when it stops moving (I can't get the loop to stop). In these if loops, or in a loop by itself, activate a function that calls upon the scrollbar value and puts it into a variable that somehow scales the movement times of each image.

I have a water level that falls:
on moveWater
put [y distance water image needs to travel] into wCounter
if wCounter > 0
[move image water +1 in y direction]
subtract 1 from wCounter
send moveWater to me in 5*tScale milliseconds
else stop moving image water
end moveWater

For example, I have a droplet image that drips repeatedly, as long as the image of the water level is falling, and increase frequency with higher speed
on moveDrop
move image drop from [start] to [finish] in .5 milliseconds
send moveDrop to me in tscale*2 milliseconds
end moveDrop

and to disrupt the loop when water stops moving
on movestopped
stop moving image drop
end movestopped

Something like that. I want the scrollbar to go from 0 to 2 and rest at 1, and somewhere continuously update the time scale
put value of scrollbar speed into tScale

But I can't get this to work.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Simultaneous moving images

Post by dunbarx » Sat Dec 12, 2015 9:47 pm

Hi.

I changed your handler from pseudo to working. Let me know if this was not done the way you intended.

Code: Select all

on moveWater
   if the optionKey is down then exit to top
   get the loc of btn 2
   add 2 to item 2 of it
   put 2 into wCounter
   if wCounter > 0 then 
      set the loc of btn 2 to it
      subtract 1 from wCounter
      send moveWater to me in 5 milliseconds
   else
      stop moving image "water"
   end if
end moveWater
Works fine.

Craig Newman

minorpayne10
Posts: 4
Joined: Thu Dec 10, 2015 12:17 pm

Re: Simultaneous moving images

Post by minorpayne10 » Thu Dec 17, 2015 6:33 pm

Thank you Craig! So I adapted this code for my Play button.

This is my Stop/Start button:

Code: Select all

global tStop

on mouseUp
   send sStop
  send changeLabel
end mouseUp

on changeLabel
   if label of button "startstop" is "Start" then
   set label of button "startstop" to "Stop"
   else set label of button "startstop" to "Start"
end changeLabel

on sStop
   if label of button "startstop" is "Stop" then
      stop moving image "drop"
      put 1 into tStop
   else put 0 into tStop
   dispatch moveBurette
   dispatch moveDrop
   dispatch moveWater
end sStop

This is my code for the Play Button:

Code: Select all

global  tScale, aStep, bStep
on mouseUp
   set visible of image "drop" to true
   send moveWater, moveBurette, moveDrop
   set visible of button "startstop" to true
   set visible of button "play" to false
   set visible of button "reset" to true
   set label of button "startstop" to "Stop"
end mouseUp

on moveWater
      if the loc of image "water" = 693,495 then 
   else 
   get the loc of image "water"
   add 1 to item 2 of it
   set the loc of image "water" to it
   get thumbPos of scrollbar "speed" 
   put 450/it into aStep
   send moveWater to me in aStep milliseconds
end if
end moveWater

on moveBurette
   if the loc of image "zoom" is not 239,-1176 then exit to top
   else if tStop = 1 then exit to top
   else
      get the loc of image "zoom"
      subtract 1 from item 2 of it
      set the loc of image "zoom" to it
      get thumbPos of scrollbar "speed"
      put 450/(it*10.29) into bStep
      send moveBurette to me in bStep milliseconds
   end if
end moveBurette

on moveDrop
   repeat if tStop = 0
      move image "drop" from 692,403 to 692,494 in .5 seconds
      get thumbPos of scrollbar "speed"
      put 10 / it into cStep
      send moveDrop to me in cStep milliseconds
      end repeat
end moveDrop
What works:
moveWater- the water image moves
scrollbar adjusting speed of water
startstop changes the variable tStop between 1 and 0

Doesn't work:
moveBurette
The animation doesn't stop when I press "Stop"
when moveDrop works, it doesn't stop for any reason... continuous loop which times out after like 10 mins (I haven't gotten it to work since I changed the code to incorporate the repeat function)

I would like the startstop button to interrupt moveBurette, moveWater and moveDrop when "Stop" is pressed, and when "Start" is pressed, to resume them. Also I'm not sure why moveBurette is not working. And moveDrop isn't being initiated either.

Another thing: should I group the startstop and play buttons?

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Simultaneous moving images

Post by Klaus » Sat Dec 19, 2015 2:39 pm

Hi Madeline,

ouch, your scripts are a "majorpayne" to be honest! :D

Before we can clean them up a bit, what does this mean?
It does not make any sense and should have thrown an error:

Code: Select all

on moveDrop
   repeat if tStop = 0
      move image "drop" from 692,403 to 692,494 in .5 seconds
      get thumbPos of scrollbar "speed"
      put 10 / it into cStep
      send moveDrop to me in cStep milliseconds
      end repeat
end moveDrop
"repeat if..."?
Not valid syntax really.


Best

Klaus

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Simultaneous moving images

Post by Klaus » Sat Dec 19, 2015 5:16 pm

OK, until we have cleared the above mentioned problem, do this:
1. put all the handlers except "mouseup" into the card script.
2. Check my modified versions of your scripts and please read the comments:

Code: Select all

on changeLabel
   if the label of button "startstop" is "Start" then
      set label of button "startstop" to "Stop"
   else 
      set label of button "startstop" to "Start"
   end if
end changeLabel

on sStop
   if the label of button "startstop" is "Stop" then
      stop moving image "drop"
      put 1 into tStop
   else 
      put 0 into tStop
   end if
   
   ## SEND and DISPATCH need to have an "addressee"!
   # dispatch moveBurette
   # dispatch moveDrop
   # dispatch moveWater
   
   ## Since these handler are in the message hierarchy (card script)
   ## we simply execute them:
   moveBurette
   moveDrop
   moveWater
end sStop

on moveWater
   ## If we do not have an IF case, we do not need to use ELSE!
   ## if the loc of image "water" = 693,495 then 
   ## else 
   if the loc of image "water" <> 693,495 then 
      get the loc of image "water"
      add 1 to item 2 of it
      set the loc of image "water" to it
      get thumbPos of scrollbar "speed" 
      put 450/it into aStep
      
      ## This may result in very few millisecs, are you sure that a greater value doesn't make more sense?
      send "moveWater" to me in aStep milliseconds
   end if
end moveWater

on moveBurette
   if the loc of image "zoom" <> 239,-1176 then 
      exit to top
   end if
   
   if tStop = 1 then 
      exit to top
   end if
   
   ## I avoid nested IF THEN ELSE clauses wherever possible!
   get the loc of image "zoom"
   subtract 1 from item 2 of it
   set the loc of image "zoom" to it
   get thumbPos of scrollbar "speed"
   
   ## See above, the user may not notice if the movement is too fast!
   put 450/(it*10.29) into bStep
   send "moveBurette" to me in bStep milliseconds
end moveBurette

#on moveDrop
#   repeat if tStop = 0
#      move image "drop" from 692,403 to 692,494 in .5 seconds
#      get thumbPos of scrollbar "speed"
#      put 10 / it into cStep
#      send moveDrop to me in cStep milliseconds
#      end repeat
#end moveDrop
Modified Start/Stop button script:

Code: Select all

global tStop

on mouseUp
   ## See other scripts regarding SEND and DISPATCH
   ## send sStop
   ## send changeLabel
   sStop
   changeLabel
end mouseUp
Play button:

Code: Select all

global  tScale, aStep, bStep

on mouseUp
   set the visible of image "drop" to true
   
   ## Or much shorter :-)
   ## show img "drop
   
   ## See above regqrding SEND and DISPATCH
   ## You need to SEND resp EXECUTE the commands ONE by ONE!
   ## That's why only the first handler "moveWater" gets fired
   ## send moveWater, moveBurette, moveDrop
   moveWater
   moveBurette
   moveDrop
   set the visible of button "startstop" to true
   set the visible of button "play" to false
   set the visible of button "reset" to true
   set the label of button "startstop" to "Stop"
end mouseUp
Best

Klaus

Post Reply