Re: [問題] 迴圈的變數去傳參數的問題

看板Flash作者 (不會用)時間18年前 (2007/09/16 20:43), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/4 (看更多)
※ 引述《etrexetrex (ETREX)》之銘言: : ※ 引述《cantuse (不會用)》之銘言: : : 請問各位高手 : : 底下這段程式 : : 我是爬過文 用了拆字串的方法 : : 去讓i這個變數 : : 變成當"store"+i+"_mc" 被rollover : : 所呼叫方法rollOverIt(i)傳的一個參數 : : for (i=1; i<=200; i++) { : : eval("_root.store_mc.store"+i+"_mc").onRollOver = function() { : 可改為 var MC = _root["store_mc.store"+i+"_mc"]; : MC.i=i; : MC.onRollOver = function(){ : rollOverIt(this.i); : } ^^^^^^ : : rollOverIt(int(this._name.substr(5, 1))); : : }; : : } : : 可以請教的是 : : 1.我沒設想到萬一變數i超過二位數 那傳遞的永遠都是1-9了 : : 請問有更好的方法嗎? : 有 : : 2.還有就是想請問 為什麼直接用rollOverIt(i) : : 得到的永遠都是最後一個數字ex200? : 當onRollOver執行時,for迴圈勢必先跑完,i永遠是最後一個數字 : 所以讓i變成MC的屬性就沒問題惹 : : 最後感謝大家 : : 謝謝 恩 真的很謝謝您 只是我還以為寫成這樣↓ for (i=1; i<=200; i++) { eval("_root.store_mc.store"+i+"_mc").onRollOver = function() { rollOverIt(i); } } 程式就會就會跑出 _root.store_mc.store1_mc.onRollOver = function() { rollOverIt(1); } _root.store_mc.store2_mc.onRollOver = function() { rollOverIt(2); } _root.store_mc.store3_mc.onRollOver = function() { rollOverIt(3); } . . . . . . . _root.store_mc.store200_mc.onRollOver = function() { rollOverIt(200); } 為什麼跑出來的還是 _root.store_mc.store1_mc.onRollOver = function() { rollOverIt(i); } 呢? 是因為它是參數的關係嗎?? 謝謝^^ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.117.100.53
文章代碼(AID): #16xIJRIx (Flash)
文章代碼(AID): #16xIJRIx (Flash)