前言:flash小技巧備忘
======================================================================================
卸載拖入舞臺的MC
我們知道只有動態創建的mc纔可以使用removeMovieClip()方法來卸載,幫助中對removeMovieClip方法的說明如下:
Quote
刪除用 duplicateMovieClip()、MovieClip.duplicateMovieClip() 或
MovieClip.attachMovie() 創建的影片剪輯實例。
下面的技巧可以讓我們隨心所欲的卸載你需要的MC,即使是你拖入舞臺的組件等。
1 : My_mc.swapDepths(100);//把mc的深度值設定爲大於0的值
2 : My_mc.removeMovieClip();//這樣就可以正常卸載mc了
是不是很有用哦。
PS:使用my_mc.unloadMovie() 以可以刪除mc實例,但是卻保留了實例屬性和剪輯處理函數。用removeMovieClip() 可以刪除實例(包括其屬性和剪輯處理函數)。
TextFormat.URL
很多人都知道使用textField.htmlText來寫超文本的方式,可是似乎很少人知道其實我們還可以使用TextFormat.URL來定義。
1 : myFormat = new TextFormat();
2 : myFormat.url = http://www.google.cn;
3 : my_txt.html = true; //這是必須的
4 : my_txt.htmlText = "kaikai";
5 : my_txt.setTextFormat(myFormat);
鼠標光標
多數情況下,需要用到鼠標光標在主場景中的位置,所以可以使用_root._xmouse和_root._ymouse表示。
onClipEvent (enterFrame) {
_x = _root._xmouse;
_y = _root._ymouse;
}//讓影片剪輯和鼠標保持相同座標
將矩形影片剪輯作爲遮罩圖形
在矩形影片剪輯的動作面板中添加如下ActionScript:
onClipEvent (load) {
xs = _width;
ys = _height;
}
onClipEvent (enterFrame) {
_xscale = 100*(_root._xmouse-_x)/(xs/2);
_yscale = 100*(_root._ymouse-_y)/(ys/2);
}
在load事件中,用變量xs和ys取得影片剪輯初始狀態下的寬度和高度值。在enterFrame事件中,計算鼠標位置與矩形影片剪輯中心 的距離,算出它與矩形寬度和高度值的比值,然後作爲_xscale和_yscale的值。該動畫實現移動鼠標位置以改變遮罩效果
鼠標雙擊問題!
lsn_DbClick = new Object();
lsn_key = new Object();
lsn_DbClick.onMouseDown = function() {
clickTime = getTimer();
if (clickTime-lastClickTime<=300) {
trace("雙擊了鼠標");
}
lastClickTime = clickTime;
};
Mouse.addListener(lsn_DbClick);
time = getTimer()-down_time;
if (time<300) {
//雙擊後的代碼部分
}
down_time = getTimer();
怎樣在從另一個swf中獲取變量
loadMovieNum("vars.swf", 1["POST"]);
_root.onEnterFrame = function() {
trace(_level1.myvab);
};
//當加載完畢後,輸出就有值了...
鼠標右鍵及滾輪的代碼
if Key.isDown(2){},(左鍵的代碼是1)
左鍵:Key.isToggled(1)
右鍵:Key.isToggled(2)
滾輪:Key.isToggled(4)
滾輪1:Key.isToggled(4) PC機上用
滾輪2:Key.isToggled(3) MAC機上用
未知:Key.isToggled(3)
this.onEnterFrame = function() {
if (Key.isDown(2)){
trace("asdfjsdjkf");
}
}
顯示幀速
function get_fps() {
var a = this.createEmptyMovieClip("fps_show", 100000);
a.createTextField("fps_text", 0, 0, 0, 16, 18);
a.fps_text.background = true;
a._fps = 0;
a.oldtim = getTimer();
a.onEnterFrame = function () {
this._fps = Math.round(1000 / (getTimer() - this.oldtim));
this.oldtim = getTimer();
this.fps_text.text = this._fps;
};
}
get_fps();
顯示時間
function get_time() {
var a = this.createEmptyMovieClip("time_show", 200412);
a.createTextField("time_text", 0, 0, 0, 140, 18);
a.time_text.background=true;
a.onEnterFrame = function () {
this.time_text.text = new Date().getFullYear()+"年"+new Date().getMonth()+"月"+new Date().getDay()+"日"+new Date().getHours()+":"+new Date().getMinutes()+":"+new Date().getSeconds();
};
}
get_time();
將十進制數N轉換成d進制:N=(N div d)*d + N mod d
function jh_conversion(N, d) {
var s = [], e="";
while (N>0) {
s.push(N%d);
N = Math.floor(N/d);
}
while (s.length>0) {
e += s.pop();
}
trace(e);
return e;
}
AS填充,並獲得填充區點陣
function get_fill(mc){
var s_j=mc._x;
var e_j=mc._x+mc._width;
var s_i=mc._y;
var e_i=mc._y+mc._height;
//
var a=mc.createEmptyMovieClip("fill",1);
var fill_data=[];
for(var i=0;i<100;i++){
fill_data[i]=[];
for(var j=0;j<25;j++){
if(mc.hitTest(i,j,true)){
a.lineStyle(1,random(0xffffff));
a.moveTo(i,j);
a.lineTo(i+.5,j);
fill_data[i][j]=1;
}else{
fill_data[i][j]=0;
}
}
}
for(var i in fill_data){
fill_data[i]=fill_data[i].join("");
}
fill_data=fill_data.join(",");
trace(fill_data);
return fill_data;
}
簡單代碼實現MC與鼠標成反向移動
onEnterFrame = function(){
a._x+=(270-_xmouse)*.01;
}