AS2 中的范围随着 setInterval 的变化而变化。只是为了确保,trace(this)
在回调内部执行,您会看到它不是 _root,正如预期的那样,但您可以将容器影片剪辑作为额外参数传递给回调函数,以便您可以将库项目添加到其中。
此外,代码无缘无故看起来很复杂:
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),因为 MovieClip 是一个动态类,您可以在运行时向它添加属性。请注意,这不是一个好习惯。目标是在剪辑动画的方式上获得一些深度:
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 的问题,因为范围发生了变化,但你可以使用回调的参数来解决这个问题。