I have a random sentence generator, but how do I add sound to it?

Subscribe to I have a random sentence generator, but how do I add sound to it? 16 posts

Sign in to reply


 
avatar for tivaelydoc tivaelydoc 147 posts
Flag Post

I would provide the game but due to it’s explicit language, I’ll abstain.

I have a code that randomly generates a random sentence:


on (release) {
result = a[random(a.length)]" "b[random(b.length)]" "c[random(c.length)]" "d[random(d.length)]" "e[random(e.length)]" "f[random(f.length)]" "g[random(g.length)]" "h[random(h.length)]" "i[random(i.length)];
}

Now on every word, I have a sound clip that I want to play, but how would I code it? I don’t know I would I would assign a sound to a word or how to incorporate it into the coding above.

Wouldn’t this work?


if(name == “example”){
mysound = new Sound();
mysound.attachSound(“example”);
mysound.start();}
}

 
avatar for Nyde Nyde 25 posts
Flag Post

I would put each word of a sentence in an array. Then step through each array item using a loop (for loop or while loop, whatever). In each iteration, I would check what that word was using conditional statements, i.e.


for(var i:Number=0; i<sentenceArray.length; i++) {
if(sentenceArray[i]==“example”) {
var mySound:Sound = new Sound();
mysound.attachSound(“example”);
mysound.start();
}
}

Use switch statements if that’s more appropriate, or put all your sounds in an array and use the index to play the correct sound.

 
avatar for tivaelydoc tivaelydoc 147 posts
Flag Post

Wow, 5 different websites and you were the only one to reply. I wish I could give you a medal. Anyway, so I would change this:


a = [“example”, “rock”, “sheep”];
b = [“example”, “rock”, “sheep”];
c = [“example”, “rock”, “sheep”];

to something like this:

var a = new Array(“example”,“rock”,“sheep”);
var b = new Array(“example”,“rock”,“sheep”);
var c = new Array(“example”,“rock”,“sheep”);

I’m confused where I would put the for loop code.

 
avatar for Nyde Nyde 25 posts
Flag Post

wait, a, b, and c are your words or your sentences?

So say result is your sentence. and your sentence is: “I love Kongregate”.

var result = "I love Kongregate";
var sentenceArray = result.split(" "); // this splits your string with the space as a delimiter, so basically every word
for(var i:Number=0; i<sentenceArray.length; i++) {
  if(sentenceArray[i]=="example") {
    var mySound:Sound = new Sound();
    mysound.attachSound("example");
    mysound.start();
  }
}


To make sure you understand the code, here are some questions you should be able to answer:
1) What is the value of i after the for loop terminates in the above example?
2) What is the index number of the last index of the array sentenceArray?
3) what is the value of sentenceArray[ 2 ]?


Good luck and if you’re having more trouble, let us know.

 
avatar for ch00se ch00se 98 posts
Flag Post

The only problem with this solution is that adding words will become cumbersome. Also if you have hundreds of words this will be a pain to code. What would be better is to have each sound clip be named exactly the word name when you set linkage. Then for each word you can call attachSound on that particular word. This way you avoid the use of an if statement or a switch statement.

 
avatar for tivaelydoc tivaelydoc 147 posts
Flag Post

Well, how do you code that chOOse? And Nyde, I think I sort of get it. So for every word I would use that var sentenceArray? And what code would I use for the button?

 
avatar for Nyde Nyde 25 posts
Flag Post

(Also if you have hundreds of words this will be a pain to code. What would be better is to have each sound clip be named exactly the word name when you set linkage. )

I don’t know how many different sound clips he wants to play, he didn’t specify. If there’s a hundred words and each of them have a different sound associated with it, obviously, the “if” statement or switch statement isn’t the wisest solution. I only used that in the EXAMPLE script because he had an if statement in his original post… I’m not going to completely code someone’s game for them. Also your solution doesn’t work if one sound clip is associated with multiple words. i.e. if for the words, “hello”, “hi”, “howdy”, you play a whistle sound.

 
avatar for tivaelydoc tivaelydoc 147 posts
Flag Post

I do have over 100 sound clips. I know how set linkage, but how do I get to play the sound when its randomly selected? I’ll type it all out if I have to, I just need to know what code to use.

 
avatar for tivaelydoc tivaelydoc 147 posts
Flag Post

I think I get it. Would I do this:

var a = new Array("example","rock","sheep");
var b = new Array("example","rock","sheep");
var c = new Array("example","rock","sheep");

setup the array

then for each sound use this:


var result = " “;
var sentenceArray = result.split(” “);
for(var i:Number=0; i<sentenceArray.length; i++) {
if(sentenceArray[i]==”example") {
var mySound:Sound = new Sound();
mysound.attachSound(“example”);
mysound.start();
}
}

So where did I mess up?

 
avatar for Nyde Nyde 25 posts
Flag Post

No offense, but that just doesn’t make sense to me, and like I’ve said, I don’t want to write other people’s games for them. why would you set result equal to " " and then split it with a " " as the delimiter? where in that loop does a, b, and c come in? So that’s how you have it set up in your flash movie? exactly like that?

You messed up when you just copied and pasted code without understanding it.

 
avatar for ch00se ch00se 98 posts
Flag Post

Nyde, I just want to make it clear that I was not trying to devalue your solution, I was merely trying to build upon it. Using an if statement would certainly work.

tivaelydoc, try starting smaller to see if you have everything set up correctly first. Try one word in sentenceArray (don’t even worry about the split until you get it working). If you can get that one sound to play then we can go from there, otherwise we need to look at how you’re setting this up (where this code is located, etc…).

Let us know how it goes!

 
avatar for tivaelydoc tivaelydoc 147 posts
Flag Post

Nyde, I didn’t ask to see how wrong I was, I asked for help to understand this code. There is no tutorials out, so I have no way of understanding what the code means.

chOOse, so use that code and try it? That clearly won’t work. What do I do?

 
avatar for Nyde Nyde 25 posts
Flag Post

Actually, I looked at your game, and it looks like you have a preset number of words in your sentence, so having a for loop with the sentenceArray length as your boundary isn’t needed.

How does the game work? Each word has a different sound? Like the word “Hello” would play a soundclip of someone saying “Hello”? So if my sentence was “Hello, I’m Nyde”, it would say “Hello, I’m Nyde”? Or are several words associated with one sound?

In my example, keep using the random word generator that you had result equal to. You don’t have to put the words in individual arrays because you’re going to split result anyways, and then put each word in result in the sentenceArray.

And then go with ch00se’s suggestion of naming your sound clips with the name of the word (if it’s a singular relationship).

Also, you’d want a delay in between each sound, I would imagine… so you can put that at the end of each iteration of the loop.

Another way I would do this is have 5 different dynamic textboxes, generate a random word for each, take the value of each textbox, and play the corresponding sound.

I apologize for being defensive and snippy, it’s been a rough week… I truly want to help, but I don’t like spoon-feeding because in the long run, it doesn’t help you at all.

 
avatar for tivaelydoc tivaelydoc 147 posts
Flag Post

Well here is a demo zip that has the FLA and the SWF:

http://daddyfile.com/download.php?file=0f264cde11128390087a1b04b8d590af

 
avatar for pel6413 pel6413 72 posts
Flag Post

does this work:

on (release) {
	var sentence:Array = new Array(a[random(a.length)], b[random(b.length)], c[random(c.length)])
	result = ""
	for(var i:Number = 0 ; i<sentence.length ; i++) {
		result += sentence[i]+" "
	}
	_root.playList = sentence
	_root.nowPlaying = 0
	sound = new Sound();
	sound.attachSound(_root.playList[_root.nowPlaying]);
	sound.onSoundComplete = function() {
		_root.nowPlaying++
		trace(_root.nowPlaying +" "+ _root.playList.length)
		if(_root.nowPlaying < _root.playList.length) {
			this.attachSound(_root.playList[_root.nowPlaying]);
			this.start()
		} else {
			this.stop()
                        delete(this)
		}
	}
	sound.start();
}

 
avatar for tivaelydoc tivaelydoc 147 posts
Flag Post

Thank you soooooo much. A month and 30+ websites later, it’s finally done. Here is the example:

http://img295.imageshack.us/my.php?image=demoet7.swf

Sign in to reply