The Flash Actionscript If Else Conditional Statement

posted: 29-04-06
author: PrimeVector
website: http://www.free-webmaster-resource.com
views: 28311
comments: 4
total pages: 1


Amazing Webmaster Widgets




The Flash Actionscript If Else Conditional Statement

The if statement is something that all developers have to learn when they start to enter the development world. Its used in all programming and scripting languages. The good thing is that it is not hard at all to understand. If you follow your own logic and the code you can often just read what a piece of code is doing.

So talk is nothing lets give you an example. Lets say you defined some variable called "age". Then lets suppose you want to output your age to the screen in Flash only if that variable is filled with your actual age. If its not filled with your real age then we just output it as an error.

In my case my age is 27 (in 2006), so i want to see my age on the the screen if variable "age" is set to 27. In this example the condition will be set to true because age is actually 27.

Open the code panel with F9 in Flash and just enter the code on the frame that is there. Try not to copy the code, its good to write it out yourself, this will make sure you will learn this a lot faster!

First we declare a variable and set it to be a number, 27 in this case. Then we check if age is equal to 27. We use == to do that check. The code between the brackets {} will be triggered only if the condition is met. So only if variable age is set to 27 the code between the { ... } In this case i just setup a trace to output some text to the screen when run inside Flash.

Btw i did not use the FlashMX2004 and Flash 8 strong datatyping. So when i declare a variable that is for example a number i just set it like var num = 10; and not var num:Number = 10; I do this because then people with Flash MX can also use this a bit more easilly.

So here is the first part of the code, we will expand thisone a little bit.

var age = 27;
if(age == 27){
     trace("The age of primevector in 2006 is 27!");
}

Expanding a bit

You could leave this example like this, but then only a message would be traced if the condition is actually met, nothing is done when it isnt met .. so for example when we would have set variable age to 20, nothing would happen because the condition wehad set was never met.

Lets also setup a trace message when the condition is not set. To do this add this bit of change the above into the following.

var age = 27;
if(age == 27){
     trace("The age of primevector in 2006 is 27!");
}else{
     trace("Error, no conditions are met");
}

As you can see we replaced the closing bracket } with }else{ ... } We also put a new trace in there. If you look at the code you can almost read it as you would normal text. At least if you are a bit used to reading through code a bit ;)

Expanding a bit more

We would also easilly do something like also try to execute some code if another condition would be true. For example we could also do a trace if the would be set to e.g. 20. If you wanted to do something like that this would work. Remember to set variable age to 20 to test it ;)

Lets set it up.

var age:Number = 20;
if(age == 27){
     trace("The age of primevector in 2006 is 27!");
}else if(age == 20){
     trace("The age is set to 20!");
}else{
     trace("Error, no conditions are met");
}

Conclusion

I hope this little tutorial could help you a bit further into your development life in Flash. I would suggest doing a few little tests with this and then move on to new cool things :) If you have any questions please don't be shy and post a message in the forum.



Amazing Webmaster Widgets






Comments

4 comment(s) found in 1 page  
viewing comment(s): 1 - 4

1 |

16-08-07:guest

16-08-07:APPROPRIATE

zqD5CY Many website. Great thanks! ,

16-08-07:Reductase

a Great such Go work. on Hi! with good site! ,

17-09-06:guest



- Press here to view all available smilies!





In Focus
Cool Stuff