Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Fading a graphic in and out

That´s the normal way i do that kind of effect (please read all carefully):
Code:
# 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.

For example, copy and paste all that code in any script above main and below Scene_Map, and make three events on the map, that call those commands:
Event 1
Code:
$scene.fade = -1
Event 2
Code:
$scene.fade = 0
Event 3
Code:
$scene.fade = 1
And try to play wit them for a while ^^ You can use this concept in any kind of script that has a constant update.

I hope you got it...
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top