I'm new in AS3. I have 15 sounds in my fla; I imported them to the library and exported all sounds with the name s1, s2, s3, ... s15
I created a function that recieve the number and then play the sound.
In AS2 I could 开发者_如何学编程use eval
like eval("s" + n)
, but in AS3 I can't!
My AS3 code is:
function PlaySound(Num:Number=NaN) {
var mySound:Sound = new ["s"+Num]();
mySound.play();
}
Obviously I'm getting an error!
Someone could help me?
Best, Flávio
The equivalent AS3 code would be:
var soundClass:Class = getDefinitionByName("s" + Num) as Class;
var mySound:Sound = new soundClass();
Put these sound objects in an array, say arr["s1"]=s1 ,arr["s2"]=s2 and so on,( yes in AS3 an array is actually a hashmap.)
Once that is done, its a piewalk, whenever you receive the number (Num) do this
function PlaySound(Num:Number=NaN) {
var mySound:Sound = arr["s"+Num];
mySound.play();
}
精彩评论