March 05, 2021, 07:20:29 AM

Author Topic: [bb] Multiple Event triggers by ralphy [ 1+ years ago ]  (Read 465 times)

Offline BlitzBot

[bb] Multiple Event triggers by ralphy [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Multiple Event triggers
Author : ralphy
Posted : 1+ years ago

Description : Handle multiple events without halting program flow

Code :
Code: BlitzBasic
  1. ;Multiple Event Trigger demo
  2. ;Useful for handling frame animation for multiple objects instead of using
  3. ;the delay or timer functions (they halt program flow).
  4.  
  5. ;Michael Ralph (ralphy)
  6.  
  7. Graphics 640,480,16,2
  8. SetBuffer BackBuffer()
  9.  
  10. ;Instead of Globals you could set up your event as a Type collection
  11. ;Type Event
  12. ;       Field eID
  13. ;       Field clockDelay
  14. ;       Field lastclock
  15. ;       Field status
  16. ;End Type
  17.  
  18. Global event_status = 0
  19. Global event_lastclock = 0
  20. Global event2_status = 0
  21. Global event2_lastclock = 0
  22. Global event3_status = 0
  23. Global event3_lastclock = 0
  24.  
  25.  
  26. While KeyHit(1) = 0
  27.  
  28.         FrameTimer() ; called only once every display update
  29.        
  30.         If event_status = 1 Then
  31.                 Text 0,y, "Event 1 fired"
  32.                 y=y+10
  33.         EndIf
  34.        
  35.         If event2_status = 1 Then
  36.                 Text 200,y2, "Event 2 fired"
  37.                 y2=y2+10
  38.         EndIf
  39.        
  40.         If event3_status = 1 Then
  41.                 Text 400,y3, "Event 3 fired"
  42.                 y3=y3+10
  43.         EndIf  
  44.        
  45.         ;etc....
  46.        
  47.         Flip
  48. Wend
  49. End
  50.  
  51.  
  52. Function FrameTimer()
  53.  
  54.         event_status = 0
  55.         event2_status = 0
  56.         event3_status = 0      
  57.        
  58.         iclock = MilliSecs()
  59.        
  60. ; you could iterate your event collection instead of the following...
  61.         If iclock > event_lastclock + 1000 Then
  62.                 event_lastclock = iclock
  63.                 event_status = 1
  64.         EndIf
  65.  
  66.         If iclock > event2_lastclock + 500 Then
  67.                 event2_lastclock = iclock
  68.                 event2_status = 1
  69.         EndIf
  70.        
  71.         If iclock > event3_lastclock + 300 Then
  72.                 event3_lastclock = iclock
  73.                 event3_status = 1
  74.         EndIf
  75.                
  76.                
  77. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal