1 decade ago by Jesse
Make your game screen fade to black (or any other color you choose) with this simple yet versatile screen fading plugin.
It is up to the game code to call
It is safe to call
Options:
fade - the type of fade, can be either
speed - The rate of speed that this fade runs at. It is multiplied against the game time, so a value of
color - An object that stores a color in an "RGB" (Red, Green, Blue) format. The lowercase letter r = Red, g = Green, b = Blue. Values can range from 0 - 255. A value for "a" is optional and it can be used for a transparent color, but "a" is not required. Default value is
screenWidth - Width of the screen used for tiling an image. Default is
screenHeight - Height of the screen used for tiling an image. Default is
tileImagePath - Location of an image to use to tile the screen with. Not required. Example:
tileWidth - Required Width of the tile only if tileImagePath is being used to tile an image
tileHeight - Required Height of the tile only if tileImagePath is being used to tile an image
callback - A function to call when the fade is finished
context - The value of "this" in the callback. The default is the current
waitUntilLoaded - A boolean that indicates to wait for the image at tileImagePath to finish loading, only used if tileImagePath is set and the default is
visible - A boolean that can be set to
delayBefore - A time expressed in seconds to wait before starting the fade effect. Default is
delayAfter - A time exressed in seconds to wait after the fade has completed before calling the
Note that delayBefore and delayAfter are seconds of game time. They use
View the source
ig.ScreenFader
is a class that represents a fade operation. Its constructor takes an object that has the options for the fade, but also has default options that do a fade-to-black type of fade.It is up to the game code to call
draw
on an instance of ig.ScreenFader
. Every time draw
is called, the fade is progressed and the fade effect is applied to the screen by either drawing a semi-transparent rectangle over the entire screen, or by using an image to draw semi-transparent tiles. It is safe to call
draw
inside of your game&039;s #draw
method because the ig.ScreenFader
class is designed so that it is non-intrusive when a game is playing and fast as possible when executing the fade effect. It is faster and easier to use the color
option rather than using tiled images. The default behavior is to use the default color
of solid black./* inside a Game subclass */ init: function() { this.screenFader = new ig.ScreenFader( { /* options go here */ } ); }, draw: function() { this.parent(); if (this.screenFader) { this.screenFader.draw(); } }
Options:
fade - the type of fade, can be either
'out'
or 'in'
, default is 'in'
. Fading in means the color or image starts from invisible and goes to fully solid. Fading out means it starts fully solid and goes to invisible.speed - The rate of speed that this fade runs at. It is multiplied against the game time, so a value of
1.0
is normal, and 2.0
is twice as fast. The default is 1.0
.color - An object that stores a color in an "RGB" (Red, Green, Blue) format. The lowercase letter r = Red, g = Green, b = Blue. Values can range from 0 - 255. A value for "a" is optional and it can be used for a transparent color, but "a" is not required. Default value is
{ r: 0, g: 0, b: 0, a: 1 }
screenWidth - Width of the screen used for tiling an image. Default is
ig.system.width
screenHeight - Height of the screen used for tiling an image. Default is
ig.system.height
tileImagePath - Location of an image to use to tile the screen with. Not required. Example:
'media/screen-tile.png'
tileWidth - Required Width of the tile only if tileImagePath is being used to tile an image
tileHeight - Required Height of the tile only if tileImagePath is being used to tile an image
callback - A function to call when the fade is finished
context - The value of "this" in the callback. The default is the current
ig.game
being played. If you would rather it be the current ig.ScreenFader
instance, change the global setting ig.ScreenFader.globalGameIsContext
to a false
value because the default is true
waitUntilLoaded - A boolean that indicates to wait for the image at tileImagePath to finish loading, only used if tileImagePath is set and the default is
true
visible - A boolean that can be set to
false
to not draw or progress a fade. It makes it frozen and invisible when it is false
, so its default is true
delayBefore - A time expressed in seconds to wait before starting the fade effect. Default is
0
, no wait.delayAfter - A time exressed in seconds to wait after the fade has completed before calling the
callback
function. If you want the screen to stay black for a certain number of seconds and then call the callback
, set this value. The default is 0
, no wait.Note that delayBefore and delayAfter are seconds of game time. They use
ig.Timer
to measure time. If they are zero, then no timer is created.View the source