PDA

View Full Version : Play Random Audio


Brandon
12-08-2004, 07:14 AM
Hi, I'm making a small game and was wondering, I have 6 different audio files in my scene. I want to randomly play one when I hit space bar. How would I do this?

farafiro
12-08-2004, 02:35 PM
Brandon
welcome to the forums
//2 methods to chose from
//*************** First
//give the sounds a linkage name in the library, like s0,s1,.....s5
//then in the first frame
var ranSound = new Sound(this)
var theKey = new Object();
theKey.onKeyDown = function() {
if (Key.isDown(Key.SPACE)) {
_level0.ranSound.attachSound("s"+Math.round(Math.random()*5))
}
ranSound.start()
};
Key.addListener(theKey);
//*************** Second
//give the sounds a linkage name in the library as u wish, I will chose like a,b,c,d,e,f
//in the first frame
var sArray = ["a","b","c","d","e","f"]
var ranSound = new Sound(this)
var theKey = new Object();
theKey.onKeyDown = function() {
if (Key.isDown(Key.SPACE)) {
_level0.ranSound.attachSound(_level0.sArray[Math.round(Math.random()*_level0.sArray.length-1)])
}
ranSound.start()
};
Key.addListener(theKey);

Brandon
12-08-2004, 06:00 PM
Hi, your code works great when I run it in a test app, but when I try to integrate it into my app it doesnt work right. Where should I be putting your code?

onClipEvent (load) {
speed = 2;
visible = false
}
onClipEvent (enterFrame) {
if (key.isdown(key.RIGHT)) {
play();
//_rotation = 90;
_x+= speed;
}
if (key.isdown(key.SPACE)) {
gotoandplay(16);
}
if (key.isdown(key.LEFT)) {
play();
_rotation = 270;
_x-= speed;
}
if (key.isdown(key.UP)) {
play();
_rotation = 0;
_y-= speed;
}
if (key.isdown(key.DOWN)) {
play();
_rotation = 180;
_y+= speed;
}
if (key.isdown(key.RIGHT) && key.isdown(key.UP)) {
_rotation = 45;
}
if (key.isdown(key.LEFT) && key.isdown(key.UP)) {
_rotation = 315;
}
if (key.isdown(key.RIGHT) && key.isdown(key.DOWN)) {
_rotation = 135;
}
if (key.isdown(key.LEFT) && key.isdown(key.DOWN)) {
_rotation = 225;
}
}

farafiro
12-09-2004, 07:45 AM
//add these lines
onClipEvent (load) {
//....
var ranSound = new Sound(this)
}
//
onClipEvent (enterFrame) {
//
if (Key.isDown(Key.SPACE)) {
_level0.ranSound.attachSound("s"+Math.round(Math.random()*5))
}
}

Brandon
12-09-2004, 05:05 PM
I tried what you said, and it wont work for me. I believe I typed everything in right:

onClipEvent (load) {
var ranSound = new Sound(this)
speed = 2;
visible = false
}
onClipEvent (enterFrame) {
if (key.isdown(key.RIGHT)) {
play();
//_rotation = 90;
_x+= speed;
}
if (key.isdown(key.SPACE)) {
_level0.ranSound.attachSound("s"+Math.round(Math.random()*5))
gotoandplay(16);
}
if (key.isdown(key.LEFT)) {
play();
_rotation = 270;
_x-= speed;
}
if (key.isdown(key.UP)) {
play();
_rotation = 0;
_y-= speed;
}
if (key.isdown(key.DOWN)) {
play();
_rotation = 180;
_y+= speed;
}
if (key.isdown(key.RIGHT) && key.isdown(key.UP)) {
_rotation = 45;
}
if (key.isdown(key.LEFT) && key.isdown(key.UP)) {
_rotation = 315;
}
if (key.isdown(key.RIGHT) && key.isdown(key.DOWN)) {
_rotation = 135;
}
if (key.isdown(key.LEFT) && key.isdown(key.DOWN)) {
_rotation = 225;
}
}


Even when I delete all my AS from my flash, and try to run any of the ones you gave I get the following errors:
Scene=Scene 1, Layer=walk, Frame=1: Line 4: Statement must appear within on/onClipEvent handler
var sArray = ["a","b","c"]

Scene=Scene 1, Layer=walk, Frame=1: Line 5: Statement must appear within on/onClipEvent handler
var ranSound = new Sound(this)

Scene=Scene 1, Layer=walk, Frame=1: Line 6: Statement must appear within on/onClipEvent handler
var theKey = new Object();

Scene=Scene 1, Layer=walk, Frame=1: Line 7: Statement must appear within on/onClipEvent handler
theKey.onKeyDown = function() {

Scene=Scene 1, Layer=walk, Frame=1: Line 13: Statement must appear within on/onClipEvent handler
Key.addListener(theKey);

farafiro
12-12-2004, 06:38 AM
OOoops
sorry, my mistake
just cahnge the _level0 to this within the enterFrame statement

Brandon
12-13-2004, 09:02 PM
I'm sorry, I tried everything and it didnt work. Though, If I put your 2nd script just in a frame, and not in the movieclip's script area...it WILL work, the drawback being, if I keep hitting spacebar it keeps playing a random sound even though the first one isnt finished, How can I make it so it doesnt play until sound x is done?

farafiro
12-14-2004, 07:19 AM
post your fla

Brandon
12-14-2004, 08:59 PM
Here...as you can see I just replaced the character with an X.