码迷,mamicode.com
首页 > 其他好文 > 详细

链条处理

时间:2016-03-24 06:31:19      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

package framework.utilt
{
    import flash.display.DisplayObject;
    import flash.geom.Point;
    import flash.net.LocalConnection;
    import flash.system.System;

    public class Utilts
    {
        /** 
         * AS3冒泡排序算法 
         * @param arr 需要排序的数组 
         * @param isAsc 是否升序 
         * @return 返回排序后的数组 
         */  
        public static function BubbleSort(arr:Array, isAsc:Boolean = true):Array  
        { 
            if(arr == null)  
            {  
                throw new Error("Array is null.");  
            }  
            var tmp:*;  
            if(isAsc)  
            {  
                for(var i:int = 0; i < arr.length; i++)  
                {  
                    for(var j:int = i; j < arr.length; j++)  
                    {  
                        if(arr[i] > arr[j])  
                        {  
                            tmp = arr[i];  
                            arr[i] = arr[j];  
                            arr[j] = tmp;  
                        }  
                    }  
                }  
            }  
            else  
            {  
                for(i = 0; i < arr.length; i++)  
                {  
                    for(j = i; j < arr.length; j++)  
                    {  
                        if(arr[i] < arr[j])  
                        {  
                            tmp = arr[i];  
                            arr[i] = arr[j];  
                            arr[j] = tmp;  
                        }  
                    }  
                }  
            }  
            return arr;
        }
        
        /**
         *flash gc 
         */
        public static function GC():void{
            trace(System.totalMemory/1024,"gc");
            try {  
                new LocalConnection().connect(‘foo‘);
                new LocalConnection().connect(‘foo‘);  
            } catch (e:*) {} 
            //    强制执行垃圾回收进程。 
            //        仅限于 Flash Player 调试版和 AIR 应用程序。在 AIR 应用程序中,System.gc() 方法仅限在运行于
            //        AIR Debug Launcher (ADL) 中的内容或已安装的应用程序的应用程序安全沙箱中的内容中启用。
            //System.gc();//Debug版本有效
        }
        
        private static function getRotation(p1:Point,p2:Point):Number
        {            
            var r:Number=Math.PI/2;            
            if(p1.x!=p2.x)
            {            
                r=Math.atan((p1.y-p2.y)/(p1.x-p2.x));            
            }
            return r;
        }
        
        private static function radianToAngle(radian:Number):Number
        { 
            return radian*(180/Math.PI);
        }

        /**
         * @param _st
         * @param _end
         * @return 
         */
        public static function GetRotation(_st:Point,_end:Point):Number
        {
            var angle:Number = getRotation(_st,_end);            
            var last:Number = radianToAngle(angle);
            if(_end.x - _st.x < 0)
            {
                last -= 180;
            }
            return last;
        }
        
        /**
         *生成角度,长度 
         * @param list
         * @return 
         * 
         */        
        public static function GetLink(list:Array):Array
        {
            if(!list || list.length < 2)
            {
                throw new Error("LinkMC error!");
            }
            var res:Array = [];
            var len:int = list.length;
            for(var i:int = 0;i < len - 1;i++)
            {
                var s:Point = list[i];
                var e:Point = list[i+1];
                
                var angle:Number =    GetRotation(s,e);
                var dis:Number =  Point.distance(s,e);
                
                var o:Object = new Object();
                o.ang = angle;
                o.d = dis;
                o.x = s.x;
                o.y = s.y;
                res[i] = o;
            }
            return res;
        }
    }
}
package
{
    import flash.display.Bitmap;
    import flash.display.DisplayObject;
    import flash.display.Graphics;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.utils.getDefinitionByName;
    
    import framework.baseUI.JButton;
    import framework.ini.SignIni;
    import framework.loader.ElementLoader;
    import framework.utilt.Utilts;
    
    public class Test0 extends Sprite
    {
//        private var bmp:Bitmap;
        
        private var _bg:Sprite;
        private var list:Array;
        private var _class:Class;
        
        public function Test0()
        {
            list=[];
            
            var btn:JButton = new JButton(onBtnMouseEvt,"build","build",true);
            addChild(btn);
            
            var clearbtn:JButton = new JButton(onBtnMouseEvt,"clear","clear",true);
            clearbtn.y = 50;
            addChild(clearbtn);
            
            
//            graphics.beginFill(0xff0000,0.1);
//            graphics.drawRect(0,0,800,600);
//            graphics.endFill();
            
            _bg = new Sprite();
            _bg.x = 100;
            _bg.graphics.beginFill(0xff0000,0.1);
            _bg.graphics.drawRect(0,0,800,600);
            _bg.graphics.endFill();
            _bg.addEventListener(MouseEvent.CLICK,onMouseEvt);
            addChild(_bg);
            
            
            ElementLoader.instance.load("0.swf",SignIni.TYPE_SWF,onComplete);
        }
        
        private function onBtnMouseEvt(e:MouseEvent):void
        {
            if((e.target as JButton).name == "build")
            {
                var arr:Array =    Utilts.GetLink(list);
                for(var i:int = 0;i < arr.length;i++)
                {
                    var o:Object = arr[i];
                    addArrow(o.ang,o.d,o.x,o.y);
                }
            }
            else
            {
            
            }
        }
        
        private function addArrow(ang:Number,dis:Number,x:Number,y:Number):void
        {
//            var mc:MovieClip = new _class() as MovieClip;
            
            var mc:Sprite = new Sprite();
            mc.graphics.beginFill(0xffff00,0.5);
            mc.graphics.drawRect(0,0,dis,20);
            mc.mouseEnabled = false;
            mc.rotation = ang;
//            mc.width = dis;
            mc.x = x;
            mc.y = y;
            _bg.addChild(mc);
        }
        
        private function onMouseEvt(e:MouseEvent):void
        {
//            trace(e.localX );
            var _x:Number = e.localX
            var _y:Number = e.localY;
            
            
            var g:Graphics =    _bg.graphics;
            g.beginFill(0xff0000);
            g.drawCircle(_x,_y,2);
            g.endFill();
            
            list.push(new Point(_x,_y));
        }
        
        
        private function onComplete(dis:DisplayObject):void
        {
//            bmp = dis as Bitmap;
//            addChild(bmp);
            var c:Class = getDefinitionByName("com.arrow") as Class;
//            var mc:MovieClip = new c() as MovieClip;
//            mc.mouseEnabled = false;
//            mc.width = 600;
//            addChild(mc);
            
            _class = c;
        }
    }
}
import flash.display.Shape;

class Cir extends Shape
{
    public function Cir()
    {
        graphics.beginFill(0xff0000);
        graphics.drawCircle(0,0,5);
        graphics.endFill();
    }
}

 

链条处理

标签:

原文地址:http://www.cnblogs.com/jiangjieqim/p/5313888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!