A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: help in array

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    1

    Post help in array

    Hi,, I want to work check before displaying any information on what it is to not repeated with supply

    By looping..

    My code:

    txt_1.text = myArray[Math.floor(Math.random() * myArray.length)];
    txt_2.text = myArray[Math.floor(Math.random() * myArray.length)];

    I don't want to be in the field for similar txt1 with txt2 and txt3 for example..

  2. #2
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Hello there, I do not know if i understood well your question, but this is what I understood: You want to input a random value from the myArray array into these textfields, but without repeating the same indexes, so each field can have a random data from the array but without repeating the same data between fields? If so, try this:

    First add this function to randomize things
    PHP Code:
    var randomNumbers:Array = [];

    function 
    getRandomNumbers():Array
    {
        var list:Array = [];
        var 
    randomList:Array = [];
        for(var 
    i:int 0myArray.lengthi++)
        {
            list.
    push(i);
        }
        
        while(list.
    length 0)
        {
            
    randomList.push(list.splice(Math.round(Math.random() * list.length 0), 1)[0]);
        }
        return 
    randomList;
    }; 
    Then, whenever you want to generate non-repeating random numbers, call that function, it will return an array with the random numbers, so we add the results of running that function into another array (randomNumbers) and after that, we can simply use the indexes of that new array to fill the textfields:

    PHP Code:
    randomNumbers getRandomNumbers();

    txt_1.text myArray[randomNumbers[0]];
    txt_2.text myArray[randomNumbers[1]];
    txt_3.text myArray[randomNumbers[2]];
    //and so on 
    You need to know that in life there are many ways of achieving things, so maybe my approach is not the best, but is the most appropiate for your sittuation, and I am not here to compete about which is the best way of coding hehe.

    If you need more explanation about the script, let me know.

    Be well
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

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

    I must have not seen this post before today.

    Seeing as there are numerous possibilities, you could also try something like this
    PHP Code:
    var minimumNumber:Number 1;
    var 
    maximumNumber:Number 300;

    var 
    textFieldAmount:Number 16;
    var 
    textField:TextField;

    var 
    numberArray:Array = new Array();
    var 
    randomArray:Array = new Array();

    var 
    pickedNumber:Number;
    var 
    randomSelect:Number;

    var 
    i:Number;

    makeNumbers();

    function 
    makeNumbers():void
    {
        for (
    minimumNumber<= maximumNumberi++)
        {
            
    numberArray.push(i);
        }
        
    generateNumbers();
        
    //trace(numberArray);
    }

    function 
    generateNumbers():void
    {
        for (
    0textFieldAmounti++)
        {
            
    randomSelect Math.floor(Math.random() * (numberArray.length 1));
            
    pickedNumber numberArray[randomSelect];

            
    randomArray.push(pickedNumber);
            
    numberArray.splice(randomSelect,1);
        }
        
    // *** Traces;
        
    trace("Selected Random Numbers: " randomArray);
        
    trace("Sorted Random Numbers: " randomArray.sort(Array.NUMERIC));
    }

    var 
    textFormat:TextFormat = new TextFormat("verdana",12);

    for (
    0textFieldAmounti++)
    {
        
    textField = new TextField();
        
    textField.defaultTextFormat textFormat;
        
    textField.text randomArray[i];
        
    textField.width 100;
        
    textField.border true;
        
    textField.height 20;
        
    textField.22 i;
        
    addChild(textField);

    just paste into a new as3 document and test
    Last edited by fruitbeard; 11-12-2014 at 04:41 AM.

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

    I see you posted over at as.org today, I still don't know what you want either really.
    http://www.actionscript.org/forums/s...92#post1156092

    Hi,, I want to work check before displaying any information on what it is to not repeated with supply

    My code:
    var sp:Array = new Array( 'Ball' , 'Seq' , 'Circle' , 'Rec');
    btn.addEventListener(MouseEvent.CLICK, Press);
    function Press (event:MouseEvent):void
    {
    txt_1.text = sp[Math.floor(Math.random() * sp.length)];
    txt_2.text = sp[Math.floor(Math.random() * sp.length)];
    txt_3.text = sp[Math.floor(Math.random() * sp.length)];
    txt_4.text = sp[Math.floor(Math.random() * sp.length)];
    txt_5.text = sp[Math.floor(Math.random() * sp.length)];
    txt_6.text = sp[Math.floor(Math.random() * sp.length)];
    }

    I don't want to be in the field for similar txt1 with txt2 and txt3 for example..
    going by that question perhaps
    PHP Code:
    var sp:Array = new Array('Ball','Seq','Circle','Rec');
    btn.addEventListener(MouseEvent.CLICKPress);
    function 
    Press(event:MouseEvent):void
    {
        
    txt_1.text sp[0];
        
    txt_2.text sp[1];
        
    txt_3.text sp[2];
        
    txt_4.text sp[3];
        
    txt_5.text sp[Math.floor(Math.random() * sp.length)];
        
    txt_6.text sp[Math.floor(Math.random() * sp.length)];

    ??? your array is shorter than the textfields amount so you will have a repeat somewhere

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