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
}
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).
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):
var excerpt_str:String = phrase_str.substr(2, 3);
var found:Boolean = (main_str.indexOf('abc') >= 0);
var posn:Number = phrase_str.indexOf(findme_str);
var phraselen:Number = phrase_str.length;
var citystate_str:String = city_str + ", " + state_str;
word_str = word_str.toLowerCase();
citystate_str = citystate_str + " " + zipcode_str;or, abbreviated:
citystate_str += " " + zipcode_str;
var addr_str:String = "402 Main Street";
addr_str = addr_str.split("Main").join("18th");
Discussed on this page:
number, boolean, true/false, string, String methods, find string, concatenate string, substring, replace one string with another