- alarms are one of the primary ways of event handling (the other being testing conditions like collisions yourself)
- they can be used to set a timer in your game or to cause certain things to happen (like release a new enemy every 5 seconds for example)
- To make a tracker you need to create a new class inside of your game's regular class. This is what is known as an inner or nested class. Often you don't want to use them but for Alarms it is the simplest way. To make a new Alarm add the following code somewhere inside your regular class, just like you would place a method.
private class MyAlarm implements Alarm
{
public void alarm( )
{
mySprite.setVisible( !mySprite.isVisible( ) ) ;
}
}
When making an Alarm you must always write the alarm( ) method. This method is where you want to put the code that will be executed when the alarm goes off. In this case, it will set the visibility of mySprite (which has to be a field instance of your regular class) to be the opposite of its current visibility. This makes an Alarm data type available, but never actually creates one. To create one and set it to go off you need to place the following in your code wherever you want to start the Alarm at.
scheduleRelative( new MyAlarm( ), time ) ;
The time can be any number or numeric variable. For instance, if you put 5 there the new Alarm will go off after 5 seconds. The first parameter must be an alarm, we are just creating one inline by saying "new MyAlarm( )". Alternatively you could make a MyAlarm variable, assign it a new MyAlarm( ), and then pass that variable into scheduleRelative, if you prefer.
- What if you want the Alarm to set a specified sprite to be visible/invisible, not mySprite all the time. Then you would need to add more code to your MyAlarm class.
private class MyAlarm implements Alarm
{
private Sprite changeSprite ;
public MyAlarm( Sprite s )
{
changeSprite = s ;
}
public void alarm( )
{
changeSprite.setVisible( !changeSprite.isVisible( ) ) ;
}
}
There are three additions here. The first is to make a field instance called changeSprite that becomes the sprite that has its visibility changed. The second is adding a constructor, which takes a String as a parameter and assigns it to changeSprite. Remember that s and changeSprite are objects, so when you assing s to changeSprite you are really only assigning s's memory reference to changeSprite. So, after that line they are both pointing to the same Sprite sitting out in memory. The final change is alarm( ) is updated to use changeSprite instead of mySprite. Since changeSprite points to the same place as the Sprite you passed in on making the alarm, it will make that Sprite visibile or invisible when the alarm goes off. To start this new alarm we must update our scheduleRelative call to pass in a Sprite to the constructor.
scheduleRelative( new MyAlarm( whateverSpriteYouWant ), time ) ;
- Finally, you can tell an alarm to reschedule itself. If we wanted changeSprite to blink instead of just turn off or on once we would need to do this. Only the alarm( ) method would need to be changed.
public void alarm( )
{
changeSprite.setVisible( !changeSprite.isVisible( ) ) ;
scheduleRelative( this, 5 ) ;
}
This new scheduleRelative sets up an alarm for "this", which refers to the current alarm going off, to go off in 5 seconds. So, the alarm reschedules itself to go off again in 5sec.