A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Array problem (easy for experts)

  1. #1
    Junior Member
    Join Date
    May 2003
    Posts
    11

    Array problem (easy for experts)

    I have created a huge array called 'colors' which contains a long list of hex colors, such as "0x0066FF","0xFFCC00" with a total of 2296 entries.

    I would like to create a script which will automatically draw 2296 tiny 5 by 5 squares, each colored with the corresponding colors in the array.

    That's pretty much it, I just need to end up with a long line of tiny colored squares.

    -------

    Here's my array, shortened to save space.

    colors = new Array ("0x555273","0xE0644F","0x942B25","0xA7421C","0x62 2BCD","0xB863AF","0x536A92","0x587D5C","0x206ECC", "0xFF2B3A","0x006454","0xA66DE1","0xAC7F81","0xC46 E6A","0x5B652C");
    trace (colors);

    Thanks

  2. #2
    Senior Member
    Join Date
    Oct 2004
    Location
    Rio de Janeiro, Brazil
    Posts
    344
    Shouldnt be easier to loop and just randomize the colours instead to use an array?

  3. #3
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,046
    first off, your array isnt goign to work like that...you are making the colors literal strings... they are hexidecimal numbers, so no quotes:

    var colors=[0xFFFFFF,0x372819,0x000000];

    now for making the 5x5 squares... im not a big fan for drawing everything dynamically like some people here are, i just htink its easier to create a template 5x5 square and put it off the stage somewhere. so do that and name the square movieclip square_template. now check out this code:
    Code:
    var colors = [0xFF0000, 0xFFFFFF, 0x0000FF, 0xFF0000, 0xFFFFFF, 0x0000FF];
    var howmany = colors.length;
    var startx = 0;
    var starty = 0;
    var spacing = 0;
    for (var i = 0; i<howmany; i++) {
    	var newname = "box"+i;
    	var extras = {_x:(5*i)+startx+(spacing*i), _y:starty};
    	var depth = _root.getNextHighestDepth()+1;
    	var m = _root.square_template.duplicateMovieClip(newname, depth, extras);
    	var setColor = new Color(m);
    	setColor.setRGB(colors[i]);
    }
    my code creates 6 boxes in a row starting at 0,0 alternating between red, white, and blue...just swap my array with your array, and itll make the 2000 some boxes you wanted

  4. #4
    Junior Member
    Join Date
    May 2003
    Posts
    11

    Thanks

    That was a big help, it works perfectly!

    Of course, I made an obvious mistake. The line is now too big to fit on the screen, which defeats the purpose of what I'm trying to do.

    Is there a way to make it wrap around to the line below it after, say, 28 squares?

  5. #5
    Registered Deviant
    Join Date
    Sep 2004
    Location
    Cornelius, OR, USA
    Posts
    280
    Delete the line that says
    Code:
    var extras = {_x:(5*i)+startx+(spacing*i), _y:starty};
    and replace it with
    Code:
    	var xmul:Number = i % 28;
    	var ymul:Number = Math.floor( i / 28 );
    	var x:Number = ( ( 5 + spacing ) * xmul ) + startx;
    	var y:Number = ( ( 5 + spacing ) * ymul ) + starty;
    	var extras:Object = { _x:x,_y:y };
    I broke out the math a little to show you what's going on; of course, you can consolidate it all back on to one line again if you want.

    xmul takes the modulus (or remainder) of i / 28... so you will always get a number between 0 and 27.
    ymul takes the division of i / 28 and rounds it down, so you get a number between 0 and 71 which increments every 28 times through the loop.

    If you want it to stretch out to the end of the last row, increase your loop to 2015.

    [EDIT]Oh, and remove the strict typing if you want to match the rest of the code... force of habit for me[/EDIT]
    Last edited by wombatLove; 12-08-2005 at 06:13 AM. Reason: hehe
    If you can read this, you're in the right place.

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