var domain:String = LoaderInfo(this.root.loaderInfo).parameters.domain; Security.allowDomain(domain); Security.allowInsecureDomain(domain); //地址参数 c.swf?path=website.swf Stage.path //检测播放器版本,适用大于8.5版 flash.system.Capabilities.version field.restrict = "0-9\\^"; //允许数字和^ field.restrict = "0-9\\\\"; //允许数字和反斜杠 //图片彩色变黑白 import flash.filters.ColorMatrixFilter; var matrix:Array = [ 0.5,0.5,0.082,0,0,0.5,0.5,0.082,0,0,0.5,0.5,0.082,0,0,0,0,0,1,0 ]; var colorMat:ColorMatrixFilter = new ColorMatrixFilter(matrix); image_mc.filters = [colorMat]; //创建xml的几种方式 //方法一 var xmlstr:String="wangchunlei"; var myxml:XML=new XML(xmlstr); //方法二 var myxml:XML=new XML(wangchunlei); //方法三 var myxml:XML=wangchunlei; //方法四 var ids:Array = ["background", "photoArea", "thumbnail"]; var d:XML = new XML(""); for (var i:int = 0; i < 3; i++){ d = d.appendChild() } //声音加载中估算长度 import flash.events.Event; import flash.media.Sound; import flash.net.URLRequest; var snd:Sound = new Sound(); snd.load(new URLRequest("http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3")); var channel:SoundChannel=snd.play(); addEventListener(Event.ENTER_FRAME, onEnterFrame); snd.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete); function onEnterFrame(event:Event):void { var estimatedLength:int = Math.ceil(snd.length / (snd.bytesLoaded / snd.bytesTotal)); var playbackPercent:uint = Math.round(100 * (channel.position / estimatedLength)); trace("Sound playback is " + playbackPercent + "% complete."); } function onPlaybackComplete(event:Event) { trace("The sound has finished playing."); removeEventListener(Event.ENTER_FRAME, onEnterFrame); } //程序延迟执行 import flash.events.TimerEvent; import flash.utils.Timer; /** * delay function * a quick and easy delay function that can call a function with parameters. configurable * with delay time and repeat frequency * * @param func:Function The function to call when timer is complete * @param params:Array An array of parameters to pass to the function * @param delay:int [OPTIONAL] The number of milliseconds to wait before running the function * @param repeat:int [OPTIONAL] The number of times the function should repeat */ function delay(func:Function, params:Array, delay:int = 350, repeat:int = 1):void { var f:Function; var timer:Timer=new Timer(delay,repeat); timer.addEventListener(TimerEvent.TIMER, f = function():void{ func.apply(null, params); if (timer.currentCount == repeat){ timer.removeEventListener(TimerEvent.TIMER, f); timer = null; } } ); timer.start(); } /* int比uint速度快 Array的遍历要比Object或者Dictionary的枚举要快得多 if (myObj != null) 要比 if (myObj) 的速度要慢 for (var i:* in myObj) 比 for (var i:String in myObj) 要快 var myText:String = "a" + "b" + "c"; 比 var myText2:String = [ "a", "b", "c" ].join(""); 速度快 */ //Dictionary类和Object唯一的区别在于:Dictionary对象可以使用非字符串作为键值对的键。 //使用标签可以从内循环体中直接break掉外循环体 var i:Number,j:Number; mainLoop:for (i=0; i<10; i) { for (j=0; j<10; j) { if (i>3&&j>3) { break mainLoop; } } } 获取10以内的随机整数 Math.random()*10>>0 移位符号 >>0 的功能去除小数点后的数,又如进度条: (已经加载的字节数/总字节数)*100>>0 "%" function fn1(){trace("1");} function fn2(){trace("2");} ([fn1,fn2][Math.random()*2>>0])(); 这个是随机执行函数的命令 if(i&1){}。i&1 的意思就是判断 i是奇数还是偶数 如果是奇数 i 的2进制表示最后一位是1, i&1=1 真;偶数 i 的2进制表示最后一位是0, i&1=0 假。 for(var i:int=0;i<32;i++){ arr[i]=getChildByName("Buff_"+i); } //及时清理事件侦听 private var listeners:Array = new Array(); override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void{ super.addEventListener(type, listener, useCapture, priority, useWeakReference); var newListener:Object = {lType:type, lListener:listener}; listeners.push( newListener ); } //调用此方法以清除全部已添加了的事件侦听器 public function clear():void{ for each( var listener:Object in listeners){ this.removeEventListener( listener.lType, listener.lListener ); } listeners = []; } 在/**和*/字符包裹中的字都会被当做是ASDoc说明文字,你还可以使用一些特殊记号,以'@'符号开头,常用的有: @author 编写者姓名 @param 函数的变量 @return 函数返回值 ASDoc支持使用HTML标签;ASDoc会自动匹配下一个被声明了的对象 在MXML标签上写标签对来替代AS中的/** */标签对,只对设置有ID的组件才能匹配 //加载外部SWF的Class var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete); loader.load(new URLRequest("main.swf")); function onComplete(e:Event):void { var domain:ApplicationDomain=e.target.applicationDomain as ApplicationDomain; var swfClass:Class=domain.getDefinition("MainSwf") as Class; var swfInstance:Sprite = (new swfClass()) as Sprite; swfInstance.x=0; this.addChild(swfInstance); } function ColorTest(r:uint,g:uint,b:uint) { var color:uint; //直接用位操作合成颜色 color=r<<16|g<<8|b; trace( "#" + color.toString(16) ); //返回16进制的值,不过该值会忽略前导的0 //获取颜色分量 var r:uint = color>>16; var g:uint = color>>8 & 0xff; var b:uint = color & 0xff; trace( "16进制分量还原结果,红:" + r.toString("16") + ",绿:" + g.toString("16") + ",蓝:" + b.toString("16") ); } var txtClass:Class = getDefinitionByName("flash.text.TextField") as Class; var txtInstance:TextField = new txtClass() as TextField; txtInstance.text = "通过反射动态创建的TextField"; addChild(txtInstance); //如果某对象的一个事件同时添加多个监听,stopPropagation将把该事件注册的所有监听处理函数执行完后,再阻止该事件继续向上冒泡; //而stopImmediatePropagation方法将本次处理函数执行完后就立即阻止事件继续向上冒泡(即同一事件的其它监听函数将不会执行)。 _child.addChild(_sub_child); addChild(_child); _child.addEventListener(MouseEvent.MOUSE_DOWN,childMouseDownHandler); //注意:这里同时添加了二个监听,并设置了优点级(subChildMouseDownHandler2的优先级高于subChildMouseDownHandler1) _sub_child.addEventListener(MouseEvent.MOUSE_DOWN,subChildMouseDownHandler1,false,1); _sub_child.addEventListener(MouseEvent.MOUSE_DOWN,subChildMouseDownHandler2,false,2); function subChildMouseDownHandler1(e:MouseEvent):void{ trace("_sub_child.MOUSE_DOWN_1"); } function subChildMouseDownHandler2(e:MouseEvent):void{ //_sub_Child上点击输出:_sub_child.MOUSE_DOWN_2,_sub_child.MOUSE_DOWN_1,_child.MOUSE_DOWN //e.stopImmediatePropagation(); //_sub_Child上点击输出:_sub_child.MOUSE_DOWN_2 //e.stopPropagation(); //_sub_Child上点击输出:_sub_child.MOUSE_DOWN_2,_sub_child.MOUSE_DOWN_1 trace("_sub_child.MOUSE_DOWN_2"); } function childMouseDownHandler(e:MouseEvent):void{ trace("_child.MOUSE_DOWN"); } //flash cs中库元素类导出ButtonPlay package{ import flash.display.SimpleButton; public class Demo extends MovieClip { [Embed(source = "skin.swf",symbol = "ButtonPlay")] private var ButtonPlay:Class; public function Demo(){ var btnTest2:SimpleButton = new ButtonPlay() as SimpleButton; addChild(btnTest2); //Function的call和apply var jimmy:Object = new Object(); jimmy.addSalary = function(addSalary:uint):void{ trace("姓名:",this.name,",原工资:",this.salary,",新工资:",this.salary + addSalary); }; trace(jimmy.addSalary is Function); //true trace(typeof jimmy.addSalary); //function jimmy.name = "jimmy.yang"; jimmy.salary = 3000; jimmy.addSalary(500); //姓名: jimmy.yang ,原工资: 3000 ,新工资: 3500 var mike:Object = new Object(); mike.name = "Mike"; mike.salary = 5000; jimmy.addSalary.call(jimmy,1000); //姓名: jimmy.yang ,原工资: 3000 ,新工资: 4000 jimmy.addSalary.call(mike,1000); //姓名: Mike ,原工资: 5000 ,新工资: 6000 jimmy.addSalary.apply(mike,[1100]); //姓名: Mike ,原工资: 5000 ,新工资: 6100 _delegate = jimmy.addSalary; _delegate.call(mike,1999); //姓名: Mike ,原工资: 5000 ,新工资: 6999 _delegate.apply(mike,[2999]); //姓名: Mike ,原工资: 5000 ,新工资: 7999 //对于不经常用到的事件侦听使用弱引用(addEventListener方法第五个参数设为true) this.addEventListener(Event.ADDED_TO_STAGE, onAdded, false, 0, true); //Embed将绑定的内容转换成“二进制数据” [Embed(source="/assets/sample.xml", mimeType="application/octet-stream")] public var xmlCls:Class; ...... var byteDataXml:ByteArray = new xmlCls(); var xml:XML = XML(byteDataXml.readUTFBytes(byteDataXml.bytesAvailable)); trace(xml.toXMLString()); //添加换行进剪贴板中 var str:String = "1111111111"; str +=+"2222222222"; System.setClipboard(str); trace("拷贝至剪贴板中,请粘贴到记事本中查看")