一道FLASH AS2.0代码

下面是雪花飘落的程序(FLASH AS2.0程序)
var i=1;
snow._x=-100;
onEnterFrame=function() {
duplicateMovieClip("snow", "snow" + i, i);
this["snow"+i]._x=random(Stage.width);
this["snow"+i]._y=-3;
this["snow"+i]._alpha=60+random(50);
this["snow"+i]._xscale=random(5)+16;
this["snow"+i]._yscale=random(5)+16;
this["snow"+i].speed=random(3)+2;
this["snow"+i]._rotation=random(360);
this["snow"+i].onEnterFrame=function() {
if(this._y<600) {
this._x+=(Math.sin(this._y/19)*1.3);
this._y+=this.speed;
} else{
//超出舞台,则回收再利用
delete this.onEnterFrame;
}
}
我的问题是:把this["snow"+i].onEnterFrame=function() {
if(this._y<600) {
this._x+=(Math.sin(this._y/19)*1.3);
this._y+=this.speed;
} 这部分代码调整成下面的代码不可以,为什么?
this["snow"+i].onEnterFrame=function() {
if(this["snow"+i]._y<600) {
["snow"+i]._x+=(Math.sin(this["snow"+i]._y/19)*1.3);
this["snow"+i]._y+=this["snow"+i].speed;
}

首先,没有调整的代码中存在一个问题,那就是i没有进行变化,导致问题是雪花元件被复制出来后,又被删除了。所以达不到下雪效果,不知是不是少复制出现的问题的,因为后面还少一个“}”号

调整后的代码
1、 this["snow"+i].onEnterFrame=function() {
2、 if(this["snow"+i]._y<600) {
3、 ["snow"+i]._x+=(Math.sin(this["snow"+i]._y/19)*1.3);
4、 this["snow"+i]._y+=this["snow"+i].speed;
5、 }

错误1:格式上,第3句,缺少this。
错误2:逻辑上错误,不影响程序运行,只是达不到效果。分析如下:
在this["snow"+i].onEnterFrame函数里的this,指的是this["snow"+i]元件本身,即onEnterFrame前面的元件,所以,函数里的this["snow"+i],其实就是 元件里的["snow"+i]属性,而属性明显是不存在的,所以达不到效果。

再者,就是函数里的i,因为i引用的是全局变量,而i在第一个onEnterFrame里是变化的,所以在
this["snow"+i].onEnterFrame里的i也是变化的,故在函数里捕捉到的this["snow"+i],并非指onEnterFrame前面的元件
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答
大家正在搜