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
April 30, 2009
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment