AS3实例:框选对象

[img]http://dl.iteye.com/upload/attachment/0066/1784/bd081d38-da73-3065-bd3b-9330c699cb53.jpg[/img]


[img]http://dl.iteye.com/upload/attachment/0066/1786/b93e8cd9-5c01-37c1-98b6-ef4e943bc02f.jpg[/img]


代码:



package
{
import com.duowan.util.DrawUtil;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.TextField;

/**
* ...
* @author hacker47
*/
public class SelectMain extends Sprite
{
private var p0:Point = new Point();
private var drawWidth:Number;
private var drawHeight:Number;

public function SelectMain()
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
}

private function onDown(e:MouseEvent):void {
p0.x=mouseX;
p0.y= mouseY;
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
}

private function onMove(e:MouseEvent):void {
drawWidth = mouseX - p0.x;
drawHeight = mouseY -p0.y;
graphics.clear();
DrawUtil.drawRect(graphics, p0, drawWidth, drawHeight);
}

private function onUp(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
checkRange();
}

private function checkRange():void {
if (drawWidth < 0) {
drawWidth = -drawWidth;
p0.x -= drawWidth;
}

if (drawHeight < 0) {
drawHeight = -drawHeight;
p0.y -= drawHeight;
}

for (var i:int = 0; i < this.numChildren; i++ ) {
var d:DisplayObject = getChildAt(i);
var rect:Rectangle = d.getRect(this);
if (rect.x > p0.x && rect.x < (p0.x + drawWidth)) {
if (rect.y > p0.y && rect.y < (p0.y + drawHeight)) {
selectedDraw(rect);
}
}

}
}

private function selectedDraw(rect:Rectangle):void {
graphics.lineStyle(2, 0x336699);
graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
}

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值