Bunny

Programming Primer
Licensing And Preface

Standardized Coding Practices

Errors And Exceptions

Variables

String DataType

Number Datatypes

Conditionals

Commonly Used Flash Commands

More Topics:

3D

Music

Processing

Flash



Part 6: Number DataTypes

What are Number Data Types?

Numbers in the programming sense work exactly as the numbers you're already familiar with. In fact programming is largely about the manipulation of numerical values. We get two main types of numbers in programming (although other languages may have more than two number data types)

  • Integers

Integers are whole numbers, that is to say numbers without a decimal value eg 1, 5, 10, 789629386390, 0, -1, -25 etc are all integers

  • Floating Point Numbers

    Also known as floats are numbers with a fractional part that is they will always have a decimal point eg 1.9, 0.74653, -98668.1, 0.0 are all examples of floats.


  • Mathematical Operators

Mathematical operators are used to combine numbers to create new values eg +, -, *, /, % are the mathematical operators available in Flash, for example,


trace(2000 -1000 + 50);

I won't go into detail on the mathematical operators as I'm sure you are already very familiar with them, so in brief “+” plus, used for addition, “-” minus, used for subtraction, “*” times, used for multiplication, “/” divide, used for division. The final mathematical operator you might not be so familiar with. It is known as modulus and represented with this symbol “%”. Modulus is used to return the remainder of an evaluated expression. For example the command,


trace(107%4);


returns a value of 3, which is what is left over after 107 has been divided 26 times by 4

Number Types in Flash

As we already know there are two number data types in Flash, Integers and Floats. We have also touched on the topic of data type conversion. Flash treats all number data types as what it refers to simply as “numbers”. What this means is that flash will actually perform data type conversion between different number data types when necessary for example,


trace(12/5);


evaluates to 2.4. Now. If you've been paying attention you will notice that neither 12 nor 4 are floating point numbers, they are in fact both integers. Since the answer Flash has returned is a float we can assume that Flash has performed data type conversion from an integer to a float on both numbers. Dividing with floats is known as true division. If the answer was returned as an integer it would be 2, and that has got nothing to do with the program performing the calculation, rounding the value of 2.4 down because it does not. To arrive at the integer equivalent of 2.4 your software (Flash) would simply remove the decimal value so 2.4, 2.999, 2.00001 would all equate to 2 as an integer. The process of data type conversion in this case can be quite useful, but something you should be aware of none the less. This is also relevant because it's worth noting that floats on a computer are usually rounded and therefore not exact. In Flash this is particularly relevant when your number's decimal value exceeds 17 digits.

Augmented Assignment Operators

Augmented assignment operators are used to assign a new value to a variable based on it's original value, for example if we had the expression,


x = x + 5;

This is quite a commonly used expression and can become cumbersome to write over and over again. In augmented form this would be written as,


x += 5;

Other mathematical operators can be used in their augmented form in a similar way for example
*= , += , -= , %= , /=

Two other augmented assignment operators exist. The long way of writing out the next two expressions follows,


x = x + 1;

x = x – 1;

The augmented assignment operator versions of these two expressions consecutively read,


x++;

x--;

As you can see using augmented assignment operators make your code easier to read, and can also contribute to make your code run faster.

Random Numbers

Random numbers are used to add an element of unpredictability to a program, for example they can be used to create a change in a computer opponents strategy, spawn enemies in a game at random locations, and create the basis of an AI opponents choices.

Random Numbers in Flash are generated between 0 and 1. Random numbers are generated using the random method of the Math Object. For example,


Math.random();

This will generate a random number and store it memory. This is not very useful because you cannot access the number if you need to use it in an expression. To generate a random number assign it to a variable then use it in an expression you would do something like this,


var randNum:Number = Math.random();

trace(randNum);

It is, however, worth noting that although Flash refers to these numbers as random they are not sophisticatedly random enough to use in programming a secure site, so there goes that dream of an online casino. If you want truly random numbers which are a lot more complicated and slower to create you would need to take into consideration factors such as radio active decay. However for the purposes of what we use Flash for, it is unlikely that you will need such sophisticated technology.

See the Guess my number game for an example of random numbers.
Download guesNumber.zip (11MB Number05.FLA, Number05.SWF, NumberSound01.FLA, NumberSound01.SWF You will need FLASH 8 Pro + to open these files )

Guess My Number


Next: Conditionals (Controlling the Flow of Execution)