Code archives/Miscellaneous/Simple Blinking Technique

This code has been declared by its author to be Public Domain code.

Download source code

Simple Blinking Technique by JoJo2002
This code is if you ever want to have text or images blink on the screen.
You need to adjust the numbers 2000 and 2200 for the effect. The second number should always be higher than the first.

NOTE: You need to draw something to the screen or whatever. If you copy and paste the code as is below, then you won't see the effect. The program has waste time doing something else too.

The code have the text blink
graphics 800,600,16,2
global looptime
SetBuffer BackBuffer()
looptime = Millisecs()
While Not KeyDown(1)

;places text on the screen
If MilliSecs() < (looptime+2000)
   Text 342,480,"Press Space"
Else
   ;takes the text off the screen and resets looptime to start it all over again
   If MilliSecs() > (looptime+2700) Then looptime = MilliSecs()
EndIf

Flip
Wend

Comments

mv3332012
Insert a "Cls" under the "Flip", to see the text blink.


matibee2012
Since mv333 has brought this back to the front page (search results will come here anyway) I thought I'd add my method.

If you don't want to hold variables for every object that might blink or flash and you can live with constant on/off rates, just do this....

if (( Millisecs() / 1000 ) & 1 ) DrawText "Blinking!", 300, 300


and that's it! (Blitzmax sample code, but applicable to Blitzplus too by the looks of it.) No variables to manage at all.


What is it doing and how do I alter the blinking rates I hear you ask...

"Millsecs() / 1000" is getting the current time in whole seconds, "& 1" is a bitwise operation to see if the seconds are odd or even.

For faster blinking divide by a bigger number, slower blinking needs a bigger divisor.

eg.
"Millsecs() / 2000" gives half second blinking
"Millsecs() / 500" gives two second blink rates


azrak2012
<code>
Graphics 800,600,16,2

AutoMidHandle True
image1=CreateImage (200,50,1)
SetBuffer ImageBuffer (image1,0)
ClsColor 128,0,0
Cls
Text 100,25,"Press Escape ",1,1

SetBuffer BackBuffer()
timer1=CreateTimer(1)

While Not KeyDown(1)

If atime% Mod 2 Then
DrawImage image1,400,300
EndIf

atime% = TimerTicks(timer1)

Text 10,50 ,"time: "+ atime% Mod 2

Flip
Cls
Wend
FreeTimer timer1
</code>


Code Archives Forum