AS: _currentframe
This is a very basic introduction to the _currentframe property used in Flash (mainly games) all the time.
Okay, so what are we going to learn about today?
Today, we're going to learn about the _currentframe property. The _currentframe property is an action used in Flash that specifys a certain frame number of something. The _currentframe property can be used to specify a certain frame number in a Movie Clip, or a certain frame in the Main Timeline! It is a very and I mean very handy piece of Actionscript.
Alright, so I know what it does now, but how do I use it?
Well, let's say, for example, you wanted to find out if the frame of a Movie Clip was on frame 25. Let's start by opening a new Flash document. Draw a basic square and convert it into Movie Clip(Highlight the Movie Clip>>Right Click>>Convert to Symbol>> Movie Clip). When you are in the Movie Clip, give it about 50 or so frames. Now go back to Scene 1 of your Flash and give the Movie Clip an instance name of "my_mc" without the quotes.
Now go to your main timeline and put these actions on it:
onEnterFrame=function(){
if(_root.my_mc._currentframe==25){
trace("The MC has reached the 25th frame");
}
}
So now let's break that code down:
onEnterFrame=function(){ //For every frame on the Flash document...
if(_root.my_mc._currentframe==25){ // if the Movie Clip "my_mc" reaches 25...
trace("The MC has reached the 25th frame"); //tells flash to tell the output box to say "The MC has reached the 25th frame"
} //end if
} //end onEnterFrame
Now, before we go any further let me clarify one thing.
Many people do not know the difference between = and ==
Well here is the difference between the two:
= is used to define something (i.e score=2, health=100, n00bs=1000 etc.)
== is used in everything else, mainly if statements(i.e if(score==2), if(health==100), if(n00bs==1000))
So now that that's cleared up we can finally move on...
How can this help me to make a game?
Let's see. The _currentframe property can be used to tell something to do something. For example:
if(_root.enemy._currentframe==5){
_root.player.gotoAndStop("Pee_Pants")
}
That says that "if the Movie Clip 'enemy' is on frame 5, then the Movie Clip 'player' goes to the frame 'Pee_Pants' where in this case the player would be pissing his pants! har har har... anyway...
The _currentframe property can also be mixed with other things, such as variables. For example:
i=0
onEnterFrame=function(){
if(_root.i=0 && _root.MC._currentframe==2){
_root.MC.play();
}
}
That is saying that "if the variable 'i' is equal to 0 AND the Movie Clip 'MC' is on frame 2, then the Movie Clip 'MC' plays"
Hmm that all makes sense, but what if I want something to continuously happen after a certain frame?
This is where the > and < signs come in. These two signs mean greater than and less than
> - greater than
< - less than
How are they used in the _currentframe property?
Well let's say you wanted to say this:
"if the Movie Clip 'MC' is greater than 5, the Movie Clip 'MC2' plays"
You would put:
onEnterFrame=function(){
if(_root.MC._currentframe>5){
_root.MC2.play();
}
}
Now this is something you all MUST learn. In the code above, it says that "if the Movie Clip 'MC' is greater than 5..." This means that if it is on frame 5, nothing will happen...at all. >5 would state that it is greater than 5, meaning... not 5 at all, but greater than it, meaning 6 or above. Many people get this confused and it needs to be understood.
Well what about if I wanted something to happen on frame 5 or greater than frame 5?
These are where the > or < signs and the = sign combine. Let's give an example before we get to into it:
onEnterFrame=function(){
if(_root.MC._currentframe<=5){
_root.MC2.play();
}
}
Now that one is saying that "if the Movie Clip 'MC' is less than or equal to 5, the Movie Clip 'MC2' will play"
<= would specify that if the frame is less than OR equal to, meaning that if it ranges from 1-4 OR is on frame 5, than the Movie Clip "MC2" plays.
Wow, that is a lot to remember! Is there anything else I need to know?
Yep! As much as it may be to remember, there is still more! And there it is! There it is again!!! There it is 3 times! What the hell am I talking about?? (no that is not it)
I'm talking about this ! Yep, the exclemation point This is used in not only the _currentframe property, but in defining variables, hitTests, and much much more. The exclemation point is used to say "NOT" Now you are probably wondering how it is used... Let's give an example!
onEnterFrame=function(){
if(_root.MC._currentframe!=3){
trace("_root.MC does not equal 3")
}
}
That says that "if the Movie Clip 'MC' is NOT equal to (on) frame 3, then trace the sentance '_root.MC does not equal 3' "
It is ALWAYS used next to an = sign where the two signs > and < do not HAVE to be.
Uhhnnn my head...so much stuff!!
Well, the best advice I can give is to read it each day, or experement a bit each day until you come to finally understand it perfectly. It is a property that is used ALL the time in Flash, mainly games...
So that is it with the _currentframe property. I hope you all learned a lot, it took me a long ass time to write =P