2

I'm trying to hide movie clips from the stage using a loop, but I'm getting the following error when I publish it (this is just an example of the concept.)

var q;
for (q = 0; q <= 3; q++) {
   stage["box_mc_" + q].visible = false;
}

The Error message:

ReferenceError: Error #1069: Property box_mc_0 not found on flash.display.Stage and there is no default value. at test_fla::MainTimeline/frame1()

ReferenceError: Error #1069: Property box_mc_0 not found on flash.display.Stage and there is no default value. at test_fla::MainTimeline/frame1()

Any help would be appreciated.

someOne
  • 1,975
  • 2
  • 14
  • 20
Mike P
  • 29
  • 2

2 Answers2

3

You simply need to use the this keyword instead of the stage variable:

var q;
for (q = 1; q <= 3; q++) {
   this["box_mc_" + q].visible = false;
}

The simple reason being is that the objects are the children of a MainTimeline object, to which the this keyword would refer to in that context, and they are not the direct children of the stage.
If you're interested for more details on the difference, you may find useful information here.

Community
  • 1
  • 1
someOne
  • 1,975
  • 2
  • 14
  • 20
  • Thank you so much! – Mike P Dec 05 '16 at 02:29
  • @MikeP You're welcome, if you find the answer useful and it resolved your issue, please consider to [accept it as an answer](http://stackoverflow.com/help/someone-answers) :) – someOne Dec 05 '16 at 08:01
1

thats not realy child of stage,

if you try it, to tracing all of the stage childs

for (var i:int =0; i<stage.numChildren; i++) {
    trace(stage.getChildAt(i).name);
}

then you can see an intermediate container exists between stage and box_mc_# that's "root", because you added box_mc_# via time line

so in my case, the correct call way is

var q;
for (q = 0; q <= 3; q++) {
   stage.getChildByName("root1")["box_mc_" + q].visible = false;
}
payam_sbr
  • 1,428
  • 1
  • 15
  • 24
  • @someOne wt? that's real answer to above question (with some little typo mistakes), also don't be fool with down vote button – payam_sbr Dec 04 '16 at 06:45
  • 1
    There would be **another** _ReferenceError, Error #1069_, in your code while accessing the `stage["root1"]` for which you need to replace with `stage.getChildAt(0)` (regardless of your typo!), and the "down vote" button is there to make the things organized in this site :) – someOne Dec 04 '16 at 06:54
  • @someOne thank you, but as you can see above, i said _**in my case**_ !!!!!!!!!!! and in the first code block a trace call provided the real name! that's not a constant, even i named it "root" there not "root1". – payam_sbr Dec 04 '16 at 07:13
  • 1
    Your _specific_ case doesn't resolve the problem, but adds ambiguity to the issue by unintentionally conveying the concept incorrectly! Your code won't work, even if you remove the trailing number in `root1`!! i.e: the `stage.root` is not necessarily equal to the `stage.getChildAt(0)`, more information [here](http://stackoverflow.com/a/7346385/3709765) :) – someOne Dec 04 '16 at 07:34
  • Your [last edit](http://stackoverflow.com/posts/40955961/revisions) is fine now and deserves taking back the "down vote" :) – someOne Dec 04 '16 at 07:44
  • @someOne thank you, now it works (just for avoiding an incorrect answer) but you'r right. – payam_sbr Dec 04 '16 at 07:45