In: Categories » Computers and technology » Flash » Comparisons and Operations in ActionScript
| Comparing two things in ActionScript is simple. You use standard mathematical symbols, such as =, <, and >. Are the Values the Same?You have already seen how the = symbol is used to assign a value to a variable. To differentiate between times when you want to assign a value and times when you want to compare two values, the double equals symbol, ==, is used when you want to compare two things. The single equals operator is used to assign values to variables. So, if you want to see whether variable a is the number 7, use ==. Here is an example that places the results in the Output window: var a = 7; trace(a == 7); This code assigns the value 7 to the variable a by using the single equals symbols. It then compares a with 7 by using the double equals symbol. When you test this code, the Output window will show "true." If you set a to 8 instead, the Output window will show "false" because 7 is not equal to 8. It is a common mistake, even for experts, to accidentally use a = in the place of a ==. This can lead to a bug that is difficult to find because the difference can easily be missed by the eye. Watch out for this. You can also use the == comparison to compare two strings. The following code compares a variable that contains a string with another string: var myString = "Hello World."; trace(myString == "Hello World."); trace(myString == "hello world."); When you run this program, you get both a "true" and a "false." This is because the first comparison matches the variable to exactly the same string, whereas the second comparison demonstrates that comparisons of strings take case into account. Suppose that you want to test to see whether two values are not equal to each other. In this case, you use the special operator !=, which just means "not equals": var a = 7; trace(a != 9); trace(a != 7); The first trace statement produces a "true" because a is not equal to 9. The second trace statement produces a false because a is indeed equal to 7, but we are trying to test for it not to be 7. Less Than or Greater ThanYou can also compare two things to see whether they are less than or greater than each other. To do this, use the standard mathematical symbols < and >. Here is an example: var a = 7; trace(a < 8); trace(a > 6); trace(a < 1); You should get a "true," "true," and "false" from this program. The variable a contains 7, which is less than 8, greater than 6, but is certainly not less than 1. You can also use the <= or >= comparisons = (greater than or equal to) operator>=) operator>to find out whether a number is less than or equal to, or greater than or equal to, another number. Here is an example: var a = 7; trace(a <= 9); trace(a >= 5); trace(a >= 7); All three of the preceding statements are "true." OperatorsYou can also modify the values of variables with operations. They are also standard mathematical symbols such as + and - for addition and subtraction. For multiplication, we use the * symbol. For division, we use the / symbol. For instance, to add 4 to a variable that contains the number 7, we just use a second assignment statement that sets the value of the variable to its current value, plus 4: var a = 7; a = a + 4; trace(a); The result is 11, of course. ActionScript actually has some shorthand for performing the same addition. The += operation takes the current variable and adds the next number to it. Here is some code that does exactly the same thing as the previous code: var a = 7; a += 4; trace(a); There is another piece of shorthand that you should be familiar with. The ++ operator is like the += operator, but it adds exactly 1 to the number. Here is an example: var a = 7; a++; trace(a); The result is 8. Now try this: var a = 7; trace(a++); trace(a); The result is first a 7 and then an 8. What happened here? Well, the first trace command placed the current value of a in the Output window. Then the ++ operator added one to a. The second trace statement placed the new value in the Output window. Now try this: var a = 7; trace(++a); trace(a); You will get two 8s this time. This is because when you place the ++ operator before the variable, the addition is performed before the command. You can use -- as well as ++ to subtract rather than add. You can also use -= to subtract a number and *= and /= to multiply or divide a variable by a number.
|
legal disclaimer
1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service
Useful tools and features
related articles
Use the Drawing API to create a shape and then use MovicClip.setMask( ) to apply the mask. Masks can be used to create unique shapes or visual effects. For example, you can use masks to create wipes and transitions or interesting animations in which only the masked portion of the artwork is visible at a given time. You can even create masks that change shape over time, and use them to mask bitmapped graphics (in movie clips). You can use any movie clip as a mask of another movie clip using the setMask( ) method. The setMask( ...
2. Drawing a Triangle using ActionScript
Create a custom MovieClip.drawTriangle( ) method using the Drawing API and invoke it on a movie clip. You can determine and plot the vertices of a triangle given the lengths of two sides and the angle between them. This is a better approach than specifying the lengths of the three sides because knowing the lengths of two sides and the angle between them always determines a triangle, whereas three arbitrary sides may not fit together to make a triangle. The custom drawTriangle( ) method accepts six parameters: ab ...
3. ActionScript: Filling a Shape with a Solid or Translucent Color
Use the beginFill( ) and endFill( ) methods to initiate and close a shape drawn at runtime. To draw a filled shape, call beginFill( ) prior to any other drawing methods, including the custom methods you have defined such as drawCircle( ) and drawPolygon( ). Invoke endFill( ) after calling other drawing methods to create the shape. You cannot apply a fill to an existing shape drawn at authoring time or runtime. You must invoke beginFill( ) before drawing the shape to be filled. This example creates a solid blue ...
4. Drawing a rectangle using ActionScript
Create a custom MovieClip.drawSimpleRectangle( ) method using the Drawing API and invoke it on a movie clip. To draw a simple rectangle, specify the stroke's attributes using the lineStyle( ) method and then draw four lines using the lineTo( ) method: // Create rectangle_mc with a depth of 1 on the main timeline. _root.createEmptyMovieClip("rectangle_mc", 1); // Specify a one-pixel, solid, black line. rectangle_mc.lineStyle(1, 0x000000, 100); // Draw four lines to form the perimeter ...
5. How to draw a rectangle with rounded corners
You want to draw a rectangle with rounded corners, an offset, or rotation.Create a custom MovieClip.drawRectangle( ) method using the Drawing API and invoke it on a movie clip. The drawSimpleRectangle( ) method is, as the name suggests, quite simple. Let's create a more complex version that also: Draws a rectangle with a specified angle of rotation Let's you specify the rectangle center's coordinates Can draw a rectangle with rounded corners The drawRectangle( ) method accepts...
6. Reusing and Organizing Code in Flash Movies
You want to reuse code that you've created for one project in another Flash movie. Or you want to write your ActionScript code in an external text editor. Place your ActionScript code in external .as files and use the #include directive to add them to your Flash movies: // Adds all the code within MyActionScriptFile.as to your Flash movie. #include "MyActionScriptFile.as" Use the #include directive to incorporate code from external text files into your Flash movie during compilation from a .fla file ...
7. Drawing a Circle using ActionScript
Create a custom MovieClip.drawCircle( ) method using the Drawing API and invoke it on a movie clip. You can create a circle in ActionScript with eight curves. Fewer curves results in a distorted circle and too many curves hinders performance. Let's create a custom method of the MovieClip class for drawing circles. This method, drawCircle( ), allows for three parameters: radius The radius of the circle x The x coordinate of the circle's center point. If undefined, the circle is centered at x =...
8. ActionScript: Repeating a Task at Timed Intervals
You want to perform an action or actions at a specific timed interval. Use the setInterval( ) function. The setInterval( ) function allows you to specify an interval (in milliseconds) at which your Flash movie will invoke a function. Use setInterval( ) to perform a particular action over time but not necessarily at the frequency of the frame rate of the movie. // Define a function. function myIntervalFunction ( ) { // Output the difference between the current timer value and its value from the ...
9. Mouse Location Flash Script
Not only can you get the location of a movie clip on the screen, you can even get the location of the mouse, also known as the cursor. What is the difference between the mouse and the cursor? The mouse is the physical device attached to your computer. You may even have a track pad or tablet instead. The cursor is the graphic that moves around the screen as you move your mouse. So, technically, cursor is the term I should be using here. However, ActionScript uses the term mouse in its keywords. I will therefore use mouse and cursor i...
10. ActionScript: Performing Actions Conditionally
You want to perform an action only when a condition is true. Use an if statement or a switch statement. You often need your ActionScript code to make decisions, such as whether to execute a particular action or group of actions. To execute an action under certain circumstances, use one of ActionScript's conditional statements: if, switch, or the ternary conditional operator (? :). The conditional statements allow you to make logical decisions, and you'll learn from experience which is more appropriate for a given situ...










