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);


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jswm20150115

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值