Next freebie in 28 Hours*
How do we take that string and let Flash know we actually want it to be a number? The answer is simple: learn a new function. This function type is called Casting (a term we will revisit in the future.)
After seeing the previous video, you might get confused about why we used a number in double quotes, wanting it to behave as string. We sometimes need the number to behave as string when the number is to be attached along with strings. This is rare when working in Flash but becomes very common when working with external teams or back-end code. Most of the time, we don't specify a number as string but it may appear in double quotes or single quotes if it's downloaded from some other source or from the server directly.
In these cases, we have the number as string but we want it to be an integer for addition. In this instance, Flash does some automatic conversion. If you leave the conversion for the application, it will be invisible to you %u2013 you might convert something without wanting to or knowing about it. This is because basically Flash tries to convert everything into strings when one string is added to a number. So to convert the number within quotes into an integer, we make use of the keyword Number. We enclose the number along with the double quotes in a bracket and type Number before it. This converts the data inside the bracket from string to integer. Hence, both the numbers are considered as numbers thereafter and addition of the numbers is performed accordingly.
A simple example will let you know how exactly it's done. If the line from the source is trace(2 '3'), the output is 23 %u2013 but we want it to be the sum of those two numbers. A few modifications are done as said earlier such as trace(2 Number('3')). Therefore, when the program is now compiled, the application adds both numbers and gives the output as 5 which is the preferred answer (mathematically correct) for most users. We therefore recommend not to allow Flash to perform automatic conversions; instead use these various techniques and keywords to make the conversions according to our will.
Have a question but don't want to ask it publicly? If you're a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.
Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.