Using Alarms

Understanding Alarms

When you schedule an alarm to go off after x seconds, FANG monitors the system clock and once that time period has elapsed it calls the alarm( ) method of the alarm that was scheduled. When you call scheduleRelative, the first argument (or parameter) is the particular Alarm instance that will go off.

When you write an Alarm class it is not specific to one instance of an alarm. Say we wanted two different sprites to blink on the screen, once every 5 seconds. You could do this by making the first MyAlarm just set both of them to turn on/off whenever the alarm( ) method gets called. However, you may not always want the blinking to start at the same time. What if you want want your triangle to blink from the start every 5 seconds but your rectangle to blink only after the user clicks? Then you can use the second MyAlarm we wrote and just schedule two different alarms.
  public void startGame( )
  {
    ....
    scheduleRelative( new MyAlarm( triangle ), 5 ) ;
    ....
  }
  ...
  public void advanceFrame( double timePassed )
  {
    ...
    scheduleRelative( new MyAlarm( rectangle ), 5 ) ;
    ...
  }

You may have noticed that we have used 5 seconds for all of our alarms. You can obviously pass in any number you want in the starting scheduleRelative call, but our scheduleRelative( this, 5 ) INSIDE of the MyAlarm class will always be the same number for all alarms of that type. So if you schedule one alarm to go off in 1sec and another to go in 7, once they do those first wait periods, they will repeat themselves every 5 seconds instead of the original time passed in. What is a way to fix this? HINT: It is similar to how we modified MyAlarm to use different sprites rather than the same one all the time.

More on Alarms

To learn more do the following tutorial: FANG tutorial. Make a new project, package, and class then go through the steps. Note that the wiki's code has "package Jam.firstalarm;" and "import wik.Wiki;" up at the top. You need to change the package to whatever package you created and remove the "import wiki.Wiki;" line before your code will work.