00:00
00:00
Newgrounds Background Image Theme

AcekillerX67 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As: Api Scripted Camera

5,663 Views | 22 Replies
New Topic Respond to this Topic

As: Api Scripted Camera 2006-07-22 16:15:48


AS: Main

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...

EXAMPLE

Response to As: Api Scripted Camera 2006-07-22 16:21:27


Sweet I can see this being usefull for quite a few types of games = )


BBS Signature

Response to As: Api Scripted Camera 2006-07-22 16:22:32


At 7/22/06 04:21 PM, Cryzabey wrote: Sweet I can see this being usefull for quite a few types of games = )

Thanks, hell, it was used in ALL my games =D (Well, most of my games anyway)

Response to As: Api Scripted Camera 2006-07-22 16:25:12


Nice very usefull.

And another plus point, I just recently started learning AS properly, and I actually UNDERSTOOD what most of the code did without the explanation. That must mean I'm getting somewhere, right?

Response to As: Api Scripted Camera 2006-07-22 16:26:04


At 7/22/06 04:25 PM, Gutya wrote: Nice very usefull.

And another plus point, I just recently started learning AS properly, and I actually UNDERSTOOD what most of the code did without the explanation. That must mean I'm getting somewhere, right?

Definately. And remember, any understandment is a step foward.

Response to As: Api Scripted Camera 2006-07-22 16:30:54


At 7/22/06 04:26 PM, True_Darkness wrote: Definately. And remember, any understandment is a step foward.

:) Thanks.

Response to As: Api Scripted Camera 2006-07-22 16:33:35


Hey!
If you're going to teach ActionScript, teach it proper!
Lol j/k. :P
You forgot to set what kind of function the functions was (onLoad, onEnterFrame):
onLoad = function ():Void {
onEnterFrame = function ():Void {


BBS Signature

Response to As: Api Scripted Camera 2006-07-22 16:35:21


At 7/22/06 04:33 PM, GuyWithHisComp wrote: Hey!
If you're going to teach ActionScript, teach it proper!
Lol j/k. :P
You forgot to set what kind of function the functions was (onLoad, onEnterFrame):
onLoad = function ():Void {
onEnterFrame = function ():Void {

Thats not really necessary.

Response to As: Api Scripted Camera 2006-07-22 16:36:39


At 7/22/06 04:35 PM, Darkfire_Blaze wrote: Thats not really necessary.

Nah, but you'll probablt need it in AS 3.0. :P
It's the same as with variables. :Number :String etc..


BBS Signature

Response to As: Api Scripted Camera 2006-07-22 16:36:50


At 7/22/06 04:33 PM, GuyWithHisComp wrote: Hey!
If you're going to teach ActionScript, teach it proper!
Lol j/k. :P
You forgot to set what kind of function the functions was (onLoad, onEnterFrame):
onLoad = function ():Void {
onEnterFrame = function ():Void {

Seem that's where I get lazy, haha. Yeah, it's a lot better to do that, but it works the same without the :Void and var: and Number: crap =p. I should get into the habbit of doing that though if I'm going to take up programming this year. I just don't find it neccesary since the scripts will work the same without them =D Besides, I'm lazy =p

Response to As: Api Scripted Camera 2006-07-22 16:39:11


At 7/22/06 04:33 PM, GuyWithHisComp wrote: Hey!
If you're going to teach ActionScript, teach it proper!
Lol j/k. :P
You forgot to set what kind of function the functions was (onLoad, onEnterFrame):
onLoad = function ():Void {
onEnterFrame = function ():Void {

Its nothing major though. The script will still work without declaring the type of function, its still a good idea to though :)
Nice tutorial, simply put. I don't get the demo though. Is it broken, or is it just me?


BBS Signature

Response to As: Api Scripted Camera 2006-07-22 16:41:00


At 7/22/06 04:39 PM, Depredation wrote: Nice tutorial, simply put. I don't get the demo though. Is it broken, or is it just me?

Thanks, the example should work... Move the mouse around. If it doesn't work, I can't say I know what's wrong =(

TD ALT

Response to As: Api Scripted Camera 2006-07-22 16:44:38


BTW, I forgot to say, really nice tutorial. :)

Response to As: Api Scripted Camera 2006-08-12 01:41:03


Yeah I don't really declare variable types or function data types personally. But apparently it speeds up the performance of an engine when you explicitly define data types.
So I'm starting to do it a bit.


I was never really here.

Response to As: Api Scripted Camera 2006-08-12 06:27:25


At 8/12/06 06:04 AM, West-End-Pro wrote: Just one question....How do we edit this to follow a movieclip? say "box" ?

Replace _xmouse, _ymouse with .box._x; , .box._y; Me thinks:)


BBS Signature

Response to As: Api Scripted Camera 2008-02-10 11:49:50


Hey that is GREAT thank you INFINITE TIMES.

Response to As: Api Scripted Camera 2008-03-29 02:13:56


can u put the script on a character so that the camera follows his movement?


BBS Signature

Response to As: Api Scripted Camera 2008-10-30 14:37:08


how, then, would you go about adding a layer that "stayed still" if you know what I mean?
-Blayzeing


BBS Signature

Response to As: Api Scripted Camera 2008-12-11 23:18:42


At 10/30/08 02:37 PM, Blayzeing wrote: how, then, would you go about adding a layer that "stayed still" if you know what I mean?
-Blayzeing

Easy insert a mcbox where you want the camera to stay put with a 0 alpha value and add a hittest. Then put the code between between an if statement like

if(StayStill=true){
the code
}

If you mean like a frame, well you just put it on a depht higher than the cam.

Response to As: Api Scripted Camera 2009-07-20 22:05:20


Is it just me, or does this script only work if placed on the first keyframe?

I tried using this code in a keyframe besides the first, and it wouldn't work.


ambition and procrastination don't mix

Response to As: Api Scripted Camera 2011-07-29 03:01:01


Nice! this is much better than having to add code inside a movieclip!

Response to As: Api Scripted Camera 2011-07-29 04:13:52


At 8/12/06 06:29 AM, West-End-Pro wrote: meh......tried it. Doesn't work. I'm not that stupid........lol

you obviously forgot _root.

Response to As: Api Scripted Camera 2011-12-12 15:12:17


At 8/12/06 06:27 AM, Depredation wrote:
At 8/12/06 06:04 AM, West-End-Pro wrote: Just one question....How do we edit this to follow a movieclip? say "box" ?
Replace _xmouse, _ymouse with .box._x; , .box._y; Me thinks:)

please tell me more, my character movie clip is named stick man and the instance name is stickman so how do i edit it so it follows the movie clip?