作者Qiqi (泼泼)
看板Flash
标题[问题] 变数的生命周期
时间Mon Apr 6 14:10:46 2009
环境:flash mx pro 2004
var len;
function creat(select) {
len = xmlFood.firstChild.childNodes[select].childNodes.length;
for (i=0; i<len; i++) {
var t=xmlFood.firstChild.childNodes[select].childNodes[i].childNodes;
img=xmlFood.firstChild.childNodes[select].childNodes[i].attributes.img;
attachMovie("suba", "sub"+i, 100+i);
this["sub"+i]._x = 0;
this["sub"+i]._y = i*25;
this["sub"+i].txt.text = t
this["sub"+i].btn.onRelease = function() {
_root.food.
food_img.loadMovie(img);
trace(img);
};
}
}
--
this["sub"+i].btn 是子选的单按钮
onRelease後会在
food_img这个影片片段load从xml取得的图片路径
但在onRelease的function里面我
trace(img),都固定是最後一个
譬如:
现在len取得是6(0~5)
this["sub"+i]也有指定到sub0~sub5
但loadMovie里面的img却是固定为childNodes[5]的值
////////////////////////////////////////////////////////////////////
解:
var len;
function creat(select) {
len = xmlFood.firstChild.childNodes[select].childNodes.length;
for (i=0; i<len; i++) {
var t =xmlFood.firstChild.childNodes[select].childNodes[i].childNodes;
img =xmlFood.firstChild.childNodes[select].childNodes[i].attributes.img;
attachMovie("suba", "sub"+i, 100+i);
this["sub"+i]._x = 0;
this["sub"+i]._y = i*25;
this["sub"+i].txt.text = t;
this["sub"+i].btn.img = img;
this["sub"+i].btn.onRelease = function() {
_root.food.food_img.loadMovie(
this.img);
};
}
}
--
因为onRelease时
按下去的时候才去找i值,这个时候for早就跑完了,i当然维持在最终状态
必须在回圈里面直接让当时的i值跟MC(MovieClip)有关连
最快的方法
就是在MC身上插一个变数
this["mc" + i].i = i;
在onRelease里面直接呼叫 this.i就好
这个i就不会是最终值
看看不太懂,看上面cjcat2266这番话就懂了。
谢谢cjcat2266帮忙解答。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.170.22.136
※ 编辑: Qiqi 来自: 118.170.22.136 (04/06 14:27)
※ 编辑: Qiqi 来自: 118.170.22.136 (04/06 14:27)
1F:推 cjcat2266:请见 3>7>3>1 04/06 14:32
2F:→ cjcat2266:发生此状况的理由跟解决方法都有讲 04/06 14:32
3F:→ cjcat2266:AS2没有Dictionary,要嘛用用dynamic variable 04/06 14:33
4F:→ cjcat2266:要嘛写一个类似Dictionary的功能,一个物件对应一个值 04/06 14:34
5F:→ cjcat2266:可以用 if (A == B) img = 3 这种方式来判断 04/06 14:34
6F:→ Qiqi:请问,如何知道我用的是as2.0还是as3.0呢? 04/06 14:48
7F:→ aquarianboy:新增档案的时候应该就知道了 04/06 15:11
8F:→ aquarianboy:而且有语法版本有差异,执行起来也会知道的 04/06 15:12
9F:→ Angelliya:1.属性列设定高与宽那边看的到 AS版本 04/06 15:12
10F:→ Angelliya:2.action工作列左上角有 04/06 15:12
11F:→ Angelliya:3.发布设定的flash里面也有 04/06 15:12
12F:→ Qiqi:了解 原文跟推文的问题都解决了,谢谢大家 04/06 15:23
※ 编辑: Qiqi 来自: 118.170.22.136 (04/06 15:30)