O'Reilly logo

Getting Started with Flex 3 by Emily Kim, Jack D. Herrington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required