First time here? First time here? Newsletter Signup Newsletter
651326 Members and growing! | Flash Jobs
Search tutorials
Author: Pascal ECHEMANN | Website: http://www.flashapi.org/
'

Page 3

We don't need anymore to create controls which are internally accessible. We will just create a Button control to convert the temperature, and the label which indicates that the temperature unit of the input field is celsius.

var _btn = new Button("Convert", _input.width);
var celsiusLabel:Label = new Label("celsius");

All controls are built now. To add them at one time into the Popup container we use the addGraphicElements method. The following line of code illustrates this process:

addGraphicElements(_input, celsiusLabel, _btn, _result);

Well, the widget is not so bad, but it is not interactive yet. We must implement a function to convert the temperature. Let's call it convertTemp. The algorithm of the convertTemp function is quite simple. When the user clicks the "convert" button, the program reads the input field value as a Number, and writes the converted value into the result label field.

To add mouse actions, we use the UIMouseEvent class: import org.flashapi.swing.event.*;

The event model of SPAS 3.0 is exactly the same as the AS3 event model. But it is better to use the SPAS 3.0 integrated tools to manipulate events, than to use an other system. SPAS 3.0 has its own event sub-system to prevent leak of memory due to event management. That's why we use the eventCollector property of the Popup class to register the button event:

eventCollector.addEvent(_btn, UIMouseEvent.CLICK, convertTemp);

When the user clicks the button, it will call the convertTemp function which is based on the algorithm described above:

private function convertTemp(event:UIMouseEvent):void {
	var temp:Number = Number(_input.label);
	_result.label = String(temp * 1.8 + 32) + " fahrenheit";
}

Well, we should have a 28 lines of code class. If not, here is the complete CelsiusConverter class:

package {
import org.flashapi.swing.*;
import org.flashapi.swing.event.*;
public class CelsiusConverter extends Popup {
public function CelsiusConverter() {
super("Convert Celsius to Fahrenheit", 250);
initialize();
}
private var _result:Label;
private var _input:TextInput;
private function initialize():void {
autoHeight = true;
padding = horizontalGap = verticalGap = 10;
_input = new TextInput("0");
_input.maxChars = 5;
_input.restrict = "0-9";
_result = new Label("fahrenheit");
var _btn = new Button("Convert", _input.width);
var celsiusLabel:Label = new Label("celsius");
addGraphicElements(_input, celsiusLabel, _btn, _result);
eventCollector.addEvent(_btn, UIMouseEvent.CLICK, convertTemp);
}
private function convertTemp(event:UIMouseEvent):void {
var temp:Number = Number(_input.label);
_result.label = String(temp * 1.8 + 32) + " fahrenheit";
}
}
}
» Level Intermediate

Added: 2008-12-11
Rating: 1 Votes: 1
(10 being the highest)
» Author
Pascal Echemann is a Web Developer and Project Manager for "Bananatree Design" on the French Riviera. He also is the creator of the "Swing Package for ActionScript 3.0" (SPAS 3.0).
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.