We present a simple custom AS3 class, SnowFlake, and an applet that creates snowflakes from a drawing generated by the user. The code combines techniques from our earlier tutorials: Kaleidoscopic Gallery in Flash CS4 and The New drawPath Method in Flash CS4. Click on the image below to open the applet in a new window.
- Download all source fla and as files corresponding to this effect: flakes.zip
The 'fla' file corresponding to the example above is flakes.fla. The simple, well-commented code on the MainTimeline consists of creating the small board with draggable red dots. The small board is a Sprite. After the user clicks the READY button, the content of the small board is drawn into a BitmapData object. This BitmapData is passed to the constructor of our custom AS3 class, SnowFlake. The class does the rest. You can easily customize all the elements of the applet.
The New Methods in Flash Player 10 Used
In our snowflake generator we use some of the new AS3 methods available in FP10. These are: Graphics.copyFrom, Graphics.drawPath, and the 'winding' parameter of the latter method. See our tutorial The New drawPath Method in Flash CS4 for a detailed discussion of the 'drawPath' method and its 'winding' parameter. The use of Graphics.copyFrom is highlighted in the comments within SnowFlake.as.
The SnowFlake Class - Public Methods
The SnowFlake class has the following public methods.
The constructor evoked with the key word 'new':
- new SnowFlake (bd: BitmapData, r: Number, n: Number=20)
The constructor takes two parameters plus one optional parameter. The first parameter is a BitmapData object that will provide the underlying image for creating snowflakes thorugh a series of symmetries. In our applet, 'bd' is created by drawing the content of the small board, 'prepBoard', in to a BitmapData object via BitmapData.draw method. 'prepBoard' is a Sprite. Drawing it into a BitmapData objects creates a 'snapshot' of it. The second parameter is the radius, in pixels, of the instance created. The radius does not have to be related to the dimensions of the BitmapData, bd. The last, optional, parameter sets the number of wedges in your snowflake. The default value: 20. In the applet we use 12 wedges.
- doSpin( ): void
The public 'doSpin' method redraws the snowflake. Since the matrix for the beginBitmapFill call in the function that does the drawing contains random parameters, the image will change each time the 'doSpin' method is called.
In addition, the SnowFlake class has a public 'destroy( )' method:
- destroy( ): void
You should call the 'destroy' if you want to delete an instance of the class at runtime. In our applet we use it repeatedly, each time the user resets the applet to create a new drawing on the small board.