Figure 12. A numerical representation of a T20 snowball de longueur with dynamic changes to x, y, and rotation properties of the textfields

3.iii. Snowball #2

Code for Snowball_2.as

1   package {
2    
3      import flash.display.Sprite;
4      import flash.text.TextField;
5      import flash.text.TextFormat;
6    
7      public class Snowball_1 extends Sprite {
8    
9         public function Snowball_1() {
10            var odo:int = 1;
11            var mag:int = 20;
12            var lf:Calibri = new Calibri();
13            var tf:TextFormat = new TextFormat();
14               tf.font = lf.fontName;
15               tf.align = "center";
16               tf.size = 10;
17               tf.color = 0x000000;
18    
19            for(var r:int = 0; r <= mag; r++) {
20               for(var c:int = 0; c < r; c++) {
21                  var t:TextField = new TextField();
22                  t.defaultTextFormat = tf;
23                  t.width = t.height = 19;
24                  t.embedFonts = true;
25                  t.text = odo.toString();
26    
27                  t.x = (1 + odo) + ((t.width + 5) * c);
28                  t.y = (1 - odo) + ((t.height + 5) * r);
29                  t.rotation = r * c * Math.random();
30    
31                  addChild(t);
32                  odo++;
33               }
34            }
35         }
36      }
37   }


Explanation of the Code

Although an on-screen comparison of Snowballs 1 and 2 seems dramatically different, the differences in the code are minimal. In the second snowball, changes have been made to three lines of code, and one additional line code has been added (Line 29). The first change is to Line 11. The value of mag has been raised to 20, which means that the snowball will be double in size; the magnitude of the snowball has been doubled.

11            var mag:int = 20;

Instead of 10 rows, there are 20, and instead of 55 numbers/textfields, now there are 210. The second and third changes are to Lines 27-28, which define the location of each textfield on the screen.

Snowball #1:
27                  t.x = 1 + ((t.width + 5) * c);
28                  t.y = 1 + ((t.height + 5) * r);

Snowball #2:
27                  t.x = (1 + odo) + ((t.width + 5) * c);
28                  t.y = (1 - odo) + ((t.height + 5) * r);

In Snowball 2, odo was added to the x or horizontal value in Line 27, which means that each textfield displayed on the screen will have “drifted” over one additional pixel to the right with each iteration of the second for loop. In Line 28, the vertical height of the snowball is reduced by a similar degree. In other words, as the rows are placed on the screen, the distance between them is reduced as the value of odo rises. If you study Figure 12, you’ll notice that some of the last rows (i.e, rows 15 – 20) begin to rise above the rows preceding them. A curling effect is generated by this simple change to Line 28.


29                  t.rotation = r * c * Math.random();

The last change is the addition of Line 29, which adds rotation to each of the textfields displayed on the screen. One again, the numerical values used to determine the value of the rotation of each textfield is generated from variables already in use in the program. The Math.random() method adds an aleatory dimension to the textfields on the screen.