AS: Fade In/Out by: True_Darkness
Related Topics:
AS: API- by -liam-
What is this about?
Well, with some very simple knowledge of API, I have created a script used for fadeing in and out in your Flash work, no Movie Clips invlolved! (Well, API ones =P)
Great! Now umm, what do I do?
Well let me just go over the basics of creating a Movie Clip using API. Here is the simple script to create a square:
_root.createEmptyMovieClip("square", 1); //creates a new Movie Clip called "square" with a depth of 1
with (square) {
lineStyle(2, 0x000000, 100); //sets the format of the lines of the square 2px thick,, black, and an _alpha of 100
beginFill(0x000000, 100); //sets the fill in of the square, black, and an _alpha of 100
lineTo(50, 0);
lineTo(50, 50);
lineTo(0, 50);
lineTo(0, 0); //use of 4 "lineTo" actions to create a square
Alright, so now you know how to create a basic square in API (better explained by -liam-.
Now What??
Now, we can begin!!
Start by making a square in API with the name "fade":
_root.createEmptyMovieClip("fade", 100);
I gave it a depth of 100 because we don't want anything with another depth to go in front of the fade (unless you want to, in which case you could change the depth to whatever you want).
with (fade) {
lineStyle(2, 0x000000, 100);
beginFill(0x000000, i);
We are using the VARIABLE "i" as our _alpha, because we need it to be able to change and work with the actions of our fade.
lineTo(5000, -5000);
lineTo(5000, 5000);
lineTo(-5000, 5000);
lineTo(-5000, -5000);
}
I chose the numbers -5000 and 5000 because we want to make the Fade bigger than the screen.
So there you have it, the basic API part of the fadeing script.
Now the very first thing we have to do is define i, otherwise this whole code won't work!! So lets put this code on the very top of your script:
onLoad=function(){
i = 0
}
There you go. Now "i" is defined as 0. So if you test your movie (Ctrl + Enter), you should have... nothing! Haha, this is because the Movie Clip that you created with API, has an _alpha of i, which is equal to 0. In other words, _alpha=0!
...That's not funny
Okay, sorry =( NOW! Next thing we need to do is make it so it fades! Right before the script that creates an empty Movie Clip:
_root.createEmptyMovieClip("fade", 100);
Add this script:
onEnterFrame=function(){
so now your code should look like this so far:
onLoad = function () {
i = 0;
};
onEnterFrame = function () {
_root.createEmptyMovieClip("fade", 100);
with (fade) {
lineStyle(2, 0x000000, 100);
beginFill(0x000000, i);
lineTo(5000, -5000);
lineTo(5000, 5000);
lineTo(-5000, 5000);
lineTo(-5000 -5000);
}
If you test your movie now, you should get an error that says:
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Statement block must be terminated by '}'
onEnterFrame = function () {
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 15: Syntax error.
Total ActionScript Errors: 2 Reported Errors: 2
So what you have to do, is just add another } at the end of your code, which will end the onEnterFrame function.
So, we are pretty much done. Now all you need is the code that actually does the fadeing part!
Add this action before the 2 closeing brackets (}):
_alpha = i; //sets the _alpha to the variable "i"
i += 2; // tells the variable "i" to plus 2 which will add to the _alpha, causeing the _alpha to rise. This is changeable
Now if you've done everything right, your code should look like this:
onLoad = function () {
i = 0;
};
onEnterFrame = function () {
_root.createEmptyMovieClip("fade", 100);
with (fade) {
lineStyle(2, 0x000000, 100);
beginFill(0x000000, i);
lineTo(5000, -5000);
lineTo(5000, 5000);
lineTo(-5000, 5000);
lineTo(-5000, -5000);
_alpha = i;
i += 2;
}
}
Now go on and test your movie (Ctrl + Enter).
The screen should fade out and turn black.
And there you have it! Now you can easily fade out, using one simple script!! Plug it onto the frame you want the screen to begin to fade out!
HEY! WAIT A MINUTE! What about fadeing... IN???
Well, doesn't take a genius to change a few numbers and signs around.
Since the variable "i" is set to 0, then the _alpha will start off at 0, as I explained earlier. So, if you want to Fade IN, change the variable to 100, which will make the _alpha of the Movie Clip start at 100.
Then, change the plus sign of what the variable "i" is adding at the bottom of the script, to a subtraction sign. This will make it so that the _alpha of the Movie Clip will subtract 2, causeing the screen to "Fade in".
The code should look like this:
onLoad = function () {
i = 100;
};
onEnterFrame = function () {
_root.createEmptyMovieClip("fade", 100);
with (fade) {
lineStyle(2, 0x000000, 100);
beginFill(0x000000, i);
lineTo(5000, -5000);
lineTo(5000, 5000);
lineTo(-5000, 5000);
lineTo(-5000, -5000);
_alpha = i;
i -= 2;
}
}
And that's all there is to it!! Easy and effective! I hope this helps you for your Flash projects =)
EDN