-
choosing random word
What coding would I write so when a user presses a button, it selects a word from the words within the text? Thanks!
Last edited by sammy123123; 05-03-2011 at 01:08 AM.
-
Seee the below code
Hi sammy123123,
you can do this by using following code
var words:Array = new Array("enjoy","love","like");
trace("Hi, I really "+words[Math.floor(Math.random()*3)]+" cats.");
Get Back to me if you have any concern
Thanks,
Kunjan.
-
thanks, Kudjan, I do appreciate it! I am aware of using a random word from an array, and it works great.. my dilemma is that I need it to choose from the words in the exact format that I posted.. that is, the user would have a sentence entered into the input box with the format, then push a button, and it would select one of the words. I dont know if there is a way, like a split.join. type of script. Thanks again!
Last edited by sammy123123; 05-03-2011 at 01:08 AM.
-
Well, assuming the source text is called 'foo', you could use something like this:
actionscript Code:
var p = foo.split("{"); for (var i = 1; i < p.length; i++) { var q = p[i].split("}"); var r = q[0].split("|"); p[i] = r[Math.floor(Math.random()*r.length)] + q[1]; } var bar = "".join(p);
-
Last edited by sammy123123; 01-29-2011 at 04:41 PM.
-
Thanks karatorian, it looks like it will work. It is showing an error, though:
Actionscript Code:
**Error** Scene=Scene 1, layer=Layer 2, frame=2:Line 8: There is no method with the name 'join'. var bar = "".join(p)
-
Senior Member
here is another approach..
taking you string.. and making two functions/steps for it..
1.) parse out the 'random word' section.. (and grab one of them randomly)
2.) rebuild the string, with the newly (randomly) chosen word..
actionscript Code:
var myString:String = "Hi, I really {enjoy|love|like} cats.";
function getRandom(targetString) { var subString = targetString.slice(targetString.indexOf("{") + 1, targetString.indexOf("}")); var splitArray:Array = subString.split("|"); var randomWord:String = splitArray[random(splitArray.length)]; return (randomWord); }
function rebuildString(targetString) { var newString:String = targetString; var splitArray2:Array = newString.split(" ");
for (i = 0; i < splitArray2.length; i++) { if (splitArray2[i].charAt(0) == "{") { var targetIndex = i; trace(targetIndex); break; } } splitArray2[targetIndex] = (getRandom(targetString)); //trace("FINAL STRING (Array): "+splitArray2.join(" ")); return (splitArray2.join(" ")); } trace(rebuildString(myString));
-
Hey, Whispers, thanks for this! Looks like a great approach. How would this work with an input text? The var myString:String can't be predefined, as the text is going to be input from the user. Would something like this work (first line):
var myString:String = myText
Where myText is the variable name of the input textbox?
-
Senior Member
var myString:String = someTextField.text;
we dont use variable names anymore really..use the instance names of your objects
but if you are using a textField.. then its even easier..
actionscript Code:
someField_txt.text = "Hi, I really {enjoy|love|like} cats.";
function getRandom(targetString) { var subString = targetString.slice(targetString.indexOf("{") + 1, targetString.indexOf("}")); var splitArray:Array = subString.split("|"); var randomWord:String = splitArray[random(splitArray.length)]; someField_txt.replaceText(targetString.indexOf("{") , targetString.indexOf("}")+1, randomWord); trace(randomWord); return (randomWord); }
getRandom(someField_txt.text);
-
Hey, whispers, thanks alot for this!! It works really well!! Is there an easy way to roll through other bracket samples in the text?
It would choose a random word in the second sample as well? It will work in this way right now if I push the button repeatedly, (I put a button to goto frame 2, the script on frame 2, and gotoandstop to frame 1 again), but is there a way it will loop until all the 'brackets' are used?
Thanks again, awesome work, whiskers!
Last edited by sammy123123; 05-03-2011 at 01:09 AM.
-
Senior Member
maybe add a little check to see if there are any more of those characters in the field? and if so.. run the function again?
actionscript Code:
someField_txt.text = "Hi, I really {enjoy|love|like} cats, but sort of {hate|dislike|scared of} dogs!";
function getRandom(targetTextField) { var charCheck = targetTextField.text.indexOf("{"); trace(charCheck); if(charCheck != -1){ trace("FOUND A RANDOM GROUP"); var subString = targetTextField.text.slice(targetTextField.text.indexOf("{") + 1, targetTextField.text.indexOf("}")); var splitArray:Array = subString.split("|"); var randomWord:String = splitArray[random(splitArray.length)]; targetTextField.replaceText(targetTextField.text.indexOf("{") , targetTextField.text.indexOf("}")+1, randomWord); //trace(randomWord); //return (randomWord); getRandom(targetTextField); }else{ trace("NO MORE RANDOM GROUPS TO PARSE"); } }
getRandom(someField_txt);
-
Thanks for a solution, whiskers!
Last edited by sammy123123; 05-03-2011 at 01:09 AM.
-
Senior Member
you could always change the brackets for the larger 'option'...
and you can use lastIndexOf() I believe as well..
-
I will have to look at it and think of something, I'm afraid lastIndexOf() will give me problems if there are more than 1 block of "selections within selections". Btw, I just got it.. "but sort of {hate|dislike|scared of} dogs!"... haha!!! :P
-
Is there a way I can code it where, if it finds within the "{ }" another set of "{ }", then I could maybe split.("{").join("~") and code it in sequence that way?
-
Senior Member
Im sure it could be done any way you want..
its just taking the tie to do it..
if I get some spare time... and you dont have it figured out I can play more with it..
-
I dont know, it's getting confusing wanting it to behave differently with the same separation indicators. If you have time and can think of a way, (or can somehow translate that php code), that would be great, and much appreciated!
Last edited by sammy123123; 05-03-2011 at 01:10 AM.
-
Senior Member
does the syntax in the text have to be like it is?
meaning do you need to have a pipe ( | ) character breaking up the two choices? I know you have them for the random array options split/delimiter...
but can it be something like:
{{enjoy|love|like} cats~{hate|dislike|scared of} dogs}.
or
{{enjoy|love|like} cats^{hate|dislike|scared of} dogs}.
something unique to identify from other delimiting characters.
or will there ALWAYS only be 3 choice in any sub-option?
-
The pipe character has to break up the two (or more) choices. That's the thing, I wish it were unique characters like you illustrated, that would make life a lot easier.. unfortunately, this is the way it has to be done. I was hoping to somehow convert those pipes and brackets into unique characters with split.join or something, somehow trying to figure a way to single them out, then do the random word/phrase, and then join the sentences back together again.
Last edited by sammy123123; 05-03-2011 at 01:10 AM.
-
Senior Member
oh..this is WAY more complicated then origianlly stated.. even more difficult than the last one you posted about..
its basically an endless amount of nested brackets.. with no rhyme or reason to it..
I was about to post an example of doing the last one you posted on.. (using a carrot character to break up the two MAIN choices)..
good luck!
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
|