开发者

How to Create Sound Object Dynamically

开发者 https://www.devze.com 2023-02-17 15:57 出处:网络
How to create sound object dynamically in one movieClip. Example 开发者_C百科for(i=1;i<5;i++){var sound + i = new Sound();}

How to create sound object dynamically in one movieClip.

Example

开发者_C百科for(i=1;i<5;i++){var sound + i = new Sound();}


You can try putting all the sounds from your loop inside an Array:

var soundArray:Array = [];
for (var i:uint = 0; i < 5; i++) {
    var sound:Sound = new Sound(); 
    // don't forget to set the path of the file you want to play

    soundArray.push(sound);
}

To play the the sound, all you need to do is take note of the index:

Sound(soundArray[0]).play();

Hope this helps.

irot


you need a MovieClip to store your sounds into then:

var mc:MovieClip = new MovieClip();
for( var i:int = 0; i < 5; i++ )
{
    mc[ 'sound_' + i ] = new Sound(); 
}

to access a sound, you can then call:

mc[ 'sound_0' ].play();

if you're already in the scope of a MovieClip, ignore the mc creation and replace 'mc' with 'this' in the loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号