PDA

View Full Version : The "If" conditional [Flash][Actionscript]



Kitsune
07-16-2007, 12:21 AM
In this tutorial i will be teaching how to use the if statement in flash. its a very powerful tool in actionscripting that could help progressions in games very much. we'll start by saying what we'll be going over:

Contents
1. If (intro)
2. Variables
3. Else
4. Uses

Its a short list, but the 3 chapters cover many helpful things youll need to know to make simple games. its very easy to learn as well so lets get started.

1. If lol emphasis
If is a primary tool in flash used for variables, mathematics, and other simple codings made in actionscript. you can add numbers automatically, jump to frames, or even make a movement smooth when youre controlling something with the arrow keys. but more on that later. of course this tutorial assumes that you know a little basic knowledge for actionscript like the gotoAndPlay();.

ill give you a small example code to see what IF itself means:

if(var == 0){
gotoAndPlay(2);
}
the var on the first line stands for a variable that has been already set. (theyll be covered later). so its just like reading a sentance. the if(var == 0){ part means "this the variable equals zero", and the { opens a bracket so that an action can be done when this variable happens. the gotoAndPlay(2); part is simply the action that happens when the variable happens. it means that it will jump to the second frame when the variable happens. the } on the third line simply closes the action. so its like a sentance, "if the variable turns to 0, then jump to the second frame." post any questions or confusions that occured while reading this. ill try to explain. this is a basic tutorial so ill only be going into simpler stuff.

2. Variables
variables are simple youre just telling flash that something is equal to something else. for example:

health = 100
when you say that, youre telling flash that the variable health has a value of 100. you can show this on the stage as well. just make a textbox with the print tool [A], stretch it to the room that the number 100 could fit, go to properties when the box is selected, and to the top left of the properties window, theres a pulldown menu that says static text as the origonal setting. make it dynamic text. but were not done. you have to apply the variable to the textbox before we can go any further. once again, in the properties box theres an option that says Var: in the lower center. type health into that box and test your movie. the number 100 comes up in the dynamic text box. you may want to make static text Health: in front of it.

now that your variable is up on the stage, we cant just leave it alone. we can subtract, add, multiply, or divide any variable. so why not do that?

now make a button which would be the enemy. (please figure out how to make buttons if you dont know). and put this code on it. ill explain, dont worry.

on(release){
(health -= 10)
}
what you just did there was telling actionscript that when you click that button in your movie, the health would go down by 10. so if you test the movie, and click on your enemy, the number will subtract 10 each click. now how you did this was with the (health -= 10) part. the -= is a subtracting particle. its simple subtraction. this goes the same with + for addition, *, and /. so if you do the same thing with a first aid kit you could put in the code:

on (release){
(health += 20)
}
and it will add.

you can apply these variables to any action. like on(rollOver, rollOut, release, or press.) also on hitTest codes. but youll have to learn hitTests in another tutorial.

you can do the same with letters.

on(release){
(word += M)
}
so if you have a dynamic text box and youre trying to fill in words, make a dynamic text box, put the Var as word, and you can apply this code to a button.

there are some other codes that i need to go over for you to understand fully. we went over the +=, and -= as operators in math as well as *= and /=. but when comparing something using the If tool then instead of = to show what the variable's new value is, you use ==. this may be confusing so ill give an example.

this is when you first enter the code. nothing is compared.

var = 20
this is when you compare something to that variable.

if(var == 30){}
when applying operators, you never use +== or anything like that. its just one equal when doing a mathemeatical action.

3. Else
else is a helper to If. its only added to if statements (that i know of). its the "otherwise" conjunction in our actionscript sentance. it says if this doesnt happen, this will. let me show you:

if(health > 0){
gotoAndPlay(2);
}else{
if(health < 0){
gotoAndPlay(3);
}
the (health > 0) means if its greater than 0. so its saying, "if the health greater than 0, go to the second frame" the else comes in as well saying, "otherwise, go to the third frame."

you can also put simple conjunctions into the conditional (statement with a variable) like and, or or. they can be used either for the same variable or different ones. for example:

if(health == 0 and ammo == 0){
gotoAndPlay(2);
}else{
if(health > 0 or ammo == 100){
gotoAndPlay(3);
}
so you can play around with your possibilities.

you can also use != in order to say "isnt".

if(var != 20){}
this means if the variable is not 20 then you do the action inside the curleh brackets.
4. Uses
there are loads of uses for vatiables. as weve stated it could do math if you need a score counter, it could jump frames if you need a healthbar, or a losing message, it could even configure a whole simple game if made correctly. i hope this helped because it took about 30 minutes to type it. my info sources are newgrounds and a bit from some members here. thanks for reading my tutorial and i hope you got something from it.

Shadow801
07-18-2007, 10:13 AM
Ok I know all this already, But what I need to know is how do I do something like this ( This gives me errors )




onClipEvent (load) {
var = 2 *= var2
}


I'm confused :(

2pacproducer
07-18-2007, 10:24 AM
maybe some finished work ?

Ordinair
07-18-2007, 11:12 AM
I'm really sorry, but this tutorial is not good quality. You didn't explain the terms properly and often times made up your own words for terms you didnt know about. The parantheses that come after "if" are the conditions, for example. All you said about that was "the var on the first line stands for a variable that has been already set. (theyll be covered later)."

Then you went on to say something about operators which was very vague. You said that "-=" was for subtraction but you didn't explain the difference between arithmatic operators ( "-" "+" "/" etc.) and compund assignment operators ("-=" "+=" "/=" etc.). Actually, an arithmatic operator is for doing simple math and a compound assignment operater is for modifing the value of a variable. As you can see by the post from Shadow801, it confused him and he was thinking it is ok to use compound assignment operators where normal arithmatic operators go.

You also didn't start your sentences with capitals which made it look really unprofessional.

There was lots more in the tutorial I could go on about. Please, next time before writng a tutorial, make sure you actually know how to explain the terms properly.

Shadow801
07-18-2007, 11:22 AM
I'm really sorry, but this tutorial is not good quality. You didn't explain the terms properly and often times made up your own words for terms you didnt know about. The parantheses that come after "if" are the conditions, for example. All you said about that was "the var on the first line stands for a variable that has been already set. (theyll be covered later)."

Then you went on to say something about operators which was very vague. You said that "-=" was for subtraction but you didn't explain the difference between arithmatic operators ( "-" "+" "/" etc.) and compund assignment operators ("-=" "+=" "/=" etc.). Actually, an arithmatic operator is for doing simple math and a compound assignment operater is for modifing the value of a variable. As you can see by the post from Shadow801, it confused him and he was thinking it is ok to use compound assignment operators where normal arithmatic operators go.

You also didn't start your sentences with capitals which made it look really unprofessional.

There was lots more in the tutorial I could go on about. Please, next time before writng a tutorial, make sure you actually know how to explain the terms properly.
Ok so can you explain to me what I did wrong??

I need this quickly :(

I have made a gravity system

Now I am trying to make a weight system so that the heavier it is the faster it falls. To do this I need to have the var gravity to be 2 x the weight

So gravity = 2 *= weight is what I tried

So explain what I did wrong.. I need this :P

Ordinair
07-18-2007, 11:32 AM
gravity = 2*weight;

You only need one assignment operator.

Shadow801
07-18-2007, 12:22 PM
Thanks :)


Now I can finish my project