A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: random letter on _mc start

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    7

    random letter on _mc start

    ok, I have an _mc (letterspot_mc) that animates across the screen. it is just a square, I want to be able to place this letterspot_mc on the stage and have a random letter appear inside it each time it starts.

    do I need to make all 36 alphanumeric characters into _mc's? ex:a_mc b_mc etc etc.....

    also A good start on the random() code will be helpful. I'm thinking picking random from an array of strings may be a way to go.

    this is the next step in a simple typing game I am working on. I have a game that scrolls a set of letters I typed across the bottom. the user can press the keyboard to match the letter or a button on a picture of a keyboard. there is no error checking for correct letters. I may add that later. it is an attempt to get young students used to the keyboard.

    thank you for ant thought on this.

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    In a rush so I hope you can make use of this, I can explain what it does another time, if you are unable to follow it.
    PHP Code:
    var Generation 1;// Letter amount
    // *** Create Random Letters
    function RandomLetters(Arg)
    {
        var 
    Chars "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
        
    //var Chars = "abcdefghijklmnopqrstuvwxyz0123456789 ";
        
    var NumChar Chars.length 1;
        var 
    RandomChar "";
        for (var 
    0Args++)
        {
            
    RandomChar += Chars.charAt(Math.floor(Math.random() * NumChar));
        }
        return 
    RandomChar;
    }
    // Set the text
    function SetText()
    {
        
    letterspot_mc.myText.autoSize true;
        
    letterspot_mc.myText.text RandomLetters(Generation);
    }
    // Call function
    SetText(); 

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    var Generation = 1;// Letter amount this is used later to populate the letterspot_mc.myText.text it seems
    // *** Create Random Letters
    function RandomLetters(Arg) creatring a function. not sure about the (arg) part
    {
    var Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "; this is the charcters that I need for random to choose from...I think?
    //var Chars = "abcdefghijklmnopqrstuvwxyz0123456789 ";
    var NumChar = Chars.length - 1; this is telling it that we only want one char. to be used
    var RandomChar = "";
    for (var s = 0; s < Arg; s++) this part makes no sense. making a var s and it is = to s less than the arg. then s++ (that part lost me)
    {
    RandomChar += Chars.charAt(Math.floor(Math.random() * NumChar)); this seems to be the code (method/function) to come up with a random letter(NumChar in this case) which will be used to populate letterspot
    }
    return RandomChar;
    }
    // Set the text
    function SetText()
    {
    letterspot_mc.myText.autoSize = true; ok, cool. text needs to be sized, but will autoSize make it fill the 50x50 _mc? and do I need an _mc called myText inside my letterspot_mc
    letterspot_mc.myText.text = RandomLetters(Generation); it seems like this will populate the _mc myText with the result of above function "RandomLetters"
    }
    // Call function
    SetText(); is this a built in function of AS2?
    }


    questions?
    what is this letterspot_mc.myText.text ? the .text property of my text which is inside letterspot_mc?

    I am sorry that I am not fluent in AS. I rarely use flash but enjoy it. I tried to just copy and past the code in and testing it work with no errors but no letters either, so then I added an _mc called mytext into the letterspot_mc, still the same. I am able to follow your code ok, but can not fully understand it. I tried to break it down using green and bold after the lines of code.

    thank you for your time.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    var Generation = 1;// amount of letters to be generated, set to one at present
    // *** Create Random Letters
    function RandomLetters(Arg)// Arg is the number sent from the RandomLetters(Generation) Generation is the Arg, it could be called anything you wanted as long as all Arg entries are called the same name
    {
    var Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";// yes, the character array to randomise from
    //var Chars = "abcdefghijklmnopqrstuvwxyz0123456789 ";// same as above just lower case, choose whichever you wanted
    var NumChar = Chars.length - 1;// Chars array length
    var RandomChar = "";// start with blank before randomly picking a letter from array
    for (var s = 0; s < Arg; s++)// checks how many letters you want from generation var, only one so s = 1. in arrays the first letter is numbered 0
    {
    RandomChar += Chars.charAt(Math.floor(Math.random() * NumChar));// selects random letter from Chars length
    }
    return RandomChar;// outputs the selected random letter
    }
    // Set the text
    function SetText()
    {
    letterspot_mc.myText.autoSize = true;// autosizes text, probably not needed with your text ??
    letterspot_mc.myText.text = RandomLetters(Generation);// calls random letter function, here generation = 1, 1 becomes Arg for other function
    }
    // Call function
    SetText();// this just calls the SetText function above, it can be called from anywhere, a button, a mousemove etc etc. not a built in function

    letterspot_mc.myText.text, is a made up name for a textfield inside your letterspot_mc movieclip (as you did not say what you had called it), it can be called what ever you want as long as you alter all entries to the same name

    You need a textfield inside your movieclip. not another movieclip

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    you say textfield, all I can find (in components) is text area, or text input. I tried both and used the instance name of myText and renamed the library item to myText. the linkage remains the name of the component though. I also tried just using the text tool and drew out a textbox but left it empty, made sure to name its instance of myText


    Am I correct that your code should be on frame one of letterspot_mc? I did try there, and also on frame one of the scene

    thanks again for your help, your descriptions of the code were great.

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    The code would be placed on the main timeline.

    The textfield is not a component, open upon up your letterspot_mc and create your textfield from the tools panel, give it a name of (in this case) myText.

  7. #7
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Ok , here is a simplified version that will create the textfield for you.

    put your letterspot_mc on the stage, this code on the main timelinie.
    PHP Code:
    function RandomLetters()
    {
        var 
    Chars "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
        
    //var Chars = "abcdefghijklmnopqrstuvwxyz0123456789 ";
        
    var NumChar Chars.length 1;
        var 
    RandomChar Chars.charAt(Math.floor(Math.random() * NumChar));
        return 
    RandomChar;
    }

    letterspot_mc.createTextField("CreatedText",this.getNextHighestDepth(),10,10,0,0);
    letterspot_mc.CreatedText.autoSize "left";
    letterspot_mc.CreatedText.text RandomLetters(); 
    if you can get that to work, great. eventually you might be able to incorporate the rest of the other code if and when you get more letters and words.

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Sweet. that worked sorta, I had to comment out your creation of the textfield, and make it myself. called it CreatedText and it worked, I now have a letter randomly chosen crawling across the screen. when I tried to duplicate it and offset it so that a letter would go then a little after another would go it just made my first switch to another letter. going to play for awhile before I request help. Thank you so much with this. My work is mostly in 3d applications and after effects. If you ever need help there hit me up

Tags for this Thread

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