AS2ではsetIntervalを使用してスコープが変更されます。念のため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
}
}
また、MovieClipは動的クラスであり、実行時にプロパティを追加できるため、そのままではあまり面白くないことに気付いたので、velocity(_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とscopeの問題に関しては、スコープが変更されるためsetIntervalの問題ですが、コールバックへの引数を使用して問題を乗り越えることができます。