Always Get Better

Posts Tagged ‘ActionScript’

atoi in Flex/ActionScript – Converting Strings to Numbers

Saturday, February 7th, 2009

It couldn’t be easier to convert a string to a number in Flex Actionscript. The language doesn’t have (or need) an atoi() function as seen in languages like C – it’s as simple as creating a cast:

[source:JavaScript]
var myString:String = “15”;
var myNum:Number = Number( myString );
[/source]

The framework will convert any data type, not just strings. If it is unable to do so, it will return 0 (for ints) or NaN (for floating point).

Accessing the stage in Flex

Sunday, November 9th, 2008

When creating components in Flex, designers sometimes need to attach events to the main stage. Unless the application has reached creationComplete, the stage property of custom components will be null.

If you need to, for example, attach a MOUSE_MOVE event to the application stage from a component that doesn’t include a creationComplete override, you have two options.

Wait for Event.ADDED_TO_STAGE

[source:jscript]
addEventListener( Event.ADDED_TO_STAGE, function(e:Event):void
{
stage.addEventListener( MouseEvent.MOUSE_MOVE, myMouseMoveEvent );
});
[/source]

Attach Through the System Manager Object

The system manager parents all displayable elements within the Flex application. It is an elegant way of accessing the stage through custom flex components which have not yet been added to it.

[source:jscript]
systemManager.stage.addEventListener( MouseEvent.MOUSE_MOVE, myMouseMoveEvent );
[/source]