Flash Advisor logo
:: Desktop Shortcut
:: Flash Help
Advice from Experts

+ Reply to Thread
Results 1 to 2 of 2
  1. #1

    Default Random Array Elements In Dynamic Text Box

    Hi,

    I have an array (named arrayFull), and I want my dynamic text box (named messageText) to display one random element from the array, changing once per second.
    The code I have makes it display all elements of the array at once in random order, changing once per second.

    How do I make it display just one element from the array at any given time?

    This is the code:


    var arrayFull:Array = new Array(1,2,3,4,5);

    import flash.utils.Timer;
    import flash.events.TimerEvent;

    var myTimer:Timer = new Timer(1000, (arrayFull.length));
    myTimer.addEventListener(TimerEvent.TIMER, runOnce);
    myTimer.start();

    function runOnce(e:TimerEvent):void {
    messageText.htmlText="";
    updateText ();
    }
    var maximumMessages:Number = 1;
    var currentMessages:Number = 0;
    function updateText(){
    for (var i = currentMessages; i<maximumMessages; i++) {
    messageText.htmlText += arrayFull.sort(function () {
    return Math.random() ? true : false;
    });
    for (i = 0; i < 4; i++) {
    this[i].text = arrayFull[i];
    }
    }
    }


    Thank you for your help.

  2. # ADS
    Join Date
    Always
    Posts
    Many
     
  3. #2
    Join Date
    Nov 2010
    Posts
    24

    Post Random element in textfield.....

    Hi,
    Hope this helps you.
    package {
    import flash.display.Sprite;
    import flash.events.TimerEvent;
    import flash.text.TextField;
    import flash.utils.Timer;

    /**
    * ...
    * @author shane.achary@gmail.com
    */
    public class RandomElement extends Sprite {
    private var _details:Array = ["sharat", "achary", "shane", "actionscript", "flash"];
    private var _tf:TextField;
    private var _randomPicker:Timer;
    private var _last:uint = 0;
    private var _current:uint = 0;

    public function RandomElement() {
    _tf = new TextField();
    _tf.border = true;
    _tf.background = true;
    _tf.width = 200;
    _tf.height = 20;

    addChild(_tf);

    // would be fired every 1 sec
    _randomPicker = new Timer(1000);
    _randomPicker.addEventListener(TimerEvent.TIMER, updateTextField_EH);
    _randomPicker.start();
    }

    private function updateTextField_EH(e:TimerEvent):void {

    // will return an integer from 0 to (_details.length - 1), thus here it would be 0 - 4.
    _current = Math.floor(Math.random() * _details.length);

    // check whether the last index is not selected again, if yes increment it.
    (_current == _last)?_current++:true;

    // check the incremented index does exceeds the array length, if yes set to 0.
    (_current >= _details.length)?_current = 0: true;

    _tf.text = _details[_current];

    //update last with the current index, as current index would be the las index for the next iteration.
    _last = _current
    }
    }
    }

    Regards,
    Shane Achary

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Sponsors
Create Speaking Characters for your website and Flash movies. 15 Day Free Trial