views:

15

answers:

1

I'm having a very odd bug with ActionScript 3 in Flash CS4. I am adding movie clips to a stage in a for loop and then moving them out of view so that I can pull them in and remove them when I need them.

I've narrowed down the issue to a point that I know that every time one of the movie clips are added to the stage using addChild(), the stage shifts to the right by one pixel. I know that sounds odd, but it's literally true... the 0 line on the y axis is shifted to the right one pixel every time the movie clip is added. I have no idea how this could be happening.

Here's the code that is doing the work:

        private function setupSlides():void 
    {
        for(x = 0; x < TOTAL_SLIDES; x++)
        {
            var ClassReference:Class = getDefinitionByName("Slide" + (x+1)) as Class;
            var s:MovieClip = new ClassReference() as MovieClip;
            s.x = 9999;
            s.y = 9999;             
            addChild(s);
            slides[x] = s;
        }
    }

Any thoughts?

A: 

After posting, I noticed that I had not declared the loop counter variable (x). I declared it, and the strange rendering is gone. I wonder why it just didn't give me an undeclared variable error?

Here's the fixed code:

        private function setupSlides():void 
    {
        for(var x:int = 0; x < TOTAL_SLIDES; x++)
        {
            var ClassReference:Class = getDefinitionByName("Slide" + (x+1)) as Class;
            var s:MovieClip = new ClassReference() as MovieClip;
            s.x = 9999;
            s.y = 9999;             
            addChild(s);
            slides[x] = s;
        }
    }
I bet it's because x by itself is an implied this.x which would refer to the stage's own x and y coordinates. Which is why it's better to use a different letter like "i" for a loop. Thanks StackOverflow! I'm so glad we could have this little conversation.
yeah; best to use _x or similar. i also usually use i in all for loops unless there is either 1.) i am nesting another for loop, or 2.) i need to be more specific about a variable for readability.
jml