Thursday, May 29, 2008

Using StartDrag Method In ActionScript 3

The startDrag method makes a specified sprite draggable. The sprite remains draggable until it is explicitly stopped by the sprite.startDrag() method or until some other sprite is made draggable. We can make only one sprite draggable at a time.

The following example demonstrates the use of startDrag method

import flash.display.Sprite
import flash.events.MouseEvent
var circle:Sprite=new Sprite();
circle.graphics.beginFill(0xFFCC00);
circle.graphics.drawCircle(225,225,20);
addChild(circle);
circle.addEventListener(MouseEvent.MOUSE_DOWN,dragmethod);

function dragmethod(Event:MouseEvent):void{
circle.startDrag();
}


The first line imports the class Sprite from the disply package. The second line then imports
the mouseEvent class from event package. Then we create circle sprite.
We have use the graphics property of the sprite in the next line to draw and fill the circle .
The beginFill graphic method takes octal color value as an argument and the draw circle takes three arguments which are x postion, y postion and the radius of circle respectively.
Then we add this circle sprite on the stage by using the addChild method.

The addEventListner method is used to register the listner function(dragmethod)
with the target of the event(circle sprite). we can name this listnerfunction withany valid identifier.
Then we create the listner function(dragmethod) which contains the statement circle.startDrag()
which makes the circle draggable when user clicks the circle.

No comments: