首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

AS3范例:框选对象

2012-07-02 
AS3实例:框选对象代码:package{import com.duowan.util.DrawUtilimport flash.display.DisplayObjectimp

AS3实例:框选对象









代码:

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);}}}

热点排行