Results 1 to 6 of 6
Thread: Looping and variables
-
July 28th, 2010, 12:20 PM #147Order is important.
postsLooping 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):
I thought that I could do something like this: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;
But I can't figure out the syntax and how to make it work...Code:for ( var i:int = 0; i<= numberOfLoops; i++) { var DataGrid[i]:DataGrid = new DataGrid(); addChild(DataGrid[i]); DataGrid[i].dataProvider = dp; }
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
-
July 28th, 2010, 06:06 PM #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.
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 = [];
for ( var i:int = 0; i<= numberOfLoops; i++) {
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
}
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 = [yourFirstDP, yourSecondDP, yourThirdDP];
for ( var i:int = 0; i<= numberOfLoops; i++) {
var dg:DataGrid = new DataGrid();
dataGrids.push(dg);
addChild(dg);
setData();
}
function setData():void {
for ( var i:int = 0; i<= numberOfLoops; i++) {
dataGrids[i].dataProvider = allProviders[i] as DataProvider;
}
}
-
July 29th, 2010, 04:05 PM #347Order is important.
postsThanks 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?
Lastly what does the as DataProvider; in the line dataGrids[i].dataProvider = allProviders[i] as DataProvider; do?PHP Code:var dataGrids:Array = [];
for ( var i:int = 0; i<= numberOfLoops; i++) {
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;
}
Thank you again for your help (and for putting up with my silly questions)! I'm going to go try it right now!
-Logan
-
July 29th, 2010, 04:47 PM #4
Yes, you just keep pushing more items into the array.
It doesn't.
No, if you're only using one DataProvider, avoid the loop and functions, keep it simple.
Yeah, don't use any loops and just do it inline, not functions needed if you just have one.[/QUOTE]
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.
-
July 29th, 2010, 04:48 PM #557Registered User
postscreatify, 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:
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 (arrayIndex; arrayIndex < allProviders.length; arrayIndex++)
{
var dg:DataGrid = new DataGrid ();
dataGrids.push (dg);
setData ();
addChild (dg);
}
}
function setData ():void
{
dataGrids[arrayIndex].dataProvider = allProviders[arrayIndex] as DataProvider;
}
pillageTHENburn, since you only need one DataProvider this is also unnecessary too lol. The first method posted by creatify will work perfectly for you.PHP Code:var dataGrids:Array = new Array ();
var allProviders:Array = new Array (yourFirstDP,yourSecondDP,yourThirdDP);
var arrayIndex:int = 0;
createDataGrids ();
function createDataGrids ():void
{
for (arrayIndex; arrayIndex < allProviders.length; arrayIndex++)
{
var dg:DataGrid = new DataGrid ();
dg.dataProvider = allProviders[arrayIndex] as DataProvider;
dataGrids.push (dg);
addChild (dg);
}
}
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
-
July 29th, 2010, 04:52 PM #6
You're correct munkychop, thanks for catching that, I screwed up that function... it should have been:
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.PHP Code:for ( var i:int = 0; i<= numberOfLoops; i++) {
var dg:DataGrid = new DataGrid();
dataGrids.push(dg);
addChild(dg);
}
setData();

Reply With Quote


Bookmarks