作用域在set2中改變了AS2。只需確保在回調中執行trace(this)
,並且您會發現它不是_root,如預期的那樣,但您可以將容器movieclip作爲額外參數傳遞給回調函數,以便您可以添加庫項目。
此外,代碼看起來複雜,沒有理由:
var instName;//keep track of the new instance for each clip
var num=0;//number of symbols
setInterval(createSym,100);//create a symbol every 100 milliseconds
function createSym(){
instName="sym"+num++;//update instance name
this.attachMovie("sym",instName,this.getNextHighestDepth());//attach a new clip
eval(instName)._x=100;//set initial position
eval(instName)._y=100;
var t1=setInterval(moveSym,8,instName);//add another interval to move the symbol
function moveSym(instName){
eval(instName)._x+=1;
}
var t2=setInterval(checkSym,1,instName);//and another inverval to check if the clip is 'outside' limits, clear intervals and remove clip
function checkSym(instName){
if(eval(instName)._x>=600){
clearInterval(t1);
clearInterval(t2);
eval(instName).removeMovieClip();
}
}
}
每個人都有一個編碼風格,所以沒有一個正確/錯誤在這一點上,只要它的工作。以下是我改寫了它,所以它會爲我做的意義:
var clips:Array = [];
var currentClips:Number = 0;
var totalClips:Number = 100;
setInterval(update,40,this);//interval is at 40 ms ~ 25 fps, also pass a target movie clip to attache library items into
function update(targetClip:MovieClip) {
if(currentClips < totalClips){//still need clips ?
var clip:MovieClip = targetClip.attachMovie('sym','sym'+currentClips,targetClip.getNextHighestDepth());//add a clip
clip._x = 100;//initialize position
clip._y = Math.random() * 100;
clips.push(clip);//update array and clips counter
currentClips++;
}
//update existing clips
for(var i:Number = 0 ; i < currentClips; i++) {
clips[i]._x+=10;
if(clips[i]._x > Stage.width) {//if a clips is outsite, remove it, update the array and counter, and another should be created instead
clips[i].removeClip();
clips.splice(i,1);
currentClips--;
}
}
}
注意剪輯被刪除,每次添加他們退出舞臺,這可能需要一些資源,如果我們簡單地重複使用,可以被保存同樣的符號通過重新定位它:
var clips:Array = [];
var currentClips:Number = 0;
var totalClips:Number = 100;
setInterval(update,40,this);//interval is at 40 ms ~ 25 fps, also pass a target movie clip to attache library items into
function update(targetClip:MovieClip) {
if(currentClips < totalClips){//still need clips ?
var clip:MovieClip = targetClip.attachMovie('sym','sym'+currentClips,targetClip.getNextHighestDepth());//add a clip
clip._y = Math.random() * 100;//initialize position
clips.push(clip);//update array and clips counter
currentClips++;
}
//update existing clips
for(var i:Number = 0 ; i < currentClips; i++) {
clips[i]._x+=10;
if(clips[i]._x > Stage.width) clips[i]._x = 0;//reuse same clips, simply update position
}
}
而且,我發現它看起來並不很好玩的是,所以我增加了對速度(_vx)的變量,因爲影片剪輯是一個動態類,你可以添加運行時的屬性。請注意,這不是一個好的做法。我們的目標是變得有點深度的方式剪輯動畫:深度
var clips:Array = [];
var currentClips:Number = 0;
var totalClips:Number = 100;
setInterval(update,40,this);//interval is at 40 ms ~ 25 fps, also pass a target movie clip to attache library items into
function update(targetClip:MovieClip) {
if(currentClips < totalClips){//still need clips ?
var clip:MovieClip = targetClip.attachMovie('sym','sym'+currentClips,targetClip.getNextHighestDepth());//add a clip
clip._y = Math.random() * 100;//initialize position
clip._vx = 5 + Math.random() * 5;//clips have different velocities - give a bit of depth
clips.push(clip);//update array and clips counter
currentClips++;
}
//update existing clips
for(var i:Number = 0 ; i < currentClips; i++) {
clips[i]._x += clips[i]._vx;
if(clips[i]._x > Stage.width) clips[i]._x = 0;//reuse same clips, simply update position
}
}
來講,剪輯是不是深度排序,但我岔開... 關於你問題_root和範圍,與setInterval的問題,因爲範圍更改,但您可以使用回調參數來解決問題。
非常感謝你的所有建議:)我必須承認我的風格很差。但有一件事仍然無法解釋(或者可能是我沒有弄清楚) - 如何調用一個不同的函數(mycaller),然後調用我想調用的函數(createSym)來重置範圍? – toofast1227 2011-03-24 16:28:16
@backslash這不是你,這是「他們」:P我不是100%肯定AVM1(ActionScript虛擬機中運行AS2字節碼)如何處理setInterval的內部,但它足以知道,通過setInterval的調用函數內部範圍的變化。例如:trace(this); var interval:Number = setInterval(update,100); function update(){ \t trace(this); \t clearInterval(interval); } – 2011-03-24 17:35:03
此外,我的checkSym函數更像是一個碰撞檢測器,用於檢查此處創建的影片剪輯與另一個由用戶控制其運動的影片剪輯之間的碰撞。這就是爲什麼我需要第二個計時器 - 或者用戶在位置更新後仍然可以擊中其他剪輯。 (我知道我原來的帖子沒有指定這個)。我想就如何以更好的方式實施它提出一些建議。 – toofast1227 2011-03-24 17:40:06