Chapter 6. More Flex Applications

This chapter will show some sample Flex applications to demonstrate various aspects of Flex development. You can use these examples as templates for your own applications.

A Runner’s Calculator

Anyone who has taken up running as an exercise knows the pain of pushing to go too far too fast. It’s commonly held wisdom that you should start at 1 mile and then go 10% farther every week. So, if you want to run 3.1 miles (5K) you should be training for 12 weeks.

To help calculate that number I’ve come up with a handy helper application written in Flex. The first draft of the code is shown in Example 6-1.

Example 6-1. Runner.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
  layout="absolute"
 creationComplete="onMilesChange(event)">
<mx:Script>
<![CDATA[
private function onMilesChange( event:Event ) : void {
 var miles:Number = parseFloat( txtMiles.text );
 var mile:Number = 1.0;
 var weeks:int = 0;
 while( mile < miles ) {
   weeks += 1;
   mile *= 1.1;
 }
 txtWeeks.text = weeks.toString();
}
]]>
</mx:Script>
 <mx:Form>
   <mx:FormItem label="Target Miles">
     <mx:TextInput id="txtMiles" change="onMilesChange(event)" 
       text="3.1" />
   </mx:FormItem>
   <mx:FormItem label="Weeks">
     <mx:Label id="txtWeeks" />
   </mx:FormItem>
 </mx:Form>
</mx:Application>

The application is split into two pieces. The calculation function, onMilesChange, is located at the top of the script. The user interface is a form containing a text input for the number ...

Get Getting Started with Flex 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.