A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Performing a function on two textboxes

  1. #1
    Member
    Join Date
    Jul 2008
    Posts
    94

    Performing a function on two textboxes

    Hi! I had an onEnterFrame script that would make a cypher of some letters for you. I thought it would be great if I could wrap the code in a function so I could apply it to multiple textboxes at once. Since doing it (wrong!) it only actually does what its supposed to on one of the textboxes. What am I doing wrong?

    To make this work you'll need a two textboxes, one with an instance name of text_one and one called text_two

    Actionscript Code:
    // The String that will reveal itself
    cypher_characters("text_one", 10, 5, 2, text_one);
    cypher_characters("text_two", 10, 5, 2, text_two);


    function cypher_characters(characters, time_until_start, time_between, frequency, textbox)
    {
        var mc = _root.createEmptyMovieClip(textbox + "clip",1);
        // chop up that string into an array
        var characters_array = characters.split("");
        // measure the length
        var char_len = characters_array.length;
       
        // create an array of timings based on the variables given earlier
        var timings = [];
        for (var i = 0; i < char_len; i++)
        {
            timings.push(time_until_start + (time_between * i));
        }
       
        // The array that will hold all the letters and timings
        var all = [];
       
        for (var i = 0; i < char_len; i++)
        {
            // pair off each character with the a timing from the arrays we've made :)
            all[i] = [characters_array[i], timings[i]];
        }
       
        var count = 0;
       
        mc.onEnterFrame = function(){
            count ++
            if(count > frequency)
            {
                count = 0;
                number_finished = 0;
                // for all our letters
                len = all.length;
                for (var i = 0; i < len; i++)
                {
                    // if they have finished randoming (with 5 frames of 'padding')
                    if(all[i][1] < -5)
                    {
                        // add to total finished
                        number_finished += 1;
                    };
                }
                // if the number of finished animations = the number of letters
                if(number_finished == len)
                {
                    trace("(finished)");
                    // stop doing this
                    delete mc.onEnterFrame;
                }
                else
                {
                    // otherwise shred the cycle function
                    cycle(textbox);
                }
            }
        }
       
        function cycle(textbox){
            // clear the output
            output_string = "";
            // for all the items in the array
            for (var i = 0; i < len; i++) {
                // take away one from the number of frames to random
                all[i][1] --;
               
                // if the number of frames to random is still greater than 0
                if(all[i][1] > 0){
                    // append a random number
                    output_string += Math.ceil(Math.random()*9);
                }
                else{
                    // must have run out of frames, append the letter specified
                    output_string += all[i][0];
                }
            }
            // fill the textbox with the output
            textbox.text = output_string;
        }
    }

    Thanks for any help!

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    You are applying the same depth to all the new movieclips:

    Code:
    var mc = _root.createEmptyMovieClip(textbox + "clip",1);
    Once you add the next movieclip to that depth, it will override the previous one. Simply add that as a parameter for the function, or use getNextHighestDepth to get the next highest depth available in that level:

    Actionscript Code:
    var mc = _root.createEmptyMovieClip(textbox + "clip", _root.getNextHighestDepth());

    Hope this helps

    EDIT: post count, 911, better not mess with me ;P
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Member
    Join Date
    Jul 2008
    Posts
    94
    :O

    So simple when you know how!! Thank you so much

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