
ActionScript for Non-Coders
Pages: 1, 2
A Virtual Camera
One of the systems that Flash doesn't have that many other animation environments have
is a virtual camera. Many professional animation systems allow you to set up a virtual
camera that can itself be animated. This allows you to create the sort of camera work seen
in traditional films, including pans, zooms, and fades.
Flash uses a more old-fashioned system based around a stage. Rather like a theater stage,
the audience view is fixed, and it becomes harder to create the sort of close edits and
camera effects we are used to seeing in Hollywood films.
Adam mentioned this one day in passing, to which I replied, "OK. If Flash doesn't have
a camera, let's make our own."
Although a virtual camera sounds like a world away from the simple particle effect we just looked at, the way I did it is almost exactly the same. Again, there is no
complex code, and the time to develop the camera was insignificant. It will, however, most likely save Adam literally days in some of his longer animations, because the
alternative to a scripted camera is to simulate the camera pans, fades, and zooms using
only tweens, and this can take a very long time to set up or change.
Adam sent me the animation in Figure 7 to illustrate the need for a virtual camera. The
Flash stage is set up here to show a short scene in Adam's next production. The trouble is,
he would like to create a number of camera movements and effects that are difficult with
tweening. Although using embedded movie clips is one solution, the large number of
separate animations needed in a typical animation make their use just as troublesome.
We really need that Vcam to create a clean solution.

Figure 7. The waterfall. Click on the image to see the waterfall animation.
|
After some discussion (and a few failed trains of thought regarding what a Vcam should
look like and do), we settled on the following implementation.
The Vcam will appear in the authoring environment as a rectangle that defines
the Vcam viewfinder. You can download the source FLA for this animation here. Note that if you test the FLA, you will see the best results if you publish to the browser
rather than view the result in the authoring environment (text movie will not
cut out the content that appears outside the viewfinder, whereas the final effect
when seen in a browser does). See Figure 8.

Figure 8. The virtual camera as seen in the Flash authoring environment. |
All the animator has to do to use the camera is to tween the viewfinder around
the stage. In the final animation, the code inside the camera component scales
and pans the stage such that only the content within the viewfinder is seen.
See Figure 9a.

Figure 9a. The virtual camera in use. Click on the image to see the Flash
animation in a popup. |
The Vcam is nothing more than a simple component -- a rectangle with a short script
attached to its first keyframe.
Here's the code that drives the camera:
function camControl():Void {
parentColor.setTransform(camColor.getTransform());
var scaleX:Number = sX/this._width;
var scaleY:Number = sY/this._height;
_parent._x = cX-(this._x*scaleX);
_parent._y = cY-(this._y*scaleY);
_parent._xscale = 100*scaleX;
_parent._yscale = 100*scaleY;
}
function resetStage():Void {
var resetTrans:Object = {ra:100, rb:0, ga:100, gb:0, ba:100, bb:0, aa:100, ab:0};
parentColor.setTransform(resetTrans);
_parent._xscale = 100;
_parent._yscale = 100;
_parent._x = 0;
_parent._y = 0;
}
// make frame invisible
this._visible = false;
// Capture stage parameters
var oldMode:String = Stage.scaleMode;
Stage.scaleMode = "exactFit";
var cX:Number = Stage.width/2;
var cY:Number = Stage.height/2;
var sX:Number = Stage.width;
var sY:Number = Stage.height;
Stage.scaleMode = oldMode;
// create color instances for color
// transforms (if any).
var camColor:Color = new Color(this);
var parentColor:Color = new Color(_parent);
// Make the stage move so that the
// v-cam is centered on the
// viewport every frame
this.onEnterFrame = camControl;
// Make an explicit call to the camControl
// function to make sure it also runs on the
// first frame.
camControl();
// If the v-cam is ever removed (unloaded)
// the stage, return the stage to the default
// settings.
this.onUnload = resetStage;
The function camControl will run every frame. This function uses the position- and
percent-scaling properties of the movie clip (_x, _y, and _xscale, _yscale) to
continuously scale and move the stage so that the viewable content in the final movie is
constrained to the stuff seen in the viewfinder (much as would occur in a real movie
camera). See Figure 10.
As well as zooming and panning, we decided it would be cool to have some way of
applying video transitions to the final content, allowing the camera to add fade effects as
well as color tints (tinting can be important in adding ambience to an animation).
Although using the color class in Flash can be daunting, the way the camera does it
makes it very easy -- all the animator has to do is apply a color tween to the camera, and
the following line (first line of camControl) will programmatically apply whatever color
tweening it sees in the camera onto the stage (in much the same way that adding a color
filter cap to a movie camera would tint the recorded movie in a real camera).
parentColor.setTransform(camColor.getTransform());
When the camera is no longer needed, the animator can delete it from the timeline. The
code uses the event that occurs when a movie clip is removed in this way (onUnload) to
cause the stage to be returned to the normal size. This is performed via the function
resetStage.
It cannot be understated how useful we found the virtual camera in creating Flash
animations that have the sort of pace and movement associated with mainstream cartoons
and live-action movies. Although it may seem like a minor piece of code to a scripter, it is
something that many animators have always found missing in Flash.
Of course, that's not a problem, because the Flash environment not only allows you to
create your own content-creation building blocks, but in most cases, almost requires you
to create them.
Finally, it is worth noting that, unlike a real camera, our virtual camera allows you to
create distorted viewfinders. These result in stretched animations.

Figure 9b, 10. The effects of using irregular shaped viewfinders: the image
on the top shows a flattened viewfinder; the image on the bottomshows the
result (a vertically stretched movie) |
Although this may seem like a minor point to some (you can keep the viewfinder
proportions the same throughout the animation if you hold down CONTROL every time
you scale the camera with the scale tool), and a bug to others (it would be easy to add
some code to the camera to make sure that the view was never distorted in this way), we
left it in because it led Adam to a rather surprising hack: distorting an image as you
zoom and pan can give a very cool, faux 3D effect.
Click here,
here,
and here
to see this effect in action.
This works because the distortion makes the scene appear as if it is mapped onto a curved
3D surface rather than onto a flat plane.
So, in setting out to create a 2D virtual camera, we seem to have also stumbled onto a
hacky 3D camera as well.
Bonus!
Also, I am working on integrating the camera as a custom tool in the Flash toolbar
(via JSAPI), given that it is proving to be such a useful animation tool. It just goes to show
how far Flash can be altered through scripting to make the process of creating content
easier for both scripters and non-scripters.

Figure 11. A prototype camera tool (currently in production). |
Conclusion
Although many people wonder how the Flash authoring tool is used to create some of the
Flash content out there, one of the big secrets is the way the authoring tool can be used to create building blocks to aid development via components, as well as optimize the authoring environment in other ways.
Despite the fact that many non-scripters find ActionScript off-putting, there is nothing
wrong with that, and it shouldn't prevent them from gaining the advantages of scripting. ActionScript can easily be written and encapsulated within the workflow such that a
designer or animator doesn't have to know about the code at all.
The creation of tools to change your workflow is not only useful in accelerating content
creation, but can often throw up totally new techniques, as illustrated by our stumbling on a 3D-camera effect whilst developing a 2D camera. Working with Flash is like that!
Sham Bhangal
is an author of and contributor to numerous books on Flash and ActionScript, including Foundation ActionScript for Flash MX, Flash MX Designer's ActionScript Reference, and Macromedia Flash MX Upgrade Essentials.
Return to the Web Development DevCenter.
In June 2004, O'Reilly Media, Inc., released Flash
Hacks .

Showing messages 1 through 89 of 89.
-
Wondering
2009-08-16 11:13:43
TanLiqingVivien
[View]
-
Movie Clips the Camera doesn't see?
2008-02-20 16:09:06
Ramiel
[View]
-
<3
2007-09-17 01:59:02
Quaay
[View]
-
<3
2007-11-20 21:08:09
darkace
[View]
-
Thank you so much!
2007-08-16 03:25:18
LogFish
[View]
-
the colors
2007-08-06 14:53:28
deadya
[View]
-
movement speed
2007-07-27 10:44:15
Kilaust
[View]
-
movement speed
2007-07-27 10:43:54
Kilaust
[View]
-
Flash 5 Problem
2007-06-09 05:58:00
Barbaro666
[View]
-
cant make it work
2007-03-20 04:09:13
neverblink
[View]
-
cant make it work
2007-03-30 23:29:48
InvasiveInfection
[View]
-
Colour gets screwed up
2007-02-17 07:54:28
Freaktwa
[View]
-
Exporting
2007-02-13 11:36:05
braintreestudios
[View]
-
errors
2007-01-30 11:43:51
FuzzySneaker
[View]
-
A Question?
2007-01-18 02:23:52
MAHIYA
[View]
-
Error Messages
2006-11-09 20:11:06
rockheadrumple
[View]
-
another javascript solution
2006-11-04 03:12:38
peanch
[View]
-
does camera work if swf imported to after effects
2006-10-23 06:16:28
madamblahblah
[View]
-
uhhhh
2006-09-19 18:35:48
sasquatch13
[View]
-
uhhhh
2006-09-19 18:35:46
sasquatch13
[View]
-
uhhhh
2006-09-19 18:35:42
sasquatch13
[View]
-
uhhhh
2006-09-19 18:31:41
sasquatch13
[View]
-
3D efect
2006-09-08 05:26:49
Allia
[View]
-
masks
2006-08-12 03:43:55
oggycheese
[View]
-
Any more camera movement tutorials
2006-07-18 21:27:12
Eddee14
[View]
-
fla file
2006-07-17 14:12:32
Soliloquy
[View]
-
fla file
2006-07-20 15:38:42
Soliloquy
[View]
-
fla file
2006-07-18 10:36:44
Soliloquy
[View]
-
Rotation and blur rendering
2006-02-17 21:44:52
Masterpingoo
[View]
-
Camera Bogs Down Movie
2006-02-06 15:20:55
mrmyers
[View]
-
Camera Bogs Down Movie
2006-06-25 17:47:27
vaisacht
[View]
-
Window size
2006-02-04 05:32:11
KKSlayer
[View]
-
Window size
2006-02-04 06:09:40
Sham_B
[View]
-
VCamera
2006-01-05 15:46:35
toonboy1
[View]
-
VCamera
2006-03-28 05:11:34
robincb
[View]
-
VCamera
2006-02-04 06:06:48
Sham_B
[View]
-
Only one problem
2005-11-30 23:16:55
MaticooL
[View]
-
Only one problem
2005-12-01 03:23:09
MaticooL
[View]
-
Wow
2005-11-30 21:47:31
MaticooL
[View]
-
pause and play button.
2005-11-14 10:58:05
cybexy
[View]
-
Where to Download Virtual Camera component?
2005-09-23 13:26:52
silverone
[View]
-
Where to Download Virtual Camera component?
2005-09-24 13:09:00
Sham_B
[View]
-
Where to Download Virtual Camera component?
2005-10-18 11:01:46
silverone
[View]
-
fading?
2005-07-07 06:23:34
shether
[View]
-
fading?
2005-07-19 17:26:05
Doki
[View]
-
Very First Frame
2005-06-28 17:24:48
Doki
[View]
-
using final swf as loadmovie
2005-06-24 10:25:10
digisam
[View]
-
The camera's not zooming in.
2005-06-21 05:05:43
15FloppyDiscsInAWetCave
[View]
-
The camera's not zooming in.
2006-06-25 18:22:59
vaisacht
[View]
-
The camera's not zooming in.
2005-06-21 15:06:46
15FloppyDiscsInAWetCave
[View]
-
The camera's not zooming in.
2005-06-21 15:49:32
15FloppyDiscsInAWetCave
[View]
-
Camera
2005-06-18 04:00:42
J.Johnson
[View]
-
Camera rotation
2005-06-18 04:07:04
J.Johnson
[View]
-
Camera rotation
2005-11-04 05:00:31
radja
[View]
-
camera
2005-06-11 23:45:08
caseyspaos
[View]
-
camera
2005-06-12 03:56:08
Sham_B
[View]
-
About the flies component...
2005-05-21 09:01:05
Stryker
[View]
-
About the flies component...
2005-06-10 05:55:56
Sham_B
[View]
-
interactive camera ?
2005-05-03 16:33:07
richardbobcat
[View]
-
interactive camera ?
2005-05-03 16:52:15
Sham_B
[View]
-
interactive camera ?
2008-01-17 21:05:23
jn_casanova
[View]
-
interactive camera ?
2007-01-15 00:17:55
Black_Vapor
[View]
-
interactive camera ?
2005-10-20 11:37:56
Murk280
[View]
-
Thanks a lot
2005-04-14 02:10:40
mkamler
[View]
-
Thanks a lot
2005-04-14 06:15:18
Sham_B
[View]
-
Color
2005-02-09 08:41:01
Deboche
[View]
-
Color
2005-04-14 03:23:06
Sham_B
[View]
-
Camera
2005-01-30 15:20:21
Solidus
[View]
-
Camera
2005-01-30 23:47:02
Sham_B
[View]
-
Additional AS Placement in the VCAM
2004-11-09 21:13:40
eff-werks
[View]
-
Additional AS Placement in the VCAM
2004-11-10 09:36:51
Sham_B
[View]
-
fisheye emulation
2004-12-29 20:12:35
mike9292
[View]
-
Additional AS Placement in the VCAM
2004-11-10 09:37:55
Sham_B
[View]
-
Export Issues.
2004-09-28 00:50:49
eff-werks
[View]
-
Export Issues.
2005-04-01 08:41:10
Sham_B
[View]
-
Export Issues.
2005-02-23 11:16:49
chadconger
[View]
-
Export Issues.
2004-09-28 07:43:09
Sham_B
[View]
-
Flash MX files
2004-09-03 02:42:51
Sham_B
[View]
-
Flash MX files
2004-09-14 03:09:10
garylarrson
[View]
-
Flash MX files
2004-09-28 07:58:55
Sham_B
[View]
-
Flash MX files
2004-09-28 11:03:14
Sham_B
[View]
-
Will it work in MX?
2004-09-02 19:32:44
Ramiel
[View]
-
Will it work in MX?
2005-01-30 23:49:31
Sham_B
[View]
-
Permission to use the components
2004-09-01 03:43:41
Sham_B
[View]
-
flash 5 version
2004-08-30 09:17:35
garylarrson
[View]
-
flash 5 version
2005-01-30 23:51:09
Sham_B
[View]
-
Action Script camera
2004-08-28 21:53:45
MC_Booga
[View]
-
camera rotation
2005-11-04 04:58:03
radja
[View]
-
Great Job
2006-07-22 09:36:05
Anonymou$
[View]

|