0

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?

1
  • I don't know what else to change to make this better. thanks Commented May 7, 2013 at 7:20

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, Thanks. That did it. Amazing! I love stackoverflow people! So fast, so giving!
That would be helpful. Thanks. I don't think you get my comments if the question is answered. I guess. I love this site but I wish they would make it a little more free. I am trying my best.
1

screenPage is a reference to just ONE thing. In your loop you keep overwriting it as you go and so you end up with it pointing to the very last instance of theFlagScreen... all the others are now lost.

You should follow prototypical's advice and store these instances in an array, that way you don't lose them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.