A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: choosing random word

  1. #1
    Senior Member
    Join Date
    Jul 2010
    Posts
    111

    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.

  2. #2
    Member
    Join Date
    May 2009
    Location
    Ahmedabad, GUJARAT, INDIA
    Posts
    31

    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.

  3. #3
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    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.

  4. #4
    Member
    Join Date
    Jan 2011
    Posts
    32
    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);

  5. #5
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    ..
    Last edited by sammy123123; 01-29-2011 at 04:41 PM.

  6. #6
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    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)

  7. #7
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    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));

  8. #8
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    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?

  9. #9
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    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);

  10. #10
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    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.

  11. #11
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    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);

  12. #12
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    Thanks for a solution, whiskers!
    Last edited by sammy123123; 05-03-2011 at 01:09 AM.

  13. #13
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    you could always change the brackets for the larger 'option'...

    and you can use lastIndexOf() I believe as well..

  14. #14
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    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

  15. #15
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    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?

  16. #16
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    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..

  17. #17
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    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.

  18. #18
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    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?

  19. #19
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    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.

  20. #20
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center