Page 1 of 2
Creating a flashing effect
Posted: Wed Mar 18, 2020 8:15 am
by glenn9
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
Re: Creating a flashing effect
Posted: Wed Mar 18, 2020 10:54 am
by Klaus
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!
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
Re: Creating a flashing effect
Posted: Wed Mar 18, 2020 11:20 am
by bogs
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
Re: Creating a flashing effect
Posted: Wed Mar 18, 2020 12:15 pm
by Thierry
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
Re: Creating a flashing effect
Posted: Wed Mar 18, 2020 12:54 pm
by glenn9
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!
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
Re: Creating a flashing effect
Posted: Wed Mar 18, 2020 12:57 pm
by glenn9
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
Re: Creating a flashing effect
Posted: Wed Mar 18, 2020 1:04 pm
by Klaus
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
Re: Creating a flashing effect
Posted: Wed Mar 18, 2020 1:06 pm
by 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
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
Re: Creating a flashing effect
Posted: Thu Mar 19, 2020 9:28 am
by Thierry
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
Re: Creating a flashing effect
Posted: Thu Mar 19, 2020 10:11 am
by bogs
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

Re: Creating a flashing effect
Posted: Thu Mar 19, 2020 10:17 am
by Thierry
bogs wrote:
..... than my hackey-sack code
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
Re: Creating a flashing effect
Posted: Thu Mar 19, 2020 4:56 pm
by glenn9
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
Re: Creating a flashing effect
Posted: Thu Mar 19, 2020 5:04 pm
by Klaus
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
Re: Creating a flashing effect
Posted: Fri Mar 20, 2020 8:06 am
by glenn9
Klaus,
Many thanks for this - I was unaware of the 'any' command and I like its simplicity and clarity.
Regards,
Glenn
Re: Creating a flashing effect
Posted: Fri Mar 20, 2020 2:06 pm
by dunbarx
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