Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 6 of 6

Thread: Looping and variables

  1. #1

    Looping and variables

    I need to make a loop that can create (and use) incremented variable names.

    Basically I need to dynamically create a certain number of sprites and datagrids then I need to add these dynamically created datagrids and sprites to the stage.

    So the static code (which works, but I think is NOT the right way to go) would look something like this (I'm leaving out a bunch of stuff but this should give you the right idea):

    Code:
    var DataGrid0:DataGrid = new DataGrid();
    var DataGrid1:DataGrid = new DataGrid();
    var DataGrid2:DataGrid = new DataGrid();
    var DataGrid3:DataGrid = new DataGrid();
    
    addChild(DataGrid0);
    addChild(DataGrid1);
    addChild(DataGrid2);
    addChild(DataGrid3);
    
    DataGrid0.dataProvider = dp;
    DataGrid1.dataProvider = dp;
    DataGrid2.dataProvider = dp;
    DataGrid3.dataProvider = dp;
    I thought that I could do something like this:

    Code:
    for ( var i:int = 0; i<= numberOfLoops; i++) {        
         var DataGrid[i]:DataGrid = new DataGrid();
         addChild(DataGrid[i]);
         DataGrid[i].dataProvider = dp;
    }
    But I can't figure out the syntax and how to make it work...
    Anybody have any ideas?! This MUST be possible!
    My knowledge is pretty thin in actionscript, so please be gentle to me! Thank you in advance!
    -Logan

  2. #2
    You can do something like this, but you'll need to stop thinking along the lines of accessing objects via declared variables. You'll want to access your object via an Array, Vector, or by the property name. I avoid name as it's just kinda sloppy imo. So, the way you're declaring your instances as DataGrid0, 1, 2... using an array is actually simpler (imo again) as you only need to rely on an integer 0,1,2 and not a name or specific instance.

    PHP Code:
    var dataGrids:Array = [];

    for ( var 
    i:int 0i<= numberOfLoopsi++) {        
         var 
    DataGrid:DataGrid = new DataGrid();
         
    dataGrids.push(DataGrid);
         
    addChild(dataGrids[i]);
         
    dataGrids[i].dataProvider dp;
         
    // see the next example, I wouldn't actually access them via the array, but it matches your setup

    granted, I'd use one function to build the instances and another to apply the dataProvider... maybe each instance needs a different data provider, in that occurance, I'd do something like this:


    PHP Code:
    var dataGrids:Array = [];

    // you could build these the same was as your dataGrids by, for example, looping XML nodes and setting all dataproviders first, then build the grids, etc.
    var allProviders:Array = [yourFirstDPyourSecondDPyourThirdDP];

    for ( var 
    i:int 0i<= numberOfLoopsi++) {        
         var 
    dg:DataGrid = new DataGrid();
         
    dataGrids.push(dg);
         
    addChild(dg);
         
    setData();
    }

    function 
    setData():void {
         for ( var 
    i:int 0i<= numberOfLoopsi++) {        
              
    dataGrids[i].dataProvider allProviders[i] as DataProvider;
         }     


  3. #3
    Thanks creatify!! That's awesome!

    It looks like I was actually sorta' close in my thinking...

    I have a few little questions.
    In your first example you create a DataGrid ever time it loops, in each loop you push that DataGrid onto the dataGrids array, incrementing the array index each time right? Is each item in the array then identical? I suppose that doesn't matter? Even if I am using the same DataProvider for each grid is it best practice to use a separate function to apply the DataProvider?

    For your second example, if I am only using one DataProvider would this be an appropriate way to write it?
    PHP Code:
    var dataGrids:Array = [];

    for ( var 
    i:int 0i<= numberOfLoopsi++) {        
         var 
    dg:DataGrid = new DataGrid();
         
    dataGrids.push(dg);
         
    addChild(dg);
         
    setData();
    }

    // I assume that this does not need to loop since it runs once each time the loop above runs... is that right?
    function setData():void {
         
    dataGrids[i].dataProvider dp;

    Lastly what does the as DataProvider; in the line dataGrids[i].dataProvider = allProviders[i] as DataProvider; do?

    Thank you again for your help (and for putting up with my silly questions)! I'm going to go try it right now!
    -Logan

  4. #4
    Quote Originally Posted by pillageTHENburn View Post
    ...incrementing the array index each time right? Is each item in the array then identical?
    Yes, you just keep pushing more items into the array.

    Quote Originally Posted by pillageTHENburn View Post
    I suppose that doesn't matter?
    It doesn't.

    Quote Originally Posted by pillageTHENburn View Post
    Even if I am using the same DataProvider for each grid is it best practice to use a separate function to apply the DataProvider? For your second example, if I am only using one DataProvider would this be an appropriate way to write it?
    No, if you're only using one DataProvider, avoid the loop and functions, keep it simple.

    Quote Originally Posted by pillageTHENburn View Post
    I assume that this does not need to loop since it runs once each time the loop above runs... is that right?
    Yeah, don't use any loops and just do it inline, not functions needed if you just have one.[/QUOTE]

    Quote Originally Posted by pillageTHENburn View Post
    dataGrids[i].dataProvider = allProviders[i] as DataProvider;
    This is simply setting each datagrid's (each of them that is in the array) to unique DataProviders that are stored in the allProviders array. This was just an example showing how to use a couple arrays of data grids and different data providers within a loop to set them.

  5. #5
    creatify, the second method of doing this actually loops over each element in the dataGrids array every time the setData method is called from the initial loop. This is a bit unnecessary.

    It would be better to create an iterator variable outside the scope of any methods. This way it can be accessed by any other methods.

    so something like this:

    PHP Code:
    var dataGrids:Array = new Array ();
    var 
    allProviders:Array = new Array (yourFirstDP,yourSecondDP,yourThirdDP);

    var 
    arrayIndex:int 0;

    createDataGrids ();

    function 
    createDataGrids ():void
    {
        for (
    arrayIndexarrayIndex allProviders.lengtharrayIndex++)
        {
            var 
    dg:DataGrid = new DataGrid ();
            
    dataGrids.push (dg);
            
    setData ();
            
            
    addChild (dg);
        }
    }

    function 
    setData ():void
    {
        
    dataGrids[arrayIndex].dataProvider allProviders[arrayIndex] as DataProvider;

    Although I would personally prefer to do it this way:

    PHP Code:
    var dataGrids:Array = new Array ();
    var 
    allProviders:Array = new Array (yourFirstDP,yourSecondDP,yourThirdDP);

    var 
    arrayIndex:int 0;

    createDataGrids ();

    function 
    createDataGrids ():void
    {
        for (
    arrayIndexarrayIndex allProviders.lengtharrayIndex++)
        {
            var 
    dg:DataGrid = new DataGrid ();
            
    dg.dataProvider allProviders[arrayIndex] as DataProvider;
            
    dataGrids.push (dg);
            
            
    addChild (dg);
        }

    pillageTHENburn, since you only need one DataProvider this is also unnecessary too lol. The first method posted by creatify will work perfectly for you.

    The reason creatify used 'as DataProvider' (typecasting) is because Flash won't necessarily know the type of Object you are trying to use unless you specifically tell it to treat an Object as a particular type, in this case DataProvider.

    hope that helps

  6. #6
    Quote Originally Posted by munkychop View Post
    creatify, the second method of doing this actually loops over each element in the dataGrids array every time the setData method is called from the initial loop.
    You're correct munkychop, thanks for catching that, I screwed up that function... it should have been:
    PHP Code:
    for ( var i:int 0i<= numberOfLoopsi++) {        
         var 
    dg:DataGrid = new DataGrid();
         
    dataGrids.push(dg);
         
    addChild(dg);
    }

    setData(); 
    So, just pull setData outside of the loop. 1) loop runs first, 2)setData() is called after the first loop. It is technically easier to just to use one function, but if you wanted to do more within the setData() function, that's the reason I separate the two. I try to keep certain specific tasks within their own individual functions so it's easier to edit, mainly visually.

Thread Information

Users Browsing this Thread

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012