Here’s the code for a ‘zoom’ effect for a flash movie which zooms into any spot the viewer clicked, then back out once the mouse is released. You can see it in action in the flash movie ‘Disco Penguin‘.
The main core of the code is at Auto Zoom at FlashKik.com. It was submitted by T Norman at N T Designs. The Actionscript is as follows:
myZoom = function (myDepth) {
if (myDepth == "deep") {
myDepth = 4;
} else if (myDepth == "medium") {
myDepth = 8;
} else if (myDepth == "light") {
// default
myDepth = 12; }
trace(myDepth);
_root.onMouseDown = function() {
if (k>0) { return; } zoom = true;
dir == 1 ? (dir=-1) : (dir=1);
if (dir == 1) {
pt = {x:_root._xmouse, y:_root._ymouse}; } };
this.onEnterFrame = function() {
if (!zoom) { return; }
_root._xscale += dir*k*50/myDepth;
_root._yscale += dir*k*50/myDepth;
var pt2 = {x:pt.x, y:pt.y};
_root.localToGlobal(pt2);
_root._x -= (pt2.x-pt.x);
_root._y -= (pt2.y-pt.y);
k++; if (k == 8) {
zoom = false; k = 0; } }; };
myZoom("medium");
It had the zoom effect but didn’t zoom out on mouse release. The actionscript in this movie required the user to click a second time to zoom back out. An onMouseUp event was needed to switch the direction of the zoom. The enterframe operation is never halted, it runs continuously. The variable “k” is used as an increment in the scale equation:
_root._xscale += dir*k*50/myDepth;
The if statement just resets the increment variable to 0 if it reaches 8. The variable itself just acts as a counter. The direction of the zoom determines whether the scale is in or out. If dir = -1… the scale would be the same as _root._xscale-=value. If dir=1 _root._xscale+=value.
In the end, to get a zoom in effect with zoom out on mouse release the actionscript needed to be as follows:
myZoom = function (myDepth) {
if (myDepth == "deep") {
myDepth = 4;
} else if (myDepth == "medium") {
myDepth = 8;
} else if (myDepth == "light") {
// default
myDepth = 12; }trace(myDepth);
_root.onMouseDown = function() {
if (k>0) {
return; }
zoom = true;
dir == 1 ? (dir=-1) : (dir=1);
if (dir == 1) {
pt = {x:_root._xmouse, y:_root._ymouse};
scale = true; }
if (dir == -1) {
scale = false;
} }
_root.onMouseUp = function() {
if (k>0) {
return;
zoom = false; }
dir == 1 ? (dir=-1) : (dir=1);
zoom = true; }
this.onEnterFrame = function() {
if (!zoom) {
return; }
_root._xscale += dir*k*50/myDepth;
_root._yscale += dir*k*50/myDepth;
var pt2 = {x:pt.x, y:pt.y};
_root.localToGlobal(pt2);
_root._x -= (pt2.x-pt.x);
_root._y -= (pt2.y-pt.y);
k++;
if (k == 8) {
zoom = false;
k = 0;
} } }
//Usage
myZoom("medium");