一楼的代码有个问题:就是向右下角拉的时候鼠标会在矩形框右下角的顶点上。下面的代码可以解决此问题:
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
public class select extends Sprite {
public var MouseStart:Array;
public var SelectSprite:Sprite;
public var FirstSelect:Boolean;
public function select():void {
stage.addEventListener(MouseEvent.MOUSE_DOWN,selectstart);
}
public function selectstart(event:MouseEvent):void {
FirstSelect=true;
MouseStart=[event.stageX,event.stageY];
stage.addEventListener(MouseEvent.MOUSE_MOVE,selecting);
stage.addEventListener(MouseEvent.MOUSE_UP,selectend);
}
public function selecting(event:MouseEvent):void {
if (!FirstSelect) this.removeChild(SelectSprite); //清除以前的选择框
FirstSelect=false;
var ax:int=MouseStart[0];
var ay:int=MouseStart[1];
var aw:int=event.stageX-ax;
var ah:int=event.stageY-ay;
SelectSprite=new Sprite();
SelectSprite.graphics.beginFill(0x3399ff,0.4);
SelectSprite.graphics.lineStyle(1,0x3399ff);
SelectSprite.graphics.drawRect(ax-1,ay-1,aw,ah);
SelectSprite.graphics.endFill();
this.addChild(SelectSprite);
}
public function selectend(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE,selecting);
stage.removeEventListener(MouseEvent.MOUSE_UP,selectend);
if (!FirstSelect) this.removeChild(SelectSprite);
}
}
}