Basics

So now comes the fun part, improving the looks of our effect.

You have probably noticed, that the particles are deleted fitfully when they reach the age of 40. It would be much nicer to fade them out smoothly.
Just like sprite entities have a my.alpha attribute, particles also have something similiar: the my_alpha attribute. It defines how transparent the particle is. A value of 0 means totally transparent (invisible), a value of 100 totally solid (0% transparency).

So we can fade out our particles easily, if we set the my_alpha attribute depending on the particle age. We just have to make sure, that my_alpha has a very low value (near 0) when the particle reaches it's terminal age.

In our case, we can achieve this effect very easily if we start the fadeout effect at an age of 20.
My_alpha must have a value of 100 at this age, the value has to decrease in steps of 5 afterwards. that means we would reach the desired value of 0 exactly at the age of 40.

Instead of setting the my_alpha value and really decreasing it by five each frame, we can also use a formula for the fadeout. This makes it easier to change the duration of the fadeout later on. Here is the according formula:

my_alpha = 100 - (my_age - 20) * 5;

We just have to pack it into an if-query, that checks if the particle has already reached the age of 20:

if (my_age > 19) {
    my_alpha = 100 - (my_age - 20) * 5;
}

Insert these lines into our function and test what happens! You should notice a big difference.

The modified files can be found in the Tutorial_templates directory. They are named tut2.wdl and tut2.wmp.

next page >>