module Screen class << self attr_accessor :sprite end def self.start_flash(color, duration) @sprite = Sprite.new @sprite.z = 999 @sprite.bitmap = Bitmap.new(Graphics.width, Graphics.height) @sprite.bitmap.fill_rect(@sprite.bitmap.rect, color) class << @sprite attr_writer :duration def update_opacity @time ||= 0 self.opacity = (self.opacity * (@duration - @time - 1)) / (@duration - @time) @time += 1 end def showed? @time == @duration - 1 || self.opacity == 0 end end @sprite.duration = duration end end class << Graphics alias update_for_screen_flash update def update update_for_screen_flash if (sprite = Screen.sprite) sprite.update_opacity Screen.sprite = nil if sprite.showed? end end end
module Screen
class << self
attr_accessor :sprite
end
def self.start_flash(color, duration)
@sprite = Sprite.new
@sprite.z = 999
@sprite.bitmap = Bitmap.new(Graphics.width, Graphics.height)
@sprite.bitmap.fill_rect(@sprite.bitmap.rect, color)
class << @sprite
attr_writer :duration
def update_opacity
@time ||= 0
self.opacity = (self.opacity * (@duration - @time - 1)) / (@duration - @time)
@time += 1
end
def showed?
@time == @duration - 1 || self.opacity == 0
end
end
@sprite.duration = duration
end
end
class << Graphics
alias update_for_screen_flash update
def update
update_for_screen_flash
if (sprite = Screen.sprite)
sprite.update_opacity
Screen.sprite = nil if sprite.showed?
end
end
end
比较简单粗暴的实现方式
要使用时用Screen.start_flash(color, duration)
一个使用范例:
class Scene_Menu alias start_for_screen start def start start_for_screen Screen.start_flash(Color.new(255,0,0), 60) end end
class Scene_Menu
alias start_for_screen start
def start
start_for_screen
Screen.start_flash(Color.new(255,0,0), 60)
end
end
效果:
|