Building a BMI Calculator Application in Flash Lite – Step By Step Walkthrough
Screen 3 Â – “actions” layer frame 3 :
var BMI:Number = 0;
/* Init text inputs */
txtHeightMajor.restrict ="0-9";
txtHeightMinor.restrict ="0-9";
txtWeight.restrict = "0-9 .";
We are now in our final screen. Here we declare out BMI variable to hold the BMI value. Next we prepare out input TextFields for user input.
txtHeightMajor – Holds the major part of the height  scale.
txtHeightMinor - Holds the minor part of the height scale.
txtWeight - Holds the weight.
switch(mode)
{
case "Metric":
lblHeightMinor.text = "(m)";
lblHeightMajor.text = "(cm)";
lblWeight.text = "(kg)";
break;
case "Standard":
lblHeightMinor.text = "(ft)";
lblHeightMajor.text = "(in)";
lblWeight.text = "(lbs)";
break;
}
In the above code we use switch – case  to make preparations for calculating the BMI based on the mode selected by user in the prior screen.
delete softKeys_mc.onSoftKeyDown;
softKeys_mc.onSoftKeyDown = Delegate.create(this,onSoftKeyPress);
softKeys_mc._LSK = "Calculate";
softKeys_mc._MSK = "";
softKeys_mc._RSK = "Back";
softKeys_mc._isLSKEnabled = true;
softKeys_mc._isMSKEnabled = false;
softKeys_mc._isRSKEnabled = true;
Once again we delete the old SoftKeys Handler and create a new one for our calculation screen.
function Calculate()
{
var __weight:Number = parseInt(txtWeight.text);
var __heightMajor:Number = parseInt(txtHeightMajor.text);
var __heightMinor:Number = parseFloat(txtHeightMinor.text);
switch(mode)
{
case "Metric":
var meterToCentimeter:Number = __heightMajor * 100;
var centimeters = meterToCentimeter + __heightMinor;
var meters = centimeters/100;
BMI = __weight / (meters * meters) ;
break;
case "Standard":
var feetToInches:Number = __heightMajor * 12;
var inches = feetToInches + __heightMinor;
BMI = (__weight / (inches * inches)) * 703;
break;
}
txtBMIOutput.text = Math.round(BMI);
}
The above code handles the core logic of our program. Based on the mode selected by user (Standard or Metric) we calculate our BMI using the standard formulas.
| English BMI Formula |
|---|
| BMI = ( Weight in Pounds / ( Height in inches ) x ( Height in inches ) ) x 703 |
| Metric BMI Formula |
| BMI = ( Weight in Kilograms / ( Height in Meters ) x ( Height in Meters ) ) |
Finally we round off the figure and display it to the user.
function onSoftKeyPress(keyPressed:String)
{
switch(keyPressed)
{
case "LSK":
if(txtHeightMajor.text.length >0 && txtHeightMinor.text.length>0 && txtWeight.text.length>0)
Calculate();
break;
case "RSK":
this.gotoAndStop(this.prevFrame());
break;
}
}
The onSoftKeyPress() function:
The onSoftKeyPress handler is triggered with user presses Left/Right Softkey or the Enter key (MSK). We then check which key was pressed and take the appropriate action. Here we handle the Right Soft Key (RSK) and the Left Soft Key (RSK). The Right SoftKey will take user back to Mode Selection Screen, and the Left Soft Key will trigger the function Calculate().
That’s all ! Our Calculator is Ready
Note: You may also choose to display message to user based on their BMI. See more info here:
Downlaod FLA : Flash Lite BMI Calculator
Popularity: 60% [?]
June 6, 2010
Tags: Actionscript 2.0, Flash, Flash Lite, Mobile, Nokia Posted in: Actionscript 2.0, Flash Lite, Mobile, Nokia, Sony Ericsson




Leave a Reply