News:

Function Finder  Find all the functions within source code files

Main Menu

Using Timer With Sinus To Flash A Message

Started by kevin, October 31, 2010, 01:46:15 PM

Previous topic - Next topic

kevin

 Using Timer With Sinus  To Flash A Message

 This example flashes a text message at a constant rate regardless of the computers speed.  


PlayBASIC Code: [Select]
   StartTime=Timer()
Do
Cls 0
CurrentTime=Timer()

TimePast=CurrentTime-StartTime
Scaler#=TimePast/1000.0

Angle#=wrapangle((360*4)*Scaler#,0)

If Sin(Angle#)>0
centertext 400,300,"Some Message"

endif


Sync
loop









Using Timer With Sinus  To Fade In/Out At Fixed Speed

This is how I normally used sinus to fade in/out.  


PlayBASIC Code: [Select]
   loadfont "veranda",1,128,0

Cls 255
boxc 0,0,32,32,true,$ff00ff
boxc 32,32,64,64,true,$ff00ff
getimage 1,0,0,64,64

TileIMage 1,0,0,false
GetImage 1,0,0,256,256

StartTime=Timer()



Do
CurrentTime=Timer()

TimePast=CurrentTime-StartTime

Scaler#=TimePast/1000.0

Xpos=((64*4))*Scaler#
Ypos=((64*2))*Scaler#

tileimage 1,Xpos,Ypos,false

Angle#=wrapangle((360/2)*Scaler#,0)

ink rgbfade($404040,50+SinRadius(Angle#,50))

centertext 395,195,"Some Message"
ink rgbfade($ffffff,50+SinRadius(Angle#,50))
centertext 400,200,"Some Message"

Sync
loop







monkeybot

Hi Kevin,

please could you explain why this is better than just having an incremental counter..
Thanks

kevin

#2
 Basing it (or anything) on time, means It doesn't matter how fast or slow the computer runs, as the duration and frequency are the same.   

monkeybot


kevin

#4
  Using Timer For Constant Movement

  This example is in the same theme as the others, but this time we're plotting motion.  When objects are created they'd each given a start and ending coordinate,  plus a duration in milliseconds of how long this journey will take.   Rather than giving each object a speed per frame, the code works off how much time has past since the object was spawned, then converts this into a scaler.   We can use the scaler to calc the position of the object over it's life span.    So they'll each reach their destination at the same time regardless of how fast the computer is running the program.  



PlayBASIC Code: [Select]
;   setfps 50


Type tObject

StartTime ; When the object was born
Duration ; The Life span in milliseconds on the object
EndTime ; When we expect this object to be dead

StartingX ; Starting X & Y cordinate
StartingY

EndingX
EndingY

Size ; The size of the object

Colour ; The COlour of this object

EndType

; create the list for the ball objects
Dim Ball as tObject List


; ------------------------------------------------------------
; >> Programs Main Loop <<
; ------------------------------------------------------------
Do

cls


CurrentTime=Timer()

if NExtAdd<CurrentTime
if Rnd(100)>90
; the length of time it taks the object to move between the two points
for lp=0 to rnd(5)
Duration= 1000*rnd(10)
X1=rnd(800)
y1=rnd(600)
X2=rnd(800)
y2=rnd(600)
AddBall(X1,y1,x2,y2,Duration,CurrentTime)
next
endif

; dont allow new objects to be added for at least 20 milliseconds (50th
; of a second
NExtAdd=CurrentTime+20
endif

lockbuffer
for each Ball()

if CurrentTime<Ball.EndTime
if CurrentTime>=Ball.StartTime
; Calc how much time has past
TimePast#=CurrentTime-Ball.StartTime

; Convert the the time past, into a scaler the represent
; how far along this object has moved since it was born.
Scaler#=TimePast#/Ball.Duration

; get the difference between the start and end positions
; and scale them by the time past scaler
CurrentX=(Ball.EndingX-Ball.StartingX)*Scaler#
CurrentY=(Ball.EndingY-Ball.StartingY)*Scaler#

; add on the objects starting position
CurrentX+=Ball.StartingX
CurrentY+=Ball.StartingY

Size#=Ball.Size*(1-Scaler#)

; draw it
Circlec CurrentX,CurrentY,Size#,true,Ball.COlour
endif

else
; This Ball is now dead
Ball= Null

endif

next
unlockbuffer




; display the FPS
Print Fps()

Sync

loop



Function AddBall(X1,y1,x2,y2,Duration,CurrentTime)

Ball = New Tobject
Ball.StartTime =CurrentTime
Ball.Duration =Duration
Ball.EndTime =CurrentTime+Duration

Ball.StartingX=x1
Ball.StartingY=Y1

Ball.EndingX=x2
Ball.EndingY=Y2
Ball.Colour=rndrgb()

Ball.Size = rndrange(10,30)

EndFunction







 Pig Tails

 Hold space to see pig tails.   Here we're plotting a circular movement while it moves between points.  



PlayBASIC Code: [Select]
;   setfps 20


Type tObject

StartTime ; When the object was born
Duration ; The Life span in milliseconds on the object
EndTime ; When we expect this object to be dead

StartingX ; Starting X & Y cordinate
StartingY

EndingX
EndingY


Angle# ; From start to finish

Size ; The size of the object

Colour ; The COlour of this object

EndType

; create the list for the ball objects
Dim Ball as tObject List


; ------------------------------------------------------------
; >> Programs Main Loop <<
; ------------------------------------------------------------
Do

if Spacekey()=false
cls
endif

CurrentTime=Timer()

if NExtAdd<CurrentTime
if Rnd(100)>90
; the length of time it taks the object to move between the two points
Duration= 1000*rnd(10)
X1=rnd(800)
y1=rnd(600)
X2=rnd(800)
y2=rnd(600)
AddBall(X1,y1,x2,y2,Duration,CurrentTime)
endif

; dont allow new objects to be added for at least 20 milliseconds (50th
; of a second
NExtAdd=CurrentTime+20
endif

lockbuffer
for each Ball()

if CurrentTime<Ball.EndTime
if CurrentTime>=Ball.StartTime
; Calc how much time has past
TimePast#=CurrentTime-Ball.StartTime

; Convert the the time past, into a scaler the represent
; how far along this object has moved since it was born.
Scaler#=TimePast#/Ball.Duration

; get the difference between the start and end positions
; and scale them by the time past scaler
CurrentX=(Ball.EndingX-Ball.StartingX)*Scaler#
CurrentY=(Ball.EndingY-Ball.StartingY)*Scaler#


; Get Angle 90 degrees from the direction we've moving in
Angle#=wrapangle(Ball.Angle#+90)

; Over the duraiton of the objects life, we want to to
; turn through 360 degrees 5 times
Angle#+=(360*5)*Scaler#

; Calc it's size at this time
Size#=Ball.Size*(1-Scaler#)

; add on the objects starting position
CurrentX+=Ball.StartingX+CosRadius(Angle#,size#*5)
CurrentY+=Ball.StartingY+SinRadius(Angle#,size#*5)

; draw it
Circlec CurrentX,CurrentY,Size#,true,Ball.COlour

endif

else
; This Ball is now dead
Ball= Null

endif

next
unlockbuffer




; display the FPS
Text 0,0,Fps()
text 0,30,"Hold space"
Sync

loop



Function AddBall(X1,y1,x2,y2,Duration,CurrentTime)

Ball = New Tobject
Ball.StartTime =CurrentTime
Ball.Duration =Duration
Ball.EndTime =CurrentTime+Duration

Ball.StartingX=x1
Ball.StartingY=Y1

Ball.EndingX=x2
Ball.EndingY=Y2

Ball.Angle#=getangle2d(x1,y1,x2,y2)

Ball.Colour=rndrgb()

Ball.Size = rndrange(10,30)

EndFunction