April 30, 2009

AS3 Drag and Swap Objects

This tutorial will teach you drag and drop of objects and also it the depth control of objects. Refer sample swf file given below . In this you can click and drag any objects to anywhere and also the object which you clicked will have an highest depth .


Let’s start. Open new Flash cs3 file. Create Movie clips with different shapes, and placed in a stage and give an instance name. In property window give class name as “drag” .
Now we will start coding part. Create new action script file and name it as “drag”. And paste the following code in this

package {

import flash.display.*;
import flash.events.MouseEvent;
public class drag extends Sprite {
var object:Sprite;
var offsetX:Number;
var offsetY:Number;
var snapToCenter:Boolean = false;
public function drag():void {
mcButterfly.addEventListener(MouseEvent.MOUSE_DOWN,startdrag);
mcButterfly.addEventListener(MouseEvent.MOUSE_UP,stopdrag);
mcSquare.addEventListener(MouseEvent.MOUSE_DOWN,startdrag);
mcSquare.addEventListener(MouseEvent.MOUSE_UP,stopdrag);
mcCircle.addEventListener(MouseEvent.MOUSE_DOWN,startdrag);
mcCircle.addEventListener(MouseEvent.MOUSE_UP,stopdrag);
mcSpade.addEventListener(MouseEvent.MOUSE_DOWN,startdrag);
mcSpade.addEventListener(MouseEvent.MOUSE_UP,stopdrag);
mcHeart.addEventListener(MouseEvent.MOUSE_DOWN,startdrag);
mcHeart.addEventListener(MouseEvent.MOUSE_UP,stopdrag);
mcButterfly.buttonMode=true;
mcSquare.buttonMode=true;
mcCircle.buttonMode=true;
mcSpade.buttonMode=true;
mcHeart.buttonMode=true;

}
function startdrag(evt:MouseEvent) {
evt.target.startDrag();

swap(evt.target);

}
function stopdrag(evt:MouseEvent) {
evt.target.stopDrag();
}
function swap(o:*):void {
var highestDepth:uint = numChildren - 1;
setChildIndex(o, highestDepth);

if (snapToCenter) {
offsetX = 0;
offsetY = 0;

o.x = mouseX;
o.y = mouseY;
} else {
offsetX = o.x - mouseX;
offsetY = o.y - mouseY;
}
}
}
}

Thats all . now run your fla file. you can Download sourcefile from here

0 comments:

Post a Comment

 

About

we are doing all kind of web / standalone applications using Flash , FMS, PHP , XML , CSS , etc. we handle all projects starting from the initial design concept through the delivery of your personalized product. Our commitment to you is comprehensive and complete-aimed at satisfying your every need. We have the experience and the expertise to get the job done.

Site Info

we Currently handling web requirements and multimedia productions and we have the capability of cutting your web , graphics and multimedia expenses to a bare minimum without sacrificing professional quality or delaying production. Our focus is on the development of smart, successful ideas and we always strive to maintain the highest . we currently using following technologies Flash 8, Flash cs3,Actionscript 2 ,Action script 3 ,Flash Media server , PHP,AMFPHP,XML ,HTML ,CSS ,Photoshop

Text

Digizone, AnnaNagar East, Chennai, TamilNadu ,India. Please view our website for further details

Flash and photoshop resources FREE Download Copyright © 2009 Community is Designed by Bie