2012-12-31 69 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"]); 

虽然我不建议使用这种方法,因为它变得非常困难,当你回到你的代码阅读。否则,我认为这是一个干净的解决方案。