AS Reference  :  Notes Index  :  Resources  :  About/Contact  :  Downloads

Simple Data Types

Number

In actionscript, variables of type Number can be positive or negative integer or decimal (floating point) numbers. This is different from many programming languages which have different data types for integer and floating point numbers, and means that one must be careful with numeric comparisons in Flash. For example, when decrementing a movieclip's _alpha property over time to fade the movieclip, the condition for ending the fade should never be:

if (fading_mc._alpha == 0) {    // not a good way to check
   // stop the loop 
}
because this condition may never be met (since _alpha is a floating point value and may not round to integer 0 exactly even if decrements are done by supposedly integer increments like 10). Instead, a comparison should be done, like:
if (fading_mc._alpha <= 0) {    // this is better
   // stop the loop 
}

Boolean

As mentioned above, values that may be assigned to a Boolean value are true or false (with no quotes around either, because that would make them String values), or 1 (interpreted as true) or 0 (interpreted as false).

String

A variable of type String is made up of any combination of alphanumeric characters (a-z, A-Z, 0-9). The value of a string (its sequence of alphanumeric characters) must be enclosed in quotes, either single or double (Flash doesn't care which, though the start and end quote have to be of the same type), when the value is assigned to a variable. One string may be concatenated to another with the "+" operator.

var housenum:String = "1234";
var streetname:String = "Main Street";
var address:String = housenum + " " + streetname;

The concatenation of a String variable with a Number variable will produce a String, which is useful when addressing movieclips or other instance names in a loop, as we'll look at later. The number-to-string conversion is done automatically:

var i:Number = 5;
var mcname:String = "rotator" + i;
trace(mcname);   // rotator5

If you are attempting to add two numeric (but not Number type) String variables, eg, from two input textfields, you must do a conversion of string to number, using the Number function. This is because Flash uses the '+' operator for both addition and concatenation, and will opt for concatenation if either of the operands is a string. eg,

// assume textfield nweeks_txt has 2 in it, textfield ndays_txt has 5 in it
var numDays:Number = nweeks_txt.text*7 + ndays_txt.text;      // wrong 
trace(numDays);   // 145 (unexpected)
numDays = Number(nweeks_txt.text)*7 + Number(ndays_txt.text);  // right
trace(numDays);   // 19  (expected)

Here are some example string operations (string variables are written ending with _str here, which is the ending to use if you want to enable code-hinting):

Intro
Flash: What & How
Example Sites
Create
Draw, Edit Shapes
Gradients
More Drawing Tips
Import
A Sample
Animate
Frames, Keyframes
Motion Tweens
More Motion Tweens
Shape Tweens
Masks
Control
Stop/Replay
Movieclips Intro
Movieclip Reference
Site Structure 1
Slideshow Movieclip
Contact Form
Scroll Resume
Preloader
Site Structure 2
Publish
Display Options
Player Detection
Optimize
AS 2.0 Basics
Intro to Syntax
Playhead Commands
Playhead Cmds 2
Coded Tween
onEnterFrame
Intro to Classes
Declare/Assign
Comments, Trace
Simple Data Types
Arrays & Objects
Code Blocks
Operators
Beyond Buttons
Code Structure
Toggle Controls
Group of Buttons
Drag and Hit
Distort Magnifier
Scroll Text
Bee Game
Dart Shooter
Sound Control
Easing Slider
Easing Slider 2
Components Intro
Timers & Delays
Dynamic Content
Intro
Drawing API
Create Text
Attach Movieclips
Easing Slider 3
Easing Slider 4
Load jpg/swf
Sliding Viewer
Preload swf
XML
Easing Slider 5
Server Comm
LoadVars (w/ PHP)
AS - PHP Lookup
Text File
Database 1:LoadVars
Database 2:Remoting
Read from directory
AS 2.0 Classes
Intro
Math
Key
Date
Color
EventDispatcher
New Samples
Pie Chart
Event-model Emailer
Tween Sequence
Fuse Sequence
SVG in Flash
Bitmap Topo
SWF as Data Holder
Two-level Menu
Yahoo! Flash Maps
Class-based Game
ASTB Samples
Disclaimer
3D Outlines
Bounce Collide
Address Book
Save Drawings
Home  :  Notes Index  :  Resources  :  About/Contact  :  Downloads