2012-12-31 66 views
0

我是AS3的新手,需要一些幫助。在運行時構造變量名稱

我有這樣的代碼來播放不同的音頻文件和動態循環他們:

var mIntroLoop = 12495; 
var mBattleLoop = 29000; 
var currentPlaying; 

function playMusic(x):void 
{ 
    musicChannel = x.play(); 
    currentPlaying = x; 
} 

function loopSound(m:Object):Function { 
    return function(e:Event):void { 
     musicChannel = m.play(m.toString() + "Loop"); 
    musicChannel.addEventListener(Event.SOUND_COMPLETE, loopSound(currentPlaying)); 
    } 
} 

playMusic(mIntro); 
musicChannel.addEventListener(Event.SOUND_COMPLETE, loopSound(currentPlaying)); 

正如你可以在代碼的頂部看到我有兩個變量,mIntroLoopmBattleLoop

我需要這一行幫助:

musicChannel = m.play(m.toString() + "Loop"); 

這當然不工作,它的存在只是讓你知道我想要做的事。

在該行中,m.play()需要具有mIntroLoopmBattleLoop的返回值作爲參數,具體取決於我當前正在播放的內容。

+0

我已經使用Object屬性解決了id。 – Fr0z3n

回答

0

我看你自己解決了,但我認爲你應該看看這個。

因爲所有的AS3類擴展Object類,你可以做這樣的事情:

musicChannel = m.play(this[m.toString() + "Loop"]); 

雖然我不建議使用這種方法,因爲它變得非常困難,當你回到你的代碼閱讀。否則,我認爲這是一個乾淨的解決方案。