Shop OBEX P1 Docs P2 Docs Learn Events
Need help with loop timing / delays — Parallax Forums

Need help with loop timing / delays

Agent420Agent420 Posts: 439
edited 2009-09-23 13:46 in Propeller 1
I'm working on my Prop Invaders app...· I've got a main loop that processes different functions based on time delays so I can somewhat regulate the function timing.· As many methods can require varying cycles to complete, I thought I would base these delays on the system timer rather than simply counting iterations through the main loop.· But I am experiencing some bug issues where my code either hangs for a while or rapidly executes - this seems to be potentially where my references to the system cnt are either wrapping by zero or Spin may be interpreting cnt values > $80000000 as negative.· Either way, I am obviously not handling this correctly.

Here is an example of my main loop operation:

 repeat
    
    if (bonus) AND (cnt > bonus_timer)
      row:=4                                                                    'erase bonus score
      col:=bonus-1
      printstr(string("   "))
      bonus:=0                                                                  'reset bonus flag
 
    if ((bomb_color) AND (cnt > bomb_timer))
      ' Only do if a bomb is onscreen (bomb_color != 0)
      ' AND if bomb_timer time interval has passed (slow down bomb)
      if (move_invader_bomb)
        if (kill_player)                                                        'bomb hit player
          game_over


I have several timer variables for different methods, such as bonus_timer, bomb_timer, missile_timer etc.· They are all reset similarly when their respective method is called by adding a predetermined delay value to the current system cnt:

PRI move_invader_bomb | i, j, tile
  'returns True if bomb hits player base
  bomb_y--                                                                 'move bomb
  bomb_timer := bomb_delay+cnt                                             'reset timer




The idea is that the bomb_timer value can now be used as a reference in the main loop so that the code is only called after a certain period of time has passed.·


Any ideas on how to·better implement this kind of timing?


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


Post Edited (Agent420) : 9/23/2009 11:02:13 AM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-09-23 13:14
    CNT is a 32 bit counter that continuously increments, so it's treated as an unsigned number. All arithmetic in Spin is done with signed numbers though and that's where you're running into problems. You should always do timing by saving the value of CNT somewhere like "bomb_timer := CNT", then test for the passage of time by looking at the difference between the current time and saved time like "if CNT - bomb_timer > bomb_delay". This will avoid problems with the CNT being unsigned, at least for delays up to about 25 seconds (at 80MHz).
  • Agent420Agent420 Posts: 439
    edited 2009-09-23 13:46
    Thanks.· I suspected that was the issue, I just had a mental block trying to work around it.

    Your example seems to have resolved my problem.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sign In or Register to comment.