Saturday, January 24, 2009

Clipevent + Conditions

Hi, remember when we talked about functions for buttons ?
Let's talk about some for movie clpis. Usually, if you are adding actions to a movie clip you always have to start with :

onClipEvent (_____) {

inside parenthesis you can type:
load, unload, enterFrame, mouseDown, mouseMove, mouseUp, keyUp, keyDown, data

well, the most important are load, and enterframe, but they all can be useful. I will now talk briefly about some of these =]

you use load when you want the action to happen when the frame loads (this is good for setting variables)
you use enterFrame when you want the actions to happen when the timeline reaches the frame the movieclip is in.
mouseDown is used when you want something to happpen when you click the MC
mouseMove refers to when you move the mouse inside the MC
mouseUp refers to when you release your mouse after clicking on the MC
keyUp refers to when you press specified key
keyDown refers to when you let go specified key.

don't worry, i will talk about unload and data later =]

for now, let's talk about the structure of blocks of actions.
everytime you open up a bracket, you have to close it. Same for parenthesis. If you have any experience with any other type of coding, you should know this. In HTML, if you open a tag, you have to close it .
Take a look at this button's actions:

on (press) {
if (Key.isDown (Key.DOWN)) {
_root.gotoAndPlay ("intro");
}
}

you can see that i have closed every single bracket and parenthesis that i opened.
i know you are wondering about this code i showed you. Wanna learn ? Ok, let's talk about conditions =]
there are many conditions but for now i will talk about if, else if, else, while.

If is used when you want something to happen when specified condition is true. At the code i showed, when you press the button, it is only gonna go to the intro frame is the key DOWN (down arrow) is down. Pretty easy right ?

else if, is used after you closed an if statement. So basically, when the if statement isn't true, you are giving it another condition, if it's true, flash will perform your actions. That was kind of hard, so i will give you a brief example:

...
if ( Dead=true ) {
_root.gotoAndPlay ("dead");
}
else if ( Dead = false ) {
_root.gotoAndPlay ("alive");
}
...

Is it clear now ? if not, just write a comment.

Else is also used after an if statement. But it doesn't give another condition, it gives an action to be performed if the if statement isn't true. As an example, take a look at the codes i just showed you for else if. It could also be written as :

...
if (Dead=true) {
_root.gotoAndPlay ("dead");
} else {
_root.gotoAndPlay ("alive");
}
...

Both codes mean the same thing. Since Dead can only be true or false, there is no need to use else if. It will work, but it is wrong. This code tells flash to go to frame "dead" if the condition ( dead = true ) is true. Else, it goes to frame "alive". you should understand that.

using while is a little more complicated. uou used it when you want something to happen while a condition is true. So you will specify the condition you want to be true, and then you type what you want to happen. An example;

...
while ( dead = false ) {
this._x += speed;
}
...

that was easy right ? This code tells our MC to perform a code WHILE dead = false. =]

Enough of conditions, if you need help, write a comment.

Now i will talk more about action script. Well, you should know that flash is a vectorial program, so if you have had enough math classes you shoud know about the Y axis and the X axis. Here is an uggly example:

Y axis
^
|
|
|
|
|
--------------> X axis

i won't talk much about this now, but on the last code i wrote i said:

this._x += speed;

that means that the MC is going to the right at specified speed.
Now why is it going to the right ?
you see that += , that means the X value of the movie clip is going to increase.
We are ADDING a number ("speed") to its X value.
If we used -= , it would go to the left.
if we used _y, it would go up.
If we used _y and -=, it would go down.

you can also say:
this._x = 45 --> that means the mc is going to position 45 of the X axis.

I know this was a little hard, but when i start tutorials, it all will get as easy as hell.

This is it for now, next time i will talk about variables.

0 comments:

Post a Comment