A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [RESOLVED] confused with loops and arrays

  1. #1
    Senior Member
    Join Date
    May 2000
    Posts
    814

    resolved [RESOLVED] confused with loops and arrays

    My objective is to create a horizontal menu spaced evenly, using dynamic random widths fed from xml.

    I'm playing with arrays in the dynamic loop, but i really have no clue to what i'm doing. Cna someone take a look at my loop code, i'm getting all my individual widths but i don't know what to do with them. In the cas e below i get item 1 and 2 in position but the rest are not moving!!!

    PHP Code:
    function createMyMenu():void {


        
    //This will be used to represent a menu item
        
    var menuItem:MenuItem;
        
        
    //Counter
        
    var i:uint 0;
        
        


            
    //Loop through the links found in the XML file
            
    for each (var link:XML in xml.link) {

            
    menuItem = new MenuItem();
            
    menuItem.id i;

            
    //If the text is longer than the textfield, autosize so that the text is 
            //treated as left-justified text
            
    menuItem.menuLabel.autoSize TextFieldAutoSize.LEFT;

            
    //Insert the menu text (link.@pgTitle reads the link's "pgTitle" attribute)
            
    menuItem.menuLabel.text link.@pgTitle;

    var 
    myArray:Array = [];
    var 
    w:Number;
    menuItem.menuLabel.width;


            
    myArray[i] = [w];
            
    menuItem.0;
            
    //menuItem.x = s +100;
            
            
            
    trace (myArray[i]);


            if (
    i==0){

          
    menuItem.0;
          
    //trace (menuItem.x);
          
    }
          
          
          
            if (
    i==1){

          
    menuItem.myArray[1] + (20 i);
                
    //trace (w);
              //trace (menuItem.x);

          
          
    }
          
          
          
            if (
    i==2){

          
    menuItem.myArray[1] + myArray[2] + (20 i);
                
    //trace (w);
              //trace (menuItem.x);

          
    }
          
            if (
    i==3){

          
    menuItem.myArray[1] + myArray[2] + myArray[3]+ (20 i);
                
    //trace (w);
              //trace (menuItem.x);

          
    }


            
    //Make the button look like a button (hand cursor)
            
    menuItem.buttonMode true;
            
    menuItem.mouseChildren false;
            
            
    //Add event handlers (used for animating the buttons)
            
    menuItem.addEventListener (MouseEvent.MOUSE_OVERmouseOverHandler);
            
    menuItem.addEventListener (MouseEvent.MOUSE_OUTmouseOutHandler);
            
    menuItem.addEventListener (MouseEvent.MOUSE_DOWNmousePressHandler);

            
    menuMC.addChild(menuItem);
            

            
    //Increment the menu button counter, so we know how many buttons there are
            
    i++;
            
            
        }




    Last edited by ratboy; 01-31-2012 at 06:33 AM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your indentation is wildly inconsistent, which makes it hard to read.

    But it seems your major problem is that you are resetting myArray each time through the loop, so you are losing the stuff you put into it before.

    You don't really need myArray anyway. You are putting each item's width in it, right? Instead of doing that and having a separate case for each i where you add myArray[0..i] + 20*i, just keep track of the last item's x coordinate and place the current item relative to that.

  3. #3
    Senior Member
    Join Date
    May 2000
    Posts
    814
    Ok thanks i'll have a play around with that.

    I'm not really a coder, hence the erratic indentations!!!

    just for the record what does [0..i] in myArray[0..i] do?

    rat

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Oh, that was just my shortcut notation for everything in the array between 0 and i. It is NOT valid AS3 code. Other languages, like python, do have that sort of range notation, but I was only using it because I was too lazy to write out "everything in the array between 0 and i".

  5. #5
    Senior Member
    Join Date
    May 2000
    Posts
    814
    oh i see. LOL

    what is the type of code needed to get the previous i position? i have the widths i'm just struggling to figure out the dynamic nature of what i'm trying to do!

    just keep track of the last item's x coordinate and place the current item relative to that.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, you're basically placing each item at the previous item's position plus its width plus some margin (20, in your case). Is that more or less correct? If so, then you pick a starting coordinate (0, here), then do something like this.
    Code:
    function placeABunchOfDisplayObjectsInARow(objs:Array):void{
      var startx:Number = 0;
      var margin:Number = 20;
      for (var i:int = 0; i < objs.length; i++){
        var aThing:DisplayObject = objs[i];
        aThing.x = startx;
        startx = startx + aThing.width + margin; //next thing goes at the new startx value
      }
    }
    That code does not add the items to a parent, or set the y values. Both of those could be done within that function or outside of it.

  7. #7
    Senior Member
    Join Date
    May 2000
    Posts
    814
    cool, i'll let you know how i get on.

    thanks for that.

    Rat

  8. #8
    Senior Member
    Join Date
    May 2000
    Posts
    814
    two lines of code and it's done. I guess i'll get these tricks of the trad over time. Thank you very much.

    Actionscript Code:
    menuItem.x = startx;
            startx = startx + w + margin; //next thing goes at the new startx value

    PHP Code:
    function createMyMenu():void {


        
    //This will be used to represent a menu item
        
    var menuItem:MenuItem;
        
        
    //Counter
        
    var i:uint 0;
        var 
    w:int;
        var 
    startx:Number 0;
        var 
    margin:Number 20;
      

            
    //Loop through the links found in the XML file
            
    for each (var link:XML in xml.link) {

            
    menuItem = new MenuItem();
            
    menuItem.id i;

            
    //If the text is longer than the textfield, autosize so that the text is 
            //treated as left-justified text
            
    menuItem.menuLabel.autoSize TextFieldAutoSize.LEFT;

            
    //Insert the menu text (link.@pgTitle reads the link's "pgTitle" attribute)
            
    menuItem.menuLabel.text link.@pgTitle;



            
    menuItem.menuLabel.width;


                
    menuItem.startx;
              
    startx startx margin//next thing goes at the new startx value

            
    menuItem.0;
            

            
    //Make the button look like a button (hand cursor)
            
    menuItem.buttonMode true;
            
    menuItem.mouseChildren false;
            
            
    //Add event handlers (used for animating the buttons)
            
    menuItem.addEventListener (MouseEvent.MOUSE_OVERmouseOverHandler);
            
    menuItem.addEventListener (MouseEvent.MOUSE_OUTmouseOutHandler);
            
    menuItem.addEventListener (MouseEvent.MOUSE_DOWNmousePressHandler);

            
    menuMC.addChild(menuItem);
            

            
    //Increment the menu button counter, so we know how many buttons there are
            
    i++;
            
            
        }


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