Creating a flashing effect

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

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Creating a flashing effect

Post by glenn9 » Wed Mar 18, 2020 8:15 am

Hi,

I'm trying to get an object's background colour to 'flash' and can achieve this with a 'wait' command as below:

Code: Select all

on mouseup
   put "red, orange, yellow, green, blue, violet" into vColors
   repeat for each item x in vColors
      set backcolor of graphic 1 to x
      wait .5 seconds
      end repeat
   end mouseup
However, if I want two flashing objects to flash at the same time I can't achieve this with a 'wait' command.

I've read that I need to use a 'send' command instead and have tried various permutations but failed so far to achieve a flash effect with 'send'!

Code: Select all

  
  -- this doesn't work!
  on mouseup
      flash
   end mouseup
   
on flash
   put "red, orange, yellow, green, blue, violet" into vColors
   repeat for each item x in vColors
      set backcolor of graphic 1 to x
      
   end repeat
   send "flash" to me in .5 seconds
end flash
Grateful for any pointers!

Regards,

Glenn

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

Re: Creating a flashing effect

Post by Klaus » Wed Mar 18, 2020 10:54 am

Hi Glenn,

I only see that you are "flashing" ONE object!? Where is the other one?

Of course you will need to out this into the same repeat loop.
And your SEND script:
1. The repeat loop will be too fast to notice
2. Since you SEND this command every 500 millisecs you will have an eternal loop you will have to force-quit! 8)

You can use WAIT in this case, maybe add a "... with messages", so the engine has time
to take care of possible other actions (mouseup etc.) during the wait.

This should do the trick:

Code: Select all

on mouseup
    flash
end mouseup
   
on flash
   put "red,orange,yellow,green,blue,violet" into vColors
   repeat for each item x in vColors
      lock screen
      set the backcolor of graphic 1 to x
      ## Here you should add the other object-to-flash!
      unlock screen
      wait 500 millisecs with messages
   end repeat
end flash
Best

Klaus

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Creating a flashing effect

Post by bogs » Wed Mar 18, 2020 11:20 am

My best guess Glenn is that you think it isn't working because you didn't step through it in the debugger. Your code, unchanged, works just fine, you just don't see the effect your looking for (probably) because it is going by WAY too fast.

(Edit - Klaus popped in while I was testing the following out)

If all you want is for the graphic to look like it is flashing, try this instead (it will take a while to go up and down the scale)...

Code: Select all


on mouseup
      flash
end mouseup
   
on flash
   put 0 into t1
   repeat 2 --<-- graphic will flash twice...
      repeat until t1=200
         put (t1 +5) into t1
         set the backcolor of graphic 1 to (t1, t1, t1)
         wait 2 ticks
      end repeat
      
      repeat until t1 = 0
         put (t1 -5) into t1
         set the backcolor of graphic 1 to (t1, t1, t1)
         wait 2 ticks
      end repeat            
   end repeat   
end flash
Image

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Creating a flashing effect

Post by Thierry » Wed Mar 18, 2020 12:15 pm

Another way is using the send command:

Code: Select all

local t1, Nrepeat

on mouseup
   -- flash
   put 0 into t1
   put 2 into Nrepeat
   send "flash2 +5" to me in 0
end mouseup

on flash2 deltaChange
   add deltaChange to t1
   if deltaChange > 0 and t1 > 200 then
      put "-5" into deltaChange
   else if deltaChange < 0 and t1 <= 0 then
      subtract 1 from Nrepeat
      if Nrepeat <= 0 then exit flash2
      put "+5" into deltaChange
   end if
   set the backcolor of graphic 1 to (t1, t1, t1)
   send "flash2" && deltaChange to me in 2 ticks
end flash2

Take what you feel more confortable with and enjoy!
Both ways are fine...

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: Creating a flashing effect

Post by glenn9 » Wed Mar 18, 2020 12:54 pm

Klaus wrote:
Wed Mar 18, 2020 10:54 am
Hi Glenn,

I only see that you are "flashing" ONE object!? Where is the other one?

Of course you will need to out this into the same repeat loop.
And your SEND script:
1. The repeat loop will be too fast to notice
2. Since you SEND this command every 500 millisecs you will have an eternal loop you will have to force-quit! 8)

You can use WAIT in this case, maybe add a "... with messages", so the engine has time
to take care of possible other actions (mouseup etc.) during the wait.

This should do the trick:

Code: Select all

on mouseup
    flash
end mouseup
   
on flash
   put "red,orange,yellow,green,blue,violet" into vColors
   repeat for each item x in vColors
      lock screen
      set the backcolor of graphic 1 to x
      ## Here you should add the other object-to-flash!
      unlock screen
      wait 500 millisecs with messages
   end repeat
end flash
Best

Klaus
Klaus,

Many thanks for your tip which I've incorporated, and now with the 2nd graphic included.

I wanted both graphics to flash/blink colour independently and so introduced another 'repeat' to loop through different colours.

It works after a fashion but graphic 2 has primacy over graphic 1, I guess because of the order of the repeats and the position of the 'wait' command.

I was just wondering whether a 'send' rather than 'wait' command would achieve independent flashing??

Regards,

Glenn

Code: Select all

local vColors, vColors2
on mouseup
    flash
end mouseup
   
on flash
   put "red,orange,yellow,green,blue,violet" into vColors
   put "violet, blue, green, yellow, orange, red" into vColors2
   repeat for each item x in vColors
      repeat for each item y in vColors2
         lock screen
         set the backcolor of graphic 1 to x
         ## Here you should add the other object-to-flash!
         set the backcolor of graphic 2 to y
         unlock screen
         wait 500 millisecs with messages
         end repeat
      end repeat
   end flash

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: Creating a flashing effect

Post by glenn9 » Wed Mar 18, 2020 12:57 pm

Thierry wrote:
Wed Mar 18, 2020 12:15 pm
Another way is using the send command:

Code: Select all

local t1, Nrepeat

on mouseup
   -- flash
   put 0 into t1
   put 2 into Nrepeat
   send "flash2 +5" to me in 0
end mouseup

on flash2 deltaChange
   add deltaChange to t1
   if deltaChange > 0 and t1 > 200 then
      put "-5" into deltaChange
   else if deltaChange < 0 and t1 <= 0 then
      subtract 1 from Nrepeat
      if Nrepeat <= 0 then exit flash2
      put "+5" into deltaChange
   end if
   set the backcolor of graphic 1 to (t1, t1, t1)
   send "flash2" && deltaChange to me in 2 ticks
end flash2

Take what you feel more confortable with and enjoy!
Both ways are fine...

Thierry
Thanks Thierry, apologies, I missed your post whilst I was replying to Klaus, I'll work have a work through this.

Kind regards,

Glenn

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

Re: Creating a flashing effect

Post by Klaus » Wed Mar 18, 2020 1:04 pm

Hi Glenn,

aha, in this case REPEAT FOR EACH is not the right one, since SPEED inside of the loop is not neccessary here.
Do it this way:

Code: Select all

on mouseup
    flash
end mouseup

on flash
   put "red,orange,yellow,green,blue,violet" into vColors
   put "violet,blue,green,yellow,orange,red" into vColors2
   repeat with tColor = 1 to 6
         lock screen
         set the backcolor of graphic 1 to item tColor of vColors
         set the backcolor of graphic 2 to item tColor of vColors2
         unlock screen
         wait 500 millisecs with messages
   end repeat
end flash
Best

Klaus

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Creating a flashing effect

Post by Thierry » Wed Mar 18, 2020 1:06 pm

Thanks Thierry, apologies,
I missed your post whilst I was replying to Klaus, I'll work have a work through this.
Kind regards,
Glenn
Hi Glen,

No problem with me, but thanks anyway :)

And the code I posted does exactly what bogs's one does;
hoping that it might interest you to compare side by side two options.

So, don't forget bogs too :)

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Creating a flashing effect

Post by Thierry » Thu Mar 19, 2020 9:28 am

Glen,

if you like repeat for each - which I do too :)
here is another way:

Code: Select all



on flash
   constant C = "red violet,orange blue,yellow green,green yellow,blue orange,violet red"
   repeat for each item aColor in C
      lock screen
      set the backcolor of graphic 1 to word 1 of aColor
      set the backcolor of graphic 2 to word 2 of aColor
      unlock screen
      wait 500 millisecs with messages
   end repeat
end flash

Enjoy!

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Creating a flashing effect

Post by bogs » Thu Mar 19, 2020 10:11 am

Thierry wrote:
Wed Mar 18, 2020 1:06 pm
...the code I posted does exactly what bogs's one does...
So, don't forget bogs too :)
Heh, thank you for the mention, Thierry, but I think yours was a tad better thought out than my hackey-sack code :wink:
Image

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Creating a flashing effect

Post by Thierry » Thu Mar 19, 2020 10:17 am

bogs wrote: ..... than my hackey-sack code :wink:
Well, don't see anything wrong with your code my friend :)

Different way of thinking and putting some lines of code one after one
to make some psychedelic flashes...

Take care,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: Creating a flashing effect

Post by glenn9 » Thu Mar 19, 2020 4:56 pm

Just wanted to say a big thank you to everyone for all the hints and tips.

The ideas have enabled me to eventually produce the effect that i had originally imagined which was to have multiple graphics blinking independently random colours.

My learning points from his project were:

- now aware of the colorNames() function (saves having to write out specific colours etc)
- use of the random() function with 'the number of items' parameter
- managed to implement 'repeat while...' for the first time on my own!
- not to tie myself up in knots with a 'send' command (still not 100% sure when a 'send' command is more appropriate than 'wait'?)

My final code looked like this

Code: Select all

global vColorNames, sFlag, x, y, z
on mouseup
put "true" into sFlag
flash
end mouseup

on flash
put colornames() into field 1
replace cr with ", " in field 1

repeat while sFlag is true

put random(the number of items of field 1) into x
put random(the number of items of field 1) into y
put random(the number of items of field 1) into z

lock screen
set the backcolor of graphic 1 to item x of vColorNames 
set the backcolor of graphic 2 to item y of vColorNames 
set the backcolor of graphic 3 to item z of vColorNames 
unlock screen

wait 500 millisecs with messages
end repeat
end flash


Regards,

Glenn

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

Re: Creating a flashing effect

Post by Klaus » Thu Mar 19, 2020 5:04 pm

Hi Glenn,

you can have it even shorter:

Code: Select all

on flash
   ## No need to convert to COMMA separated list, 
   ## we can work with this CR separated list see below...
   
   ## vColorNames does NOT have any content!
   put colornames() into vColorNames 
   put vColorNames into field 1

   repeat while sFlag is true      
      lock screen

      ## ANY is a variation of RANDOM and fits fine here
      set the backcolor of graphic 1 to any line of vColorNames 
      set the backcolor of graphic 2 to any line of vColorNames 
      set the backcolor of graphic 3 to any line of vColorNames 
      unlock screen
      wait 500 millisecs with messages
   end repeat
end flash
Best

Klaus

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: Creating a flashing effect

Post by glenn9 » Fri Mar 20, 2020 8:06 am

Klaus,

Many thanks for this - I was unaware of the 'any' command and I like its simplicity and clarity.

Regards,

Glenn

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

Re: Creating a flashing effect

Post by dunbarx » Fri Mar 20, 2020 2:06 pm

I have not run all the offerings here, but they seem to me to be too ordered. I like chaos.

Just an experiment. Make four buttons. Put this in the card script:

Code: Select all

on mouseUp
   put "red,green,yellow,blue,orange" into tColors
   repeat 30
      set the backcolor of btn random(4) to any item of tColors
      wait random(20)
   end repeat
end mouseUp
Craig

Post Reply