Leon Gaban
Leon Gaban

Reputation: 39028

Why am I getting the "Duplicate variable definition" error here?

It think Flash is telling me that my array tabData is getting duplicated, but I only set it up once in the var section of my code:

This is the line with the error:

for (var i in tabData) {

TabMenu class

private var tabData:Array = []; // <- tabData created here

public function drawTabMenu(w, h, color, videoHDiff, ypos):void
    {           
        trace("drawTabMenu --------------");

        for (var i in Global.xml..tab) {
            tabData.push( {id:Global.xml..tab[i].@id, video:Global.xml..tab[i].vid} );
        }

        // DRAW GRAPHICS CODE HERE...

        //draw the tabs
        for (var i in tabData) { // < line throwing error

            var tab:MovieClip = new TabDefault();
            var active:MovieClip = new TabActive();
            tabArray.push(tab);
            tab.video = tabData[i].video;
            tab.addChild(active);
            i < 1 ? active.visible = true : active.visible = false;
            tab.y = topShadow.y + 5;

            // add in the textfield here
            // addChild(tf);

            // resize the tab background to textfield
            tab.x = i < 1 ? 10 : tabArray[(i-1)].x + (tabArray[(i-1)].width+3);
            tab.active = i < 1 ? tab.active = true : tab.active = false;
            topShadow.addChild(tab);

            tab.mouseChildren = false;
            tab.buttonMode = true;
            tab.addEventListener(MouseEvent.CLICK, tabClick);
        }

        // set default thumbnails here
        trace("FIRST TAB DATA: "+tabData[0].video);
    }

Upvotes: 0

Views: 863

Answers (2)

Carl Manaster
Carl Manaster

Reputation: 40336

I don't know flash, but is the fact that you use i as the loop variable in both loops a problem? I think it shouldn't be - certainly wouldn't be in Java - but maybe that's it.

Also, unrelated to your problem, this line:

tab.active = i < 1 ? tab.active = true : tab.active = false;

would be easier to read like this:

tab.active = i < 1;

Again, assuming flash works like languages I know better.

Upvotes: 3

Michael Brewer-Davis
Michael Brewer-Davis

Reputation: 14276

i is the duplicate variable, not tabData. Actionscript allows only function scope, not block scope as in many (most) other languages.

Promoting apparently block level scope variables to function scope is called hoisting.

Resources:

Upvotes: 3

Related Questions