SoFunction
Updated on 2025-04-14

AS3 self-written notes sorted out Dot class


package { 

  import ; 
  import ; 

  import ; 

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

//Binding DisplayObject
    public function bind(_dis:DisplayObject,isInTime:Boolean = false):void{ 
      dis = _dis; 
      updata(); 
      if(isInTime) ("enterFrame",enterFrameFun); 
    } 

//Frequent frame events
    private function enterFrameFun(e:Object):void{ 
      if(_x != ) x = ; 
      if(_y != ) y = ; 
    } 

//Update xy data
    public function updata():void{ 
      if(dis != null){ 
        _x = ; 
        _y = ; 
      } 
    } 

//Calculate the distance between this point and another point
    public function from(_dot:Dot,isQuadrant:Boolean = false):Number{ 
      updata(); 
      var num:Number = ((_dot.x - _x,2) + (_dot.y - _y,2)); 
      if(!isQuadrant) num = (num); 
      return num; 
    } 

//Calculate the angle between the line segment formed by this point and another point and the horizontal line, and calculate it in time.
    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 = (numy/numx); 
      if(!isRadian) num = num * 180 / ; 
      return num; 
    } 

//Return to which quadrant of the current point is in another point or return to which quadrant of the current point is in another point
    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; 
    } 

//Return the distance from this point to 0 points
    public function get length():Number{ 
      updata(); 
      var num:Number = ((_x,2) + (_y,2)); 
      return num; 
    } 

//Clear the display object
    public function clear():void{ 
      dis = null; 
    } 

//Change X coordinate
    public function set x(num:Number):void{ 
      _x = num; 
      if(dis != null)  = num; 
      if(isListen) dispatchEvent(new DotEvent(DotEvent.DOT_CHANGE,true)); 
    } 

//Set X coordinate
    public function get x():Number{ 
      updata(); 
      return _x; 
    } 

//Change Y coordinate
    public function set y(num:Number):void{ 
      _y = num; 
      if(dis != null)  = num; 
      if(isListen) dispatchEvent(new DotEvent(DotEvent.DOT_CHANGE,true)); 
    } 

//Set Y coordinates
    public function get y():Number{ 
      updata(); 
      return _y; 
    } 
  }