常用代码(一)
1. 拉选框
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
public class AreaRect extends Sprite
{
private var rect:Sprite;
private var point:Point;
public function AreaRect()
{
rect = new Sprite();
stage.addChild(rect);
stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
}
private function onMouseDown(evt:MouseEvent):void
{
point = new Point(mouseX,mouseY);
stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
}
private function onMouseMove(evt:MouseEvent):void
{
rect.graphics.clear();
rect.graphics.lineStyle(2,0x0A7CCA,1);
rect.graphics.drawRect(point.x,point.y,mouseX-point.x,mouseY-point.y);
}
private function onMouseUp(evt:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
}
}
}
2. FMS文本聊天
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init()">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
private var nc:NetConnection;
private var so:SharedObject;
private function init():void
{
nc = new NetConnection();
nc.connect("rtmp://localhost/virtualShow");
nc.client = this;
nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
}
private function onNetStatus(evt:NetStatusEvent):void
{
trace(evt.info.code);
if(evt.info.code=="NetConnection.Connect.Success")
{
so = SharedObject.getRemote("myMsg",nc.uri,false);
so.addEventListener(SyncEvent.SYNC,onSYNC);
so.connect(nc);
}
}
private function onSYNC(evt:SyncEvent):void
{
for(var i:uint=0;i<evt.changeList.length;i++)
{
switch(evt.changeList[i].code)
{
case "change":
updateList();
break;
case "clear":
break;
case "success":
break;
default:
break;
}
}
}
private function updateList():void
{
msgList.text += so.data.msg+"\n";
}
protected function onClick(event:MouseEvent):void
{
so.setProperty("msg",txtInput.text);
msgList.text+=txtInput.text+"\n";
txtInput.text="";
}
public function onBWDone():void{}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:TextArea id="msgList" x="173" y="37" width="300" height="150"/>
<s:TextInput id="txtInput" x="173" y="218" width="209"/>
<s:Button id="btnPost" x="399" y="219" label="按钮" click="onClick(event)" />
</s:Application>
3. FMS视频聊天
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init()">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.core.UIComponent;
private var nc:NetConnection;
private var mic:Microphone;
private var cam:Camera;
private function init():void
{
//加载自己的摄像头
mic = Microphone.getMicrophone();
cam = Camera.getCamera();
var video:Video = new Video();
video.attachCamera(cam);
var uiBox:UIComponent = new UIComponent();
uiBox.addChild(video);
this.addElement(uiBox);
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
nc.connect("rtmp://192.168.1.16/virtualShow");
nc.client = this;
}
private function onNetStatus(evt:NetStatusEvent):void
{
trace(evt.info.code);
if(evt.info.code=="NetConnection.Connect.Success")
{
var ns:NetStream = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
ns.attachAudio(mic);
ns.attachCamera(cam);
ns.client = this;
ns.publish("hxw","live");
}
}
public function onBWDone():void
{
}
]]>
</fx:Script>
</s:Application>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init()">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.core.UIComponent;
private var nc:NetConnection;
private function init():void
{
nc = new NetConnection();
nc.connect("rtmp://192.168.1.16/virtualShow");
nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
nc.client = this;
}
private function onNetStatus(evt:NetStatusEvent):void
{
var ns:NetStream = new NetStream(nc);
ns.play("hxw");
var video:Video = new Video();
video.attachNetStream(ns);
var uiBox:UIComponent = new UIComponent();
uiBox.addChild(video);
this.addElement(uiBox);
}
public function onBWDone():void
{
}
]]>
</fx:Script>
</s:Application>
4. PopupManager父窗体传值给子窗体
{
//var titleWindow:TitleWindow = PopUpManager.createPopUp(this, UploadImage, true) as TitleWindow;
var titleWindow:UploadImage = PopUpManager.createPopUp(this, UploadImage, true) as UploadImage;
titleWindow.uploadType = "一个值";
PopUpManager.centerPopUp(titleWindow);
}
private function onClick(event:MouseEvent):void
{
Alert.show(uploadType);
}
5. PopupManager子窗体传值给父窗体
{
//var titleWindow:TitleWindow = PopUpManager.createPopUp(this, UploadImage, true) as TitleWindow;
var titleWindow:UploadImage = PopUpManager.createPopUp(this, UploadImage, true) as UploadImage;
titleWindow.addEventListener("tw_click",update);
PopUpManager.centerPopUp(titleWindow);
}
private function update(event:Event):void
{
Alert.show("测试");
}
{
dispatchEvent(new Event("tw_click"));
}
6. Bitmap的翻转函数
public static function upanddown(bt:BitmapData):BitmapData
{
var bmd:BitmapData = new BitmapData(bt.width, bt.height, true, 0x00000000);
for (var xx:int=0; xx<bt.width; xx++)
{
for (var yy:int=0; yy<bt.height; yy++)
{
bmd.setPixel32(xx, bt.height-yy-1, bt.getPixel32(xx,yy));
}
}
return bmd;
}
/**水平翻转*/
public static function rightandleft(bt:BitmapData):BitmapData
{
var bmd:BitmapData = new BitmapData(bt.width, bt.height, true, 0x00000000);
for (var yy:int=0; yy<bt.height; yy++)
{
for (var xx:int=0; xx<bt.width; xx++)
{
bmd.setPixel32(bt.width-xx-1, yy, bt.getPixel32(xx,yy));
}
}
return bmd;
}
/**90翻转*/
public static function turn90(bt:BitmapData):BitmapData
{
var bmd:BitmapData = new BitmapData(bt.height, bt.width, true, 0x00000000);
for (var yy:int=0; yy<bt.height; yy++)
{
for (var xx:int=0; xx<bt.width; xx++)
{
bmd.setPixel32(yy,bt.width-xx, bt.getPixel32(xx,yy));
}
}
return bmd;
}
/**270翻转*/
public static function turn270(bt:BitmapData):BitmapData
{
var bmd:BitmapData = new BitmapData(bt.height, bt.width, true, 0x00000000);
for (var yy:int=0; yy<bt.height; yy++)
{
for (var xx:int=0; xx<bt.width; xx++)
{
bmd.setPixel32(bt.height-yy-1,bt.width-xx, bt.getPixel32(xx,yy));
}
}
return bmd;
}
7. Flex打包swc文件
FlexSDK中的compc命令可以将那些带包名的as文件编译成swf文件,并打包成一个文件,文件的扩展名是.swc,在Flash中这叫组件。其实组件文件格式实际上是zip, 把扩展名.swc改成.zip用winrar打开看看就知道了
假如是自己开发,可以先在本地把目录规划好,调试无误以后在单独拿出去打包
假如在"D:\项目\Tool\huxiaowei\classes"目录下有两个AS文件"GameTask.as","MovieTask.as",在Adobe Flex 3 SDK Command Prompt命令行下输入:
compc -include-sources huxiaowei\classes\ output d:/hxwLib.swc
这样就将两个AS文件打包到d:\下名为hxwLib.swc包里了
在项目里就可以删除掉AS文件,只需要再ActionScript Build Path -> Library Path -> Add SWC下应用这个hxwLib.swc即可
PS:如果有一个AS是基于hxwLib.swc编写的,如果要将这个as编译成不必依赖组件的swf(即可以单独存在),可以进一步在命令行运行:
flashplayer HelloWorld.swf
8. 阻止事件冒泡
Event提供两个方法来阻止事件冒泡,evt.stopImmediatePropagation()和evt.stopPropagation()
如果某对象的一个事件同时添加多个监听,stopPropagation将把该事件注册的所有监听处理函数执行完后,再阻止该事件继续向上冒泡;而stopImmediatePropagation方法将本次处理函数执行完后就立即阻止事件继续向上冒泡(即同一事件的其它监听函数将不会执行)。
如果某对象的一个事件有多个监听,stopPropagation()将处理完所有的监听后,再阻止该事件冒泡;而stopImmediatePropagation()仅处理本次监听,然后立即阻止事件冒泡
{
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.events.TextEvent;
public class Test extends Sprite
{
public function Test()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
var box:Sprite = new Sprite();
this.addChild(box);
with(box)
{
graphics.beginFill(0xCCCCCC);
graphics.drawRect(0,0,200,200);
graphics.endFill();
addEventListener(MouseEvent.CLICK,onBoxClick);
}
var item:Sprite = new Sprite();
box.addChild(item);
with(item)
{
graphics.beginFill(0x000000);
graphics.drawRect(0,0,100,100);
graphics.endFill();
x = y = 50;
addEventListener(MouseEvent.CLICK,onItemClick,false,1);
addEventListener(MouseEvent.CLICK,onItemClick2,false,2);
}
}
private function onBoxClick(evt:MouseEvent):void
{
trace("box click");
}
private function onItemClick(evt:MouseEvent):void
{
trace("item click 1");
}
private function onItemClick2(evt:MouseEvent):void
{
trace("item click 2");
//不再执行onItemClick
//evt.stopImmediatePropagation();
//还是会执行onItemClick
evt.stopPropagation();
}
}
}
9. 遍历对象属性
obj.aa = "AAA";
obj.bb = "BBB";
obj.cc = "CCC";
for(var str:String in obj)
{
trace("属性"+str+"的值是:"+obj[str]);
}
//只能获得属性的value,不能获得key
for each(var p:String in obj) {
trace(p);
}
10. 用代码去执行一个对象的点击事件