天天看點

AS3常用代碼片

1、全屏語句

stage.displayState = StageDisplayState.FULL_SCREEN; 

2、廣播事件

//偵聽事件
EventDispatcherEx.dispatcher.addEventListener("returnManyou", ljTo);

function ljTo(e){
	//ljt.gotoAndStop(p.frameNum);
	//執行相應的操作	
}

//廣播事件
EventDispatcherEx.dispatcher.dispatchEvent(new Event("returnManyou"));

//EventDispatcherEx 類
package {
	import flash.events.EventDispatcher;
	
	public class EventDispatcherEx{
		public static const dispatcher:EventDispatcher = new EventDispatcher;
		
		public function EventDispatcherEx(){
			
		}
	}
}
           

3、加載外部背景音樂并循環播放

import flash.media.SoundChannel;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.events.Event;

var soundM:Sound=new Sound();
var soundct:SoundChannel=new SoundChannel();
soundM.load(new URLRequest("bgmusic.mp3"));
soundct=soundM.play();
soundct.addEventListener(Event.SOUND_COMPLETE,onComplete);

function onComplete(eve:Event):void
{
	soundct=soundM.play();
	soundct.addEventListener(Event.SOUND_COMPLETE,onComplete);
} 
           

4、引用OSMultiTouch.swc庫實作兩點觸摸效果

import com.lylib.touch.OSMultiTouch;
import com.lylib.touch.gestures.DirectionGesture;
import com.lylib.touch.events.DirectionEvent;
import com.lylib.touch.gestures.*;
import com.lylib.touch.events.ZoomEvent;
import com.lylib.touch.events.RotateEvent;
import com.lylib.touch.events.*;

var multiTouch:OSMultiTouch = OSMultiTouch.getInstance();

multiTouch.enableGesture (tvClip1, new ZoomGesture(), onZoomGesture); //縮放
multiTouch.enableGesture (tvClip1, new RotateGesture(), onRotateGesture); //旋轉
multiTouch.enableGesture (tvClip1, new DragMoveGesture(), onDragGesture); //拖動

function onZoomGesture (e:ZoomEvent):void
{
	e.target.scaleX *=  e.deltaScale;
	e.target.scaleY *=  e.deltaScale;
}

function onRotateGesture (e:RotateEvent):void
{
	e.target.rotation +=  e.deltaAngle * 180 / Math.PI;
}

function onDragGesture (e:DragMoveEvent):void
{
	e.target.x +=  e.deltaOffsetX;
	e.target.y +=  e.deltaOffsetY;
}
           

5、fla中視訊檔案聲音大小控制語言

//fla中視訊音量控制
var ylkz:SoundTransform = new SoundTransform();
ylkz.volume = num;   //num為你想要的音量大小0~1
tvmc.soundTransform = ylkz;   //tvmc 為加載了一個視訊的MovieClip影片
           

6、在解除安裝子swf時清空記憶體(聽說不算好,但好像還是有用的)

function GC() 
{
    try { 
         var lc1:LocalConnection = new LocalConnection(); 
         var lc2:LocalConnection = new LocalConnection(); 
         lc1.connect('name'); 
         lc2.connect('name2'); 
    }catch (e:Error){ 
        
	} 
}
           

7、360度序列幀撥動播放

stop();
var preMouse:Number; //滑鼠位置
var frameNum:int = 1; //目前幀
var dist:Number = 8; //在屏上滑動多遠距離跳一幀 
this.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
this.addEventListener(MouseEvent.MOUSE_UP,upHandler);
this.addEventListener(MouseEvent.MOUSE_OUT,outHandler);

function downHandler(event:MouseEvent):void
{
	preMouse = mouseX;
	this.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
}

function moveHandler(event:MouseEvent):void
{
	if(mouseX - preMouse > dist)
	{
		frameNum ++;
		if(frameNum < totalFrames){
			this.nextFrame();
		}else{
			this.gotoAndStop(1);
			frameNum = 1;
		}
		preMouse = mouseX;
	}
	
	if(mouseX - preMouse < -dist)
	{
		frameNum --;
		if(frameNum > 1){
			this.prevFrame();
		}else{
			this.gotoAndStop(totalFrames);
			frameNum = totalFrames;
		}
		preMouse = mouseX;
	}
}

function upHandler(event:MouseEvent):void
{
	this.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
}

function outHandler(event:MouseEvent):void
{
	this.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
}
           

2013-08-16

字元串去掉前後空格

//var src:String=" Hello! ";  
//trace("\""+src+"\"");    //原文本  
//trace("\""+src.replace(/^\s*/g,"")+"\"");    //去掉前面的空格  
//trace("\""+src.replace(/\s*$/g,"")+"\"");    //去掉後面的空格 
           

2014-05-04

儲存圖檔到本地,有彈出檔案儲存框的方式,主要用到FileReference類,感謝 flash023

//import com.adobe.images.JPGEncoder;
function save(defaultFileName:String = null):void {
        var _fileRef:FileReference=new FileReference();//用于儲存檔案
        var _encoder:JPGEncoder=new JPGEncoder(80);//用于編碼位圖
        var bitmapData:BitmapData=new BitmapData(stage.stageWidth,stage.stageHeight);
        bitmapData.draw(this);//得到位圖
        var ba:ByteArray=_encoder.encode(bitmapData);//編碼成JPG圖檔,品質為80
        _fileRef.save(ba, defaultFileName);//儲存到磁盤,會出現個系統儲存對話框。
        ba.clear();
}
function onClick(_evt:MouseEvent ):void {
        save("flash023.jpg");
}
bg.addEventListener("click",onClick);
           

2016-08-10 

根據url打開浏覽器

var url:URLRequest = new URLRequest("http://www.shao-ming.cn");
 navigateToURL(url,"_blank");
           

2019-08-15

AS3 mc轉換成Bitmap

将mc轉換成bitmap首先得建立一個BitmapData,使用BitmapData的draw方法或是BitmapData.copyPixels方法繪制。 

var mc:MovieClip = new MovieClip(); 

var mBit:BitmapData = new BitmapData(mc.width,mc.height, true, 0xffffff); 

mBit.draw(mc); 

var bitmap:Bitmap = new Bitmap(mBit);