View Full Version : mouse effect.

08-24-2010, 10:00 AM
anybody here have an action script for mouse forward effect.

Example: a big house background in flash and you need to forward your mouse up, side and down to be able to view the other part of the house. how to do that?

Ravi Bhadauria
02-20-2011, 10:24 AM
// Import the MatrixTransformer class
import fl.motion.MatrixTransformer;

Initially hide the zoom in/out cursors. Note that mcIn and mcOut were created on the stage and converted to movieclips using Modify > Convert to
Symbol with the registration point set in the center. We converted the header text to a movieclip as well, mcHeader, to make it invisible while
the image is loading.
mcIn.visible = false;
mcOut.visible = false;

//We created a dynamic text field, infoBox, to display loading information.


// 'board' provides a background on which the image resides.
var board:Sprite=new Sprite();

// We create variables for the width and height of the board so we can scale the picture accordingly.
// These settings should be chosen with consideration of the pixel dimensions of the original picture.
var boardWidth:int=538;
var boardHeight:int=300;

//The position of 'board' will detrmine the position of your picture within the main movie.

/* The board will have a background so we can have the whole board listen for mouse events. The "image sprite" created below will be a child of the board.
board.graphics.drawRect(0,0,boardWidth,boardHeight );

spImage is a Sprite on which we will place the image.
spImage is a Sprite so we can attach listeners for response to mouse events to implement drag & drop.
var spImage:Sprite = new Sprite();

// Create a mask for the spImage with shape identical to the board.
// This will keep a scaled or moved picture from "leaving" the designated board area.
var boardMask:Shape=new Shape();
boardMask.graphics.drawRect(0,0,boardWidth,boardHe ight);
boardMask.x = 0;
boardMask.y = 0;
spImage.mask = boardMask;

// scaleFactor for the proportion of scaling corresponding to each 'click' of the mouse wheel.
// The value 0.8 reflects 80% scaling when zooming out and 125% scaling when zooming in.
var scaleFactor:Number = 0.8;
var minScale:Number = 0.25;
var maxScale:Number = 2.0;

var image:Bitmap;

// Declare variable for loading of image from a file.

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE,initPic);
loader.contentLoaderInfo.addEventListener(Progress Event.PROGRESS,updateInfo);

infoBox.text="Loading begins";

//Replace Kazanie_Skargi by the name of your image or the address of your image.
loader.load(new URLRequest("Kazanie_Skargi.jpg"));

function updateInfo(e:ProgressEvent):void {

infoBox.text="Loading: "+String(Math.floor(e.bytesLoaded/1024))+" KB of "+String(Math.floor(e.bytesTotal/1024))+" KB.";


function initPic(e:Event):void {





minScale = boardWidth/image.width;


// We add the image and the cursor movie clips to spImage. Since spImage is masked, so will its children.

spImage.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

// Clean up listeners & loader now that the load is complete.
loader.contentLoaderInfo.removeEventListener(Event .COMPLETE,initPic);
loader.contentLoaderInfo.removeEventListener(Progr essEvent.PROGRESS,updateInfo);

// Drop shadows are always cool
board.filters=[ new DropShadowFilter() ];

// A click on the image sprite is handled by the zoom function below.
spImage.addEventListener(MouseEvent.CLICK, zoom);

// Event handlers for mouse_down and mouse_up that enable basic drag-and-drop functionality.
function startDragging(mev:MouseEvent):void {

function stopDragging(mev:MouseEvent):void {

// Event handler for click event that performs zooming in and out via MatrixTransformer methods.
function zoom(mev:MouseEvent):void {
// If it is not the case that exactly one of the shift key or ctrl key is depressed, then we do nothing.
if ((!mev.shiftKey)&&(!mev.ctrlKey)) {
if ((mev.shiftKey)&&(mev.ctrlKey)) {

// Declare a variable for the transform matrix of the image object.
var mat:Matrix;

Get the point at the mouse in terms of image coordinates and (its parent) spImage coordinates. These points are aligned when the mouse click happens but after image is scaled, they will not be aligned anymore.
var externalCenter:Point=new Point(spImage.mouseX,spImage.mouseY);
var internalCenter:Point=new Point(image.mouseX,image.mouseY);

// We scaled the image up (but bounded by maxScale) or down (bounded by minScale) depending on whether the shift key
// or the ctrl key is down while the mouse click happens.
if (mev.shiftKey) {
image.scaleX = Math.max(scaleFactor*image.scaleX, minScale);
image.scaleY = Math.max(scaleFactor*image.scaleY, minScale);
if (mev.ctrlKey) {
image.scaleX = Math.min(1/scaleFactor*image.scaleX, maxScale);
image.scaleY = Math.min(1/scaleFactor*image.scaleY, maxScale);
The mat matrix is the transformation matrix for the scaled version of image; i.e., the version that no longer has internalCenter and external center aligned.
mat = image.transform.matrix.clone();

The matchInternalPointWithExternal method of the MatrixTransformer class changes the mat matrix to include a
translation that makes the point in terms of image coordinates line up with the point in terms of spImage
MatrixTransformer.matchInternalPointWithExternal(m at,internalCenter,externalCenter);

// Now applying the mat matrix to the enlarged image will align the internalCenter to the externalCenter.

// We use the ctrl and shift keys to display the two different cursors that were created on the stage.
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);

function keyHandler(ke:KeyboardEvent):void {
// No matter what key is pressed or released we move the cursor movie clips to the mouse position.
mcIn.x = spImage.mouseX;
mcIn.y = spImage.mouseY;
mcOut.x = spImage.mouseX;
mcOut.y = spImage.mouseY;

// The visible cursor will correctly correspond to a depressed key.
mcIn.visible = ke.ctrlKey;
mcOut.visible = ke.shiftKey;

// If either key (ctrl or shift) is depressed, we hide the mouse.
if (ke.ctrlKey || ke.shiftKey) {
} else {

Ravi Bhadauria
ADMEC Multimedia Institute