# Suppose we have in a scene a sprite that may appear
# and disappear at some conditions, defined in the screen. First, let´s create
# the sprite:
class Scene_Map
attr_accessor :fade
alias old_main main
def main
@sprite = Sprite.new
@sprite.z = 9999
@sprite.opacity = 0
@sprite.bitmap = RPG::Cache.battler("001-Fighter01", 0)
# Set to start with a fade in effect
@fade = 1
@timing = Graphics.frame_rate
@max_opacity = 255
@min_opacity = 0
old_main
@sprite.dispose
end
end
#-----------------------------------------------------------------------
# @sprite is your actual sprite that will be shown in the screen.
# @fade is the variable that will be holding the method the sprite
# will fade
# - a value of 0 means no fading method
# - a value of 1 means a Fade In
# - a value of -1 means a Fade Out
# @timing is in frames and defines the time the fade effects will last.
# In our case it´s set to 1 second, so to make it 2 seconds, do
# @timing = Graphics.frame_rate * 2, or even a half second,
# @timing = Graphics.frame_rate / 2. You say how many time you want.
# You can even put directly a defined number of frames, it´s up to you.
# @min_opacity is the minimum opacity the sprite can have
# @max_opacity is the maximum opacity the sprite can have
#------------------------------------------------------------------------
# Now that the variables have been defined, let´s go to the real
# fade effect
class Scene_Map
alias old_update update
def update
old_update
@sprite.update # No real need to update it, but let´s follow the rules ^^
# This is the total opacity variation for the sprite
opacity_range = @max_opacity - @min_opacity
case @fade
when -1
# The Fade Out Effect
@sprite.opacity -= opacity_range / @timing if @sprite.opacity > @min_opacity
@fade = 0 if @sprite.opacity <= @min_opacity
when 1
# The Fade In Effect
@sprite.opacity += opacity_range / @timing if @sprite.opacity < @max_opacity
@fade = 0 if @sprite.opacity >= @max_opacity
end
end
end
# In this example, if the @fade variable is set to 0 it does nothing,
# because no fade effect was selected. If @fade is 1, it does the
# Fade In effect, and in that case it adds a constant
# (opacity_range / @timing) to the opacity while it´s less than the
# maximum opacity value (@max_opacity). In case that the
# @sprite.opacity becomes bigger than the max. value, the effect is done,
# and the @fade is set to 0 again. The same happens with the Fade Out
# effect, but in the opposite way.
# In this example you can even cancel or change the effect in any time.