as3 dynamic variable within a loop

I need to have the screenPage var accessible for later calls. I am dynamically creating several pages. The issue is when I call screenPage.startDrag(); then it only drags the last page. How can I add a var in the name or make all the screenPages accessible through code?

Here is code in short:

var screenPage:MovieClip;

for(var p = 1; daTotalPages >= p; p++)
{
    screenPage = new theFlagScreen();
}

Can I make screenPage a dynamic var and add like a 1 at the end and then a 2 and so forth with the loop?

Answers


Why not use an array ?

var pages:Array = new Array;

for(var p = 1; daTotalPages >= p; p++)
{
   var screenPage:MovieClip = new theFlagScreen();
   pages.push(screenPage)
}

Now you have an array containing all of your instances.

var myPage:MovieClip = pages[5] as MovieClip;
myPage.startDrag();

Also, the code you have above is creating an instance each time through, but since you then go ahead and create a new one each iteration and assign it to the same variable without storing it or adding it to the display list.... once you create a new one, the last one is marked for garbage collection.

The result is only the LAST instance you created still exists as it's the only one that has a variable that references it.


Need Your Help

Hovering over image map does not change cursor to hand in IE

html internet-explorer imagemap

I have an image with a map of two clickable areas. These work as expected on firefox and chrome. In IE, the cursor doesn't change to the 'hand'. I am able to click and navigate fine. So most things...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.