舉例:
上面的flash展示源代碼:
import index.base.geom.Dot; import index.base.events.DotEvent;
var po1:Dot = new Dot(0,0,true); var po2:Dot = new Dot(0,0,true); po1.bind(p1,true); po2.bind(p2,true);
po1.addEventListener(DotEvent.DOT_CHANGE,dotChangeFun); po2.addEventListener(DotEvent.DOT_CHANGE,dotChangeFun);
function dotChangeFun(e:DotEvent):void{ te.text = "物體1坐標(biāo):" + po1.x + "," + po1.y; te.appendText("\n物體2坐標(biāo):" + po2.x + "," + po2.y); te.appendText("\n兩點(diǎn)之間距離:" + po1.from(po2)); te.appendText("\n所形成的角度:" + po1.angle(po2)); te.appendText("\n物體1所在象限:" + po1.quadrant(new Dot,false)); te.appendText("\n物體2所在象限:" + po2.quadrant(new Dot,false)); te.appendText("\n物體1對(duì)于物體2在象限:" + po2.quadrant(po1)); }
p1.addEventListener(MouseEvent.MOUSE_DOWN,p1MouseDownFun); p2.addEventListener(MouseEvent.MOUSE_DOWN,p2MouseDownFun);
function p1MouseDownFun(e:MouseEvent):void{ p1.startDrag(); stage.addEventListener(MouseEvent.MOUSE_UP,p1MouseUpFun); }
function p1MouseUpFun(e:MouseEvent):void{ p1.stopDrag(); stage.removeEventListener(MouseEvent.MOUSE_UP,p1MouseUpFun); }
function p2MouseDownFun(e:MouseEvent):void{ p2.startDrag(); stage.addEventListener(MouseEvent.MOUSE_UP,p2MouseUpFun); }
function p2MouseUpFun(e:MouseEvent):void{ p2.stopDrag(); stage.removeEventListener(MouseEvent.MOUSE_UP,p2MouseUpFun); }
下面是類(lèi)的源代碼:
package index.base.geom{ import flash.events.EventDispatcher; import flash.display.DisplayObject; import index.base.events.DotEvent; public class Dot extends EventDispatcher{ private var _x:Number; private var _y:Number; private var dis:DisplayObject; public var isListen:Boolean; public function Dot(x_:Number = 0,y_:Number = 0,_isListen:Boolean = false){ _x = x_; _y = y_; isListen = _isListen; } //綁定DisplayObject public function bind(_dis:DisplayObject,isInTime:Boolean = false):void{ dis = _dis; updata(); if(isInTime) dis.addEventListener("enterFrame",enterFrameFun); } //幀頻繁事件 private function enterFrameFun(e:Object):void{ if(_x != dis.x) x = dis.x; if(_y != dis.y) y = dis.y; } //更新xy數(shù)據(jù) public function updata():void{ if(dis != null){ _x = dis.x; _y = dis.y; } } //計(jì)算該點(diǎn)與另外一點(diǎn)的距離 public function from(_dot:Dot,isQuadrant:Boolean = false):Number{ updata(); var num:Number = Math.sqrt(Math.pow(_dot.x - _x,2) + Math.pow(_dot.y - _y,2)); if(!isQuadrant) num = Math.abs(num); return num; } //計(jì)算該點(diǎn)與另外一點(diǎn)所形成的線段與水平線的夾角,按順時(shí)間計(jì)算 public function angle(_dot:Dot,isRadian:Boolean = false):Number{ updata(); var numx:Number = _dot.x - _x; var numy:Number = _dot.y - _y; var num:Number = Math.atan(numy/numx); if(!isRadian) num = num * 180 / Math.PI; return num; } //返回當(dāng)前點(diǎn)處在另外一點(diǎn)的哪個(gè)象限中 或 返回另外一點(diǎn)處在當(dāng)前點(diǎn)的哪個(gè)象限中 public function quadrant(_dot:Dot,isMaster:Boolean = true):int{ updata(); if(_x == _dot.x || _y == _dot.y){ return 0; } var num:int; var p1:Boolean = (_x - _dot.x) > 0; var p2:Boolean = (_y - _dot.y) > 0; num = isMaster ? (p1 ? (p2 ? 2 : 3) : (p2 ? 1 : 4)) : (p1 ? (p2 ? 4 : 1) : (p2 ? 3 : 2)); return num; } //返回該點(diǎn)距0點(diǎn)的距離 public function get length():Number{ updata(); var num:Number = Math.sqrt(Math.pow(_x,2) + Math.pow(_y,2)); return num; } //清除顯示對(duì)象 public function clear():void{ dis = null; } //改變X坐標(biāo) public function set x(num:Number):void{ _x = num; if(dis != null) dis.x = num; if(isListen) dispatchEvent(new DotEvent(DotEvent.DOT_CHANGE,true)); } //設(shè)置X坐標(biāo) public function get x():Number{ updata(); return _x; } //改變Y坐標(biāo) public function set y(num:Number):void{ _y = num; if(dis != null) dis.y = num; if(isListen) dispatchEvent(new DotEvent(DotEvent.DOT_CHANGE,true)); } //設(shè)置Y坐標(biāo) public function get y():Number{ updata(); return _y; } } }
事件類(lèi)的代碼:
package index.base.events{ import flash.events.Event; public class DotEvent extends Event{ public static const DOT_CHANGE:String = "dotChange"; public function DotEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false){ super(type,bubbles,cancelable); } } }
經(jīng)典論壇交流: http://bbs.blueidea.com/thread-2842487-1-1.html
本文鏈接:http://www.95time.cn/tech/multimedia/2008/5706.asp
出處:藍(lán)色理想
責(zé)任編輯:bluehearts
上一頁(yè) Dot類(lèi) [1] 下一頁(yè)
◎進(jìn)入論壇RIA設(shè)計(jì)與應(yīng)用版塊參加討論
|