Spark LineGrid Component

Since I had a day off, I decided to work on converting my old Flex Examples to the new Flex 4.  As stated in a previous post, the new Flex 4 breaks Degrafa and Degrafa seems to be defunct now.  Most of the drawing functions are handled with spark and sprite functions, but little things like arcs and line repeaters are not.  So where before it was easy enough to create a repeating line in Degrafa, now I have to do a loop to create the lines needed.  Well, in this case, instead of drawing lines, I’m drawing rectangles.  While it might seem simple at first just to draw a bunch of rectangles together to create a grid, there is a little trickiness to it.  You can have to account for the size of the total grid, based on the number of columns, rows and the grid box size.  And you also have to use both the column and row count value to draw the correct number of rows and columns and at the right size.  It is all just a matter of thinking about it for a few moments and then having an “ah ha!” moment.  Another little quirk with the components is that when you add a visual element, it puts it in the center of the group component.  On the Egg Timer, I just left it there for now because it worked for what I needed.  I might change it later.  But for the LineGrid, it made for it not to line up correctly.  So, before I start drawing the rectangles, I had to determine the actual 0,0 location first.  It also took a couple tries to make sure I was using the right zero.


// Find the actual 0,0 start
var actualZeroX:int = -1 * ((columnCount * boxSize) * .5);
var actualZeroY:int = -1 * ((rowCount * boxSize) * .5);

One good thing about the new components, is that when you use the declarations, it is the same as creating a set function, which means less code.  I simply added declarations for the column and row counts, then put those values where I need them.  When I use the LineGrid component I add the values I need just like any other attribute.


<components:LineGrid id="lineGrid1" rowCount="5" columnCount="5" boxSize="50" lineThickness="4" lineColor="0x009900" />

Here is the example showing a grid with equal columns and rows and then two other grids with column or rows determined by the size of the application.

Get Adobe Flash player

New sprite Egg Timer

At work I upgraded to Flex 4.5 and started updating one of my projects.  I started running into errors with my components that used Degrafa.  Apparently the Degrafa group has stopped working on updates and code in Flex 4 has broken the arc and wedge drawing utilities.  This causes a problem with my Egg Timer component, because it relies on the wedge or arc for the display.  I decided to use the new functions of Flex to create the timer.  Unfortunately, Flex still doesn’t have it’s own wedge or arc drawing utilities.  It does have a curveTo method as part of the Graphics function, but you still have to figure out what point you want to curve to what point.  I initially thought I could just use the Shape function to draw a triangle and an ellipse, join the two together and create a wedge.  However, that was proving to be too much of a pain to do.  Then I found Lee Brimelow’s Action Script Wedge Class (Google Project Code Link).  This made things simple, except for the part where the starting point is at 90° instead of 0.  For me, this wasn’t a big issue, because all I had to do was rotate the Sprite back 90 degrees to put it at the 0 position.  A poster provided other code to fix the issue, but since it was fairly simple to fix, I didn’t bother using the other code.

var timerArc:Sprite = new Sprite();
timerArc.graphics.beginFill(0x000000);
timerArc.graphics.endFill();
timerArc.rotation = -90;
drawWedge(timerArc, 0, 0, timerSize, 360, 0);

Another little quirk with Flex 4 is the Declarations tag. Not completely familiar with everything new in Flex 4, I’m trying my best to try out new features. I figured adding the time defaults to the declarations would help things out.


<fx:Declarations>
<fx:Number id="oneMinute">60</fx:Number>
</fx:Declarations>

Well, not really.  After I had everything working with the wedges and the look of the timer, the Timer function was never reaching the TIMER_COMPLETE function.  After searching the web and trying to figure out why the Timer didn’t work the correct way anymore, I gave up using the Declarations area and moved the variables to the script tag area.  That fixed the issue.


// minutes in seconds
private var oneMinute:Number = 60;
private var twoMinute:Number = 120;
private var fiveMinute:Number = 300;
// timers with delay of 1 second, repeat of minute value
private var oneMinuteTimer:Timer = new Timer(1000, oneMinute);
private var twoMinuteTimer:Timer = new Timer(1000, twoMinute);
private var fiveMinuteTimer:Timer = new Timer(1000, fiveMinute);

To change the size of the timer, just use scaleX and scaleY on the component.  The default size is 50 x 50, which is a good general size, but I can see that there might be a need to make it larger or smaller.

Later I will probably add the multi-color option that shows green when things are still good, yellow when it is close to expiring and then red in the last 5 seconds.  I will also try working skins to see how I can use them with the Sprites.

Get Adobe Flash player