|The other error messages happened because we removed the snowfall function from within the snowflake function. Because of this, the snowfall function does not know how to recognize the snowflakes. We need to make a change to the snowfall function code to tell it to recognize the current snowflake that has just been created. We will change "_Snowflake" to "event.target" in the four places that it occurs.|
We have one more correction before we can test our movie again. Let's go back to the snowflake function code. The Event Listener inside this code calls the snowfall function. But it is still listening to the stage. We need to change this so that the Event Listener is attached to each new snowflake as it is created, and not to each new frame of the animation. At the front of the Event Listener code, change "stage" to "_Snowflake".
Test again. No errors and we have all 70 snowflakes falling to the bottom of the stage. But then the animation stops because we are not creating any new snowflakes after the initial 70 snowflakes. We need to add some code that will recycle these 70 snowflakes back up to the top of the stage. As we move these snowflakes back to the top, let's use the same Math.random() code that we used in the snowflake function to randomly place our recycled snowflakes. But this time we need to use the "event.target" instead of "_Snowflake".
Finally, we need to remove the removeEventListener code from inside the snowfall function. We do not want to stop our snowflakes from falling anymore. We want them to go to the top and fall down again. Here is the new code for the conditional statement.
Test again and you will see 70 snowflakes fall from the top to the stage followed by the recycled snowflakes. But there are a few seconds before the recycled snowflakes begin to fall. It would be a much smoother animation if we could change the code so that the initial 70 snowflakes are place over the entire stage instead of just at the top. Let's go back to the snowflake function and change the "40" to "400" for the_Snowflake.y property. This will place the 70 initial snowflakes at random positions from 0 to 400 along the Y axis which will cover the entire stage.
Test again and we have success. Save your snow scene as SnowScene10.fla.
Now we have our animation optimized and animating perfectly, we could stop here. But with just a few changes, we could make this code a more modular particle system. Then we could use the code to make anything from rain to pigs fall from the sky. We will do that next.