Here is another small change that happened from Actionscript 2.0 to Actionscript 3.0 .
The method startDrag of the MovieClip class has been ( in my opinion ) significantly improved without major changes.
Here are those changes?
Within Actionscript 2.0, you could control the drag of a MovieClip using, in fact, startDrag with its 5 parameters :
lock center: true or false, specifies if, while dragging the MovieClip, its centre corresponds to the Mouse centre ( true ) or the point where the click happened ( false ).
left: leftmost drag point
top: uppermost drag point
right: rightmost drag point
bottom: lowermost drag point
Within Actionscript 3.0, the drag is controlled by passing an instance of the Rectangle class to the startDrag method, which now accepts only 2 parameters:
lock center: as in Actionscript 2.0
bounds: Rectangle class instance
In order to use the bounds parameter, it?s sufficient to create a virtual rectangle remembering that only 4 simple parameters are needed to create a Rectangle object:
x: the x coordinate where the rectangle begins
y: the y coordinate where the rectangle begins
width: the width of the rectangle
height: its height
So, as you can see in the class I?ve written:
Code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
public class Controllo extends MovieClip
{
private var rect:Rectangle;
public function Controllo()
{
init();
}
private function init():void
{
stage.frameRate=31;
ball_mc.x=sfondo_mc.x;
ball_mc.y=sfondo_mc.y;
ball_mc.buttonMode=true;
initRectangle();
initListener();
}
private function initRectangle():void
{
rect=new Rectangle(sfondo_mc.x-sfondo_mc.width/2+ball_mc.width/2,
sfondo_mc.y-sfondo_mc.height/2+ball_mc.height/2,
sfondo_mc.width-ball_mc.width,
sfondo_mc.height-ball_mc.height);
}
private function initListener():void
{
ball_mc.addEventListener(MouseEvent.MOUSE_DOWN,iniziaDrag);
stage.addEventListener(MouseEvent.MOUSE_UP,stoppaDrag);
}
private function iniziaDrag(e:MouseEvent):void
{
ball_mc.startDrag(false,rect);
}
private function stoppaDrag(e:MouseEvent):void
{
ball_mc.stopDrag();
}
}
}
The result:
In this case, instead of creating a sort of virtual rectangle with new coordinates, I?ve passed the coordinates of sfondo_mc to the startDrag method.
Stay tuned !