常用代码(二)

1. MovieClip声音卸载

Code
this.addEventListener(Event.REMOVED_FROM_STAGE,removedFromStageHandler);

private function removedFromStageHandler(event:Event):void
{
    this.soundTransform = new SoundTransform(0);
}

 

2. 自定义事件

EventDispatcher是DisplayObject 类的基类,其实就是一个可以执行dispatchEvent的Object,适用于没有图形界面,而又希望执行事件的类,继承关系就是这样:

Sprite-->DisplayObjectContainer-->InteractiveObject-->DisplayObject-->EventDispatcher-->Object

MyEvent
package
{
    import flash.events.Event;

    
public class MyEvent extends Event
    {
        
public static const XOVER300:String = "x over 300 pixel";
        
public static const YOVER250:String = "y over 250 pixel";
        
public var param:String = "";
        
        
public function MyEvent(type:String)
        {
            super(type);
        }
    }
}

 

Rect
package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    
public class Rect extends Sprite
    {
        
public function Rect()
        {
            init();
        }
        
        
private function init():void
        {
            
this.graphics.beginFill(0xFF0000);
            
this.graphics.drawRect(0,0,30,30);
            
this.graphics.endFill();
            
            
this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
            
this.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
        }
        
        
private function mouseDownHandler(event:MouseEvent):void
        {
            
this.startDrag();
        }
        
        
private function mouseUpHandler(event:MouseEvent):void
        {
            
this.stopDrag();
            
            
if(this.x>300)
            {
                var myEvent:MyEvent 
= new MyEvent(MyEvent.XOVER300);
                myEvent.param 
= "当前X坐标:"+this.x;
                
this.dispatchEvent(myEvent);
            }
            
            
if(this.y>250)
            {
                
this.dispatchEvent(new MyEvent(MyEvent.YOVER250));
            }
        }

    }
}

 

文档类
package
{
    import flash.display.Sprite;
    
    
public class Sample0104 extends Sprite
    {
        
private var rect:Rect;
        
        
public function Sample0104()
        {
            rect 
= new Rect();
            rect.addEventListener(MyEvent.XOVER300,xover300Handler);
            rect.addEventListener(MyEvent.YOVER250,yover250Handler);
            
this.addChild(rect);
        }
        
        
private function xover300Handler(event:MyEvent):void
        {
            trace(
"x坐标超过了300!得到的参数是:"+event.param);
            
event.target.x = 50;
        }
        
        
private function yover250Handler(event:MyEvent):void
        {
            trace(
"y坐标超过了250!");
            
event.target.y = 50;
        }
    }
}

 

3. far命令打包asc

far -package -archive main -files 1.asc 2.asc 3.asc main.asc

 

4. 将swf以资源的形式嵌入

[Embed(source="bg.swf", mimetype="application/octet-stream")]
public var swfAsset:Class;

 

5. TextField添加图形/动画资源

Code
package
{
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.geom.Rectangle;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;

    public class TestTextField extends Sprite
    {
        private var textBox:TextField;
        
        public function TestTextField()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            var bg:Sprite = new Sprite();
            bg.graphics.beginFill(0xCCCCCC);
            bg.graphics.drawRect(0,0,300,600);
            bg.graphics.endFill();
            this.addChild(bg);
            
            textBox = new TextField();
            textBox.autoSize = TextFieldAutoSize.LEFT;
            textBox.multiline = true;
            textBox.wordWrap = true;
            textBox.width = 200;
            textBox.text = "/:11/:13The 我 胡Ì 是 /:02 ab the  /:03  with a  /:04/:02/:01  as whye  /:05 with a  /:06  on egg  /:07 with a  /:02  /:02  /:55 中华人民共和国的成立,标志着100多年来半殖民地半封建的旧中国历史的 /:02 结束,揭开了中国历史的新篇章,使一个占世界人口近四分之一的大国成/:88为独立自主的国家,人民从此成为国家的主人/:77";
            textBox.border = true;
            bg.addChild(textBox);
            
            replaceMarkToFace();
        }
        
        private function replaceMarkToFace():void
        {
            var reg:RegExp = /\/:[0-9]{2}/g;
            
            var arrSplit:Array = textBox.text.split(reg);
            
            var arrFace:Array = textBox.text.match(reg);
            
            textBox.htmlText = textBox.htmlText.replace(reg,"<font size=\"24\">胡</font>");
            
            var count:int = 0;
            var arrIndex:Array = new Array();
            for(var i:int=0;i<arrSplit.length;i++)
            {
                count++;
                var len:int = arrSplit[i].toString().length;
                count += len;
                arrIndex.push(count);
            }
            
            for(var j:int=0;j<arrIndex.length;j++)
            {
                var curIndex:int = arrIndex[j]-1;
                if(textBox.text.charCodeAt(curIndex)!=13)
                {
                    trace(textBox.height);
                    var startRect:Rectangle = textBox.getCharBoundaries(curIndex);
                    
                    var rect:Sprite  = new Sprite();
                    rect.graphics.beginFill(0x00FF00,.8);
                    rect.graphics.drawRect(0,0,startRect.width,startRect.height);
                    rect.graphics.endFill();
                    rect.x = startRect.x;
                    rect.y = startRect.y;
                    textBox.parent.addChild(rect);
                    
                    //add img or movieclip
                    var tmpText:TextField = new TextField();
                    tmpText.text = arrFace[j];
                    rect.addChild(tmpText);
                }
            }
        }
    }
}

 

with scrollbar
package
{
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    
    import org.aswing.ASColor;
    import org.aswing.AsWingConstants;
    import org.aswing.AssetPane;
    import org.aswing.JButton;
    import org.aswing.JScrollPane;
    import org.aswing.JViewport;
    import org.aswing.geom.IntDimension;
    
    public class Temp extends Sprite
    {
        private var textBox:TextField;
        private var pane:AssetPane;
        private var scrollPane:JScrollPane;
        
        public function Temp()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            textBox = new TextField();
            textBox.autoSize = TextFieldAutoSize.LEFT;
            textBox.multiline = true;
            textBox.wordWrap = true;
            textBox.width = 200;
            textBox.text = "ffffffffffffffffffffferewrfwerewfewwefwwrwwwwwwwwwwwww/:11/:13The 我 胡Ì 是 /:02 ab the  /:03  with a  /:04/:02/:01  as whye  /:05 with a  /:06  on egg  /:07 with a  /:02  /:02  /:55 中华人民共和国的成立,标志着100多年来半殖民地半封建的旧中国历史的 /:02 结束,揭开了中国历史的新篇章,使一个占世界人口近四分之一的大国成/:88为独立自主的国家,人民从此成为国家的主人/:77";
            textBox.background = true;
            textBox.backgroundColor = 0xFFFF00;
            
            pane = new AssetPane(textBox);
            scrollPane = new JScrollPane(pane,JScrollPane.SCROLLBAR_ALWAYS);
            scrollPane.setLocationXY(50,50);
            scrollPane.setPreferredSize(new IntDimension(300, 200));
            var viewPort:JViewport = scrollPane.getViewport() as JViewport;
            viewPort.setHorizontalAlignment(AsWingConstants.LEFT);
            viewPort.setVerticalAlignment(AsWingConstants.TOP);
            scrollPane.setOpaque(true);
            scrollPane.setBackground(ASColor.BLUE);
            scrollPane.pack();
            addChild(scrollPane);
            
            replaceMarkToFace();
            
            var btn:JButton = new JButton("确定");
            btn.setLocationXY(10,400);
            btn.addEventListener(MouseEvent.CLICK,onClick);
            btn.pack();
            addChild(btn);
        }
        
        private function onClick(event:MouseEvent):void
        {
            textBox.htmlText += "<p><a href='http://www.baidu.com'>老爸</a>喜欢养/:88花";
            
            replaceMarkToFace();
        }
        
        private function replaceMarkToFace():void
        {
            var reg:RegExp = /\/:[0-9]{2}/g;
            
            var arrSplit:Array = textBox.text.split(reg);
            
            var arrFace:Array = textBox.text.match(reg);
            
            textBox.htmlText = textBox.htmlText.replace(reg,"<font size=\"24\">胡</font>");
            
            var count:int = 0;
            var arrIndex:Array = new Array();
            for(var i:int=0;i<arrSplit.length;i++)
            {
                count++;
                var len:int = arrSplit[i].toString().length;
                count += len;
                arrIndex.push(count);
            }
            
            for(var j:int=0;j<arrIndex.length;j++)
            {
                var curIndex:int = arrIndex[j]-1;
                if(textBox.text.charCodeAt(curIndex)!=13&&curIndex<textBox.text.length)
                {
                    trace(textBox.height);
                    var startRect:Rectangle = textBox.getCharBoundaries(curIndex);
                    
                    var rect:Sprite  = new Sprite();
                    rect.graphics.beginFill(0x00FF00,.8);
                    rect.graphics.drawRect(0,0,startRect.width,startRect.height);
                    rect.graphics.endFill();
                    rect.x = startRect.x;
                    rect.y = startRect.y;
                    rect.mouseEnabled = false;
                    textBox.parent.addChild(rect);
                    
                    //var tmpText:TextField = new TextField();
                    //tmpText.text = arrFace[j];
                    //rect.addChild(tmpText);
                }
            }
            
            pane.reload();
            pane.pack();
            pane.y = -1*pane.height;
        }
    }
}

 

6. 自定义鼠标指针

MyCursor
package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.ui.Mouse;
    import flash.ui.MouseCursorData;

    public class MyCursor extends Sprite
    {
        [Embed(source="pen.png")]
        private var penImg:Class;
        
        public function MyCursor()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            if(Mouse.supportsNativeCursor)
            {
                var mouseVector:Vector.<BitmapData> = new Vector.<BitmapData>();
                var bmpData:BitmapData = (new penImg() as Bitmap).bitmapData;
                mouseVector.push(bmpData);
                
                var mcd:MouseCursorData = new MouseCursorData();
                mcd.data = mouseVector;
                //mcd.frameRate = 1;

                Mouse.registerCursor("drawCursor",mcd);
                Mouse.cursor  = "drawCursor";
            }
        }
    }
}

 

7. 实时绘画

Painter
package
{
    import com.adobe.serialization.json.JSON;
    
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.events.MouseEvent;
    import flash.events.NetStatusEvent;
    import flash.events.TimerEvent;
    import flash.net.NetConnection;
    import flash.net.SharedObject;
    import flash.utils.Timer;

    public class Painter extends Sprite
    {
        private var fmsConnect:NetConnection;
        private var line:Sprite;
        private var trackSO:SharedObject;
        private var trackList:String="";
        private var clearTime:Timer;
        private var isMouseDown:Boolean=false;
        
        public function Painter()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            fmsConnect = new NetConnection();
            fmsConnect.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
            fmsConnect.connect("rtmp://xxx.xxx.xxx/live");
            fmsConnect.client = this;
            
            line = new Sprite();
            line.graphics.lineStyle(2,0x0000FF);
            this.addChild(line);
            
            var rect:Sprite = new Sprite();
            rect.graphics.beginFill(0x00FF00);
            rect.graphics.drawRect(0,0,50,50);
            rect.graphics.endFill();
            rect.x = 300;
            rect.y = 100;
            rect.addEventListener(MouseEvent.CLICK,onClick);
            this.addChild(rect);
        }
        
        private function onClick(event:MouseEvent):void
        {
            trackList = "clear";
        }
        
        private function netStatusHandler(event:NetStatusEvent):void
        {
            switch(event.info.code)
            {
                case "NetConnection.Connect.Success":
                    stage.addEventListener(MouseEvent.MOUSE_DOWN,stage_mouseDownHandler);
                    stage.addEventListener(MouseEvent.MOUSE_UP,stage_mouseUpHandler);
                    stage.addEventListener(MouseEvent.MOUSE_MOVE,stage_mouseMoveHandler);
                    
                    trackSO = SharedObject.getRemote("trackRemote",fmsConnect.uri,false); 
                    trackSO.connect(fmsConnect);
                    
                    var trackTimer:Timer = new Timer(4000);
                    trackTimer.addEventListener(TimerEvent.TIMER,trackTimer_TimerHandler);
                    trackTimer.start();
                    
                    clearTime = new Timer(10000);
                    clearTime.addEventListener(TimerEvent.TIMER,clearTimer_TimerHandler);
                    clearTime.start();
                    break;
            }
        }
        
        private function stage_mouseDownHandler(event:MouseEvent):void
        {
            clearTime.reset();
            clearTime.start();
            
            isMouseDown = true;
            line.graphics.moveTo(mouseX,mouseY);
            
            var obj:Object = new Object();
            obj.time = flash.utils.getTimer();
            obj.x = mouseX;
            obj.y = mouseY;
            obj.type = "s";
            var str:String = encodeToJSON(obj);
            if(trackList=="clear")
            {
                trackList = "";
            }
            
            if(trackList=="")
            {
                trackList += str;
            }
            else
            {
                trackList += "|"+str;
            }
        }
        
        private function stage_mouseUpHandler(event:MouseEvent):void
        {
            isMouseDown = false;
        }
        
        private function stage_mouseMoveHandler(event:MouseEvent):void
        {
            if(isMouseDown)
            {
                clearTime.reset();
                clearTime.start();
                
                line.graphics.lineTo(mouseX,mouseY);
                
                var obj:Object = new Object();
                obj.time = flash.utils.getTimer();
                obj.x = mouseX;
                obj.y = mouseY;
                obj.type = "p";
                var str:String = encodeToJSON(obj);
                if(trackList=="clear")
                {
                    trackList = "";
                }
                
                if(trackList=="")
                {
                    trackList += str;
                }
                else
                {
                    trackList += "|"+str;
                }
            }
        }
        
        private function trackTimer_TimerHandler(event:TimerEvent):void
        {
            trackSO.setProperty("track",trackList);
        }
        
        private function clearTimer_TimerHandler(event:TimerEvent):void
        {
            trackList = "clear";
            trackSO.setProperty("track",trackList);
        }
        
        public function onBWDone():void
        {
            
        }
        
        public function onMetaData(...args):void
        {
            
        }
        
        public function encodeToJSON(obj:Object):String
        {
            return com.adobe.serialization.json.JSON.encode(obj);
        }
    }
}

 

Viewer
package
{
    import com.adobe.serialization.json.JSON;
    
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.events.NetStatusEvent;
    import flash.events.SyncEvent;
    import flash.net.NetConnection;
    import flash.net.SharedObject;
    import flash.utils.setTimeout;

    public class Viewer extends Sprite
    {
        private var fmsConnect:NetConnection;
        private var line:Sprite;
        private var trackSO:SharedObject;
        private var trackArray:Array;
        private var count:int = 0;
        
        public function Viewer()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;

            fmsConnect = new NetConnection();
            fmsConnect.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
            fmsConnect.connect("rtmp://xxx.xxx.xxx/live");
            fmsConnect.client = this;
            
            line = new Sprite();
            line.graphics.lineStyle(2,0x0000FF);
            line.graphics.moveTo(100,100);
            this.addChild(line);
        }
        
        private function netStatusHandler(event:NetStatusEvent):void
        {
            switch(event.info.code)
            {
                case "NetConnection.Connect.Success":
                    trackSO = SharedObject.getRemote("trackRemote",fmsConnect.uri,false);
                    trackSO.addEventListener(SyncEvent.SYNC,syncHandler);
                    trackSO.connect(fmsConnect);
                    break;
            }
        }
        
        private function syncHandler(event:SyncEvent):void
        {
            if(!trackSO.data["track"])
            {
                return;
            }
            var str:String = trackSO.data["track"].toString();
            if(str=="clear")
            {
                count=0;
                if(trackArray!=null)
                {
                    trackArray.splice(0);
                }
                return;
            }
            trackArray = str.split("|");
            trackArray.reverse();
            if(count>0)
            {
                trackArray.splice(trackArray.length-count);
            }
            
            if(trackArray.length>1)
            {
                var objInfo1:Object = com.adobe.serialization.json.JSON.decode(trackArray[trackArray.length-1].toString());
                var objInfo2:Object = com.adobe.serialization.json.JSON.decode(trackArray[trackArray.length-2].toString());
                
                if(objInfo1.type=="s")
                {
                    line.graphics.moveTo(objInfo1.x,objInfo1.y);
                }
                else
                {
                    line.graphics.lineTo(objInfo1.x,objInfo1.y);
                }
                
                count++;
                var timeSpan:int = objInfo2.time - objInfo1.time;
                trackArray.splice(trackArray.length-1,1);
                setTimeout(draw,timeSpan);
            }
        }
        
        private function draw():void
        {
            if(trackArray.length>1)
            {
                var objInfo1:Object = com.adobe.serialization.json.JSON.decode(trackArray[trackArray.length-1].toString());
                var objInfo2:Object = com.adobe.serialization.json.JSON.decode(trackArray[trackArray.length-2].toString());
                if(objInfo1.type=="s")
                {
                    line.graphics.moveTo(objInfo1.x,objInfo1.y);
                }
                else
                {
                    line.graphics.lineTo(objInfo1.x,objInfo1.y);
                }
                count++;
                var timeSpan:int = objInfo2.time - objInfo1.time;
                trackArray.splice(trackArray.length-1,1);
                
                setTimeout(draw,timeSpan);
            }
            else if(trackArray.length==1)
            {
                var objInfo3:Object = com.adobe.serialization.json.JSON.decode(trackArray[trackArray.length-1].toString());
                if(objInfo3.type=="s")
                {
                    line.graphics.moveTo(objInfo3.x,objInfo3.y);
                }
                else
                {
                    line.graphics.lineTo(objInfo3.x,objInfo3.y);
                }
                count++;
                trackArray.splice(0);
            }
        }
        
        public function onBWDone():void
        {
            
        }
        
        public function onMetaData(...args):void
        {
            
        }
        
        public function encodeToJSON(obj:Object):String
        {
            return com.adobe.serialization.json.JSON.encode(obj);
        }
    }
}

 

8. 播放RTMP服务器上的视频

package
{
    import flash.display.Sprite;
    import flash.events.NetStatusEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    
    public class PlayVideo extends Sprite
    {
        private var nc:NetConnection;
        private var ns:NetStream;
        private var video:Video;
        
        public function PlayVideo()
        {
            trace("init");
            nc = new NetConnection();
            nc.connect("rtmp://fms.xxx.com:25/vod");
            nc.client=this;
            nc.addEventListener(NetStatusEvent.NET_STATUS,onStatusHandler);
        }
        
        private function onStatusHandler(evt:NetStatusEvent):void
        {
            trace(evt.info.code);
            if(evt.info.code=="NetConnection.Connect.Success")
            {
                ns=new NetStream(nc);
                ns.addEventListener(NetStatusEvent.NET_STATUS,onStatusHandler);
                ns.client=this;
                video=new Video();
                video.attachNetStream(ns);
                ns.play("video/testconn");
                addChild(video);
            }
        }
        
        public function onPlayStatus(info:Object):void 
        { 
            trace("onPlayStatus:"+info.code);
            if(info.code == "Netstream.Play.Complete"){
                trace("Complete")
            }
        } 
        
        public function onBWDone():void
        {
            
        }
        
        public function onMetaData(...args):void
        {
            
        }
    }
}
PlayVideo

 

 

 

posted @ 2010-12-20 11:49  CoderWayne  阅读(1223)  评论(0编辑  收藏  举报