-
Passing var type name as parameter?
Hi,
I would like to know if is possible to pass a var type as parameter to a function. I have several sounds in library, and I would like to do something like that:
Code:
function PlayMe(soundname:String)
{
var snd:this[soundname] = new this[soundname];
snd.play();
}
Thanks!
-
Yes, you need to pass the Class instead of a String. If for some reason you want to get a Class by name, use getDefinitionByName.
Code:
function playMe(soundclass:Class):void{
var snd:Sound = new soundclass(); //assumes that soundclass extends Sound.
snd.play();
}
-
half as fun, double the price
With this, note that types (anything after the ":" in a variable declaration) cannot be dynamic. They need to be known types at compile time. Any value passed into a function as an argument or other variables have dynamic values that cannot be used as types.
If you have no idea what the soundclass class really is (is it a Sound? Or something else?) then you would have to type your variable as a generic Object.
-
Thanks a lot 5Tons, is exactly what I was looking for!
-
Thanks Senocular,
Good tip. I always fall in this pitfall, and am always trying to make a dynamics declaration after :!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|