API Scripted Camera
Hey everyone. Another AS thread brought to you here to teach you all how to make an "API" scripted camera, without the use of a V-Cam. So, let's start shall we?
So what IS a "Scripted Camera?"
Well, a scripted camera is pretty much everything it sounds like. A scripted camera. What it does is, without having to draw anything on the screen, uses the Flash Scripting language (Actionscript) to make a "camera" in effect, that would move across the screen however you want, without having to tween.
You spoke of a "V-Cam" before, what's that?
A V-Cam is a drawn out, scripted camera, that you can tween across the screen and do whatever you want with so you don't have to tween. The V-Cam is great and is used many times in games and animations. The purpose of my scripted camera using API, is to easily make a camera that is "lock onable" so to speak. While the V-Cam is just as good if not better, the Scripted Camera of mine is a short and easy way to make cameras in flashs.
So where do I start?
First, obviously, open your Flash program. When done so, open up the Actions Panel at the bottom of the screen, or press F9.
Now, we can begin looking at the script. Here, is the full script for your copy and pasting needs.
createEmptyMovieClip("boxCam", 1000);
with (boxCam) {
onLoad = function () {
CamX = _x;
CamY = _y;
};
onEnterFrame = function () {
_x = _root._xmouse-10;
_y = _root._ymouse-10;
CamX = CamX+(_x-CamX)/50;
_root._x = Stage.width/2-CamX;
CamY = CamY+(_y-CamY)/50;
_root._y = Stage.height/2-CamY;
};
}
The script, when pasted on the main timeline, will make a camera that follows the mouse anywhere on the screen. Go on ahead and try it out. Be sure to draw something on the screen so you can see the cameras movement.
So now let's look at the script piece by piece. The first thing that we see is our little form of API, the createEmptyMovieClip command. What this does, is creates a blank movie clip, purely from actionscript, without having to go through some panels, draw some garbage and convert it to a symbol, short and sweet. You'll notice that the name I gave was camBox, which is the name that will be used for the camera. Next to the name is the number 1000. This is the depth, which could be any number in effect, but to make sure it stays above anything else in the flash, the depth is set to 1000. (This REALLY does not matter).
Next you'll see the action "with(boxCam)" which is just a short way of doing things without having to put _root.boxCam before everything. After that is an onLoad function, which in this case is used to load variables before the flash starts. These two variables are CamX and CamY, which will be used later in the script. The CamX and CamY are both set to the _x and _y because the camera will recognize this "emptyMovieClip" as the camera, and must know that it will be following the _x and _y of the camera, which are later set to the mouse in effect.
The next function is set up, and is called the onEnterFrame function, which tells flash to constantly continue the actions throughout the flash. Note that these functions are still in the with command, telling them to work on the boxCam movieClip.
In the enterFrame function, you'll notice that the _x and _y are set to the _xmouse and _ymouse. This is what makes the camera be moved at the mouse. The CamX and CamY are also set to _xmouse and _ymouse, since the variables are following the _x and _y.
Next, the big script comes in. This is the actual camera script.
CamX = CamX+(_x-CamX)/50;
_root._x = Stage.width/2-CamX;
CamY = CamY+(_y-CamY)/50;
_root._y = Stage.height/2-CamY;
There it is. Let's take a look shall we?
The first thing you'll see is the variable CamX coming into play, where it is being told to add itself to it's _x- the camera variable, causing it to stay in place. You see, if you add something to something that is being subtracted by itself, it causes a "double negative" in a way, causing nothing to happen making it stay still, when in effect, something IS happening. Confusing? I know. You'll see that it is being divided by 50. 50 is the number that defines the "drag" of the camera, telling the camera how fast or slow it should go. The number can be changed to any number. Note that the larger the number is, the slower it goes. And vice versa.
The next part of the script tells the main stage _x to split in half, and take away from the CamX variable that was defined to "stay still", making it follow the _xmouse.
The same thing is done to the variable CamY, and works for the _y instead of the _x.
So, now, the camera follows the _xmouse and the _ymouse, at the given speed/drag, 50.
Feel free to edit the script yourself however you like. That about sums it up.
Till next time...