-
Actionscript Developer
attachSound gone, so how can I load random sound?
Hi,
I see that attachSound is gone and you have to instantiate a sound object in replacement.
In other words I can no longer do this:
Code:
var S:Sound = new Sound(this);
S.attachSound("SoundLinkedInLibrary");
S.start(0,0);
I have to do:
Code:
var S:Sound = new SoundLinkedInLibrary();
S.play(0,0);
;
However, this is a big problem! I would like to play a random sound from my library! How can I do this without having if statements?
I used to be able to do this:
Code:
var S:Sound = new Sound(this);
S.attachSound("MusicTrackNumber"+random(5));
S.start(0,0);
But I don't see a way of doing this in AS 3? It seems like I have to do:
Code:
var rand:Number = Math.floor(Math.randon()*5);
var S:Sound;
if (rand == 0)
S = new MusicTrackNumber0();
else if (rand == 1)
S = new MusicTrackNumber1();
else if...
//etc
There MUST be a way. Does anyone have any idea or methods they'd like to share?
Thanks!
-Danny
K2xL - My games, tutorials, message boards, and experiments.
Blog
-
Senior Member
For 4 sounds:
var u:String = "track"+(Math.round(3*Math.random()))+".mp3";
var a:URLRequest = new URLRequest(u);
var b:Sound = new Sound(a);
b.play ();
- The right of the People to create Flash movies shall not be infringed. -
-
Actionscript Developer
Well I figured the answer (getDefinitionByName). It wasn't what you posted. What you posted seems like for external mp3s.
Thanks anyway!
-Danny
K2xL - My games, tutorials, message boards, and experiments.
Blog
-
Senior Member
Yeah, I noticed. Just for completion of this thread.
var B:Class = getDefinitionByName("track"+(Math.round(3*Math.ran dom()))) as Class;
var a:Object = new B();
a.play ();
- The right of the People to create Flash movies shall not be infringed. -
-
Hello
I am using the examples above and I would like to know how can I stop a sound
for example:
I have two buttons. Each one plays a differnt sound
However, when I click on "button 1", the sound from "button 2" keep playing. And if I click on "button 1" again, I will get the same sound playing over.
Since I have a sound.play() but dont have a sound.stop(), I really dont know how stop the sound
Thanx
-
Senior Member
To stop sounds you need to use SoundChannel. here is an example:
PHP Code:
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
var channel1:SoundChannel;
var channel2:SoundChannel;
button1.addEventListener (MouseEvent.CLICK, startAudio1);
function startAudio1 (e:MouseEvent)
{
var mySound1:Sound = new Sound();
mySound1.load (new URLRequest("track1.mp3"));
channel1 = mySound1.play (0, 1);
try
{
channel2.stop ();
}
catch (e:Error)
{
trace ("Error");
}
}
//
//
//
button2.addEventListener (MouseEvent.CLICK, startAudio2);
function startAudio2 (e:MouseEvent)
{
var mySound2:Sound = new Sound();
mySound2.load (new URLRequest("track2.mp3"));
channel2 = mySound2.play (0, 1);
try
{
channel1.stop ();
}
catch (e:Error)
{
trace ("Error");
}
}
- The right of the People to create Flash movies shall not be infringed. -
-
thanx a lot!
worked fine here =))
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
|