Game Development Reference
In-Depth Information
The previous example creates two circles that are overlapping, the first one has an
alpha value of 1, which means it is opaque, and the second sprite ( s1 ) has an alpha
value of 0.5, which is 50 percent transparent. s1 is placed on the stage so that it
overlaps the first sprite. Here is what the stage looks like when you run the
previous example:
Cool fading screens
Now that you know how to control the transparency in a sprite, how can we do that
with an arbitrary image? Well, we will go a step further. Let us load an arbitrary
image and have it fade out. In order to create transparency for an image, one way
is to add a filter to the sprite representing the image. If we then add a timer and for
each timer event callback we tweaked the alpha value by a delta starting from 1
(fully opaque) to 0(fully transparent), we can achieve the fading-out effect. A fade-in
effect can be similarly achieved by updating the alpha value from 0 to 1.
Fading images are not only useful for splash screens, but also finds its uses in game
playing. For example, when the player kills an enemy such as a monster, you can
either simply remove the dead enemy by removing the sprite ( removeChild ) or you
can fade it out and then remove the sprite, increasing your general visual appeal for
your game.
The following is a generic class that takes in a bunch of sprites; you could also call it
with just one sprite that you want to dissolve. When the sprite's transparency reaches
zero, the sprites are automatically removed from the parent and that is the reason
the constructor of the class takes in the parent as one of the parameter. Lastly, it also
throws an event when the sprites have dissolved and removed from its parent.
Let us examine the various parts of this class now.
In order to generate an event from the class, we will have it inherit from
EventsDispatcher , as shown next:
public class SpriteDissolve extends EventDispatcher
We define the start and end values of alpha, which are essentially 1 and 0,
respectively. We also allow for some flexibility of how fast the sprites should be
dissolved—this is controlled by the steps' ( m_maxSteps ) private variable.
 
Search WWH ::




Custom Search