Home > RGSS Script Reference > Sprite_Timer
Sprite_Timer
Inherits from: Sprite
Description: The class updates the timer graphic.
class Sprite_Timer < RPG::Sprite
# ------------------------------------
def initialize
super
self.bitmap = Bitmap.new(88, 48)
self.bitmap.font.name = "Arial"
self.bitmap.font.size = 32
self.x = 640 - self.bitmap.width
self.y = 0
self.z = 500
update
end
# ------------------------------------
def dispose
if self.bitmap != nil
self.bitmap.dispose
end
super
end
# ------------------------------------
def update
super
self.visible = ($game_system.timer_working and $game_system.timer > 0)
if $game_system.timer / Graphics.frame_rate != @total_sec
self.bitmap.clear
@total_sec = $game_system.timer / Graphics.frame_rate
min = @total_sec / 60
sec = @total_sec % 60
text = sprintf("%02d:%02d", min, sec)
self.bitmap.font.color.set(255, 255, 255)
self.bitmap.draw_text(self.bitmap.rect, text, 1)
end
end
end
|
Total_Sec: The total number of seconds remaining on the timer.
Initialize
Arguments: None
Local Variables: None
How it Works: This class sets up the timer's font, location, and Z-index.
Dispose
Arguments: None
Local Variables: None
How it Works: This method is the garbage collection method for this sprite. If the sprite's bitmap isn't nil, then it disposes of that bitmap. It then calls Sprite#Dispose in order to do further garbage collection.
Update
Arguments: None
Local Variables: None
How it Works: This method updates the timer's graphic every frame. First, the call to super handles the frame update processing common to all sprites. The timer is then set to be visible if the value of game_system.timer_working is true and the amont of time on the timer is greater than 0 seconds. The if $game_system.timer / Graphics.frame_rate != @total_sec clause adjusts amount of time shown on the timer based on the frame rate. If the timer needs to be decremented by a second, or the frame rate has changed, then the timer's bitmap is cleared and the value of @total_sec is changed to the value of $game_system.timer (the number of frames remaining on the timer) divided by the frame rate. This value is then divided into minutes and seconds and drawn on the screen.
|
|