Skip to main content
shasimo
Inspiring
September 20, 2017
Answered

Nested loop not working

  • September 20, 2017
  • 3 replies
  • 3816 views

Hi
I'm new to Animate and createJS, and I'm trying to learn the syntax, but I'm stuck with this code, where my loop wont loop, meaning that the outer loop stops after the first iteration.

var letters = ["M_container", "O1_container", "T_container", "I_container", "O2_container", "N_container"]

// Pause events

root.pauseBtn.addEventListener("click", pauseAll.bind(this));

function pauseAll() {   

    var j;

    var i;

    var containermc;

    for (j = 0; j < letters.length; j++) {

        containermc = letters;

        for (i = root[containermc].numChildren - 1; i >= 0; i--) {

            var mc = root[containermc].getChildAt(i);   

            mc.stop();

        }

    }

}

So all instances running in the first containermc "M_container" stops as intended, but then nothing else happens.

Also, I'm getting a console error from line 12 ( mc.stop();) , saying that it's not a function even though it works.


I hope someone can help me

P.s. Where can I find some guides for rewriting actionscript syntax to javascript/createJS?
createjs.com is fine for learning some things, but I'm an unexperienced coder, so I need some really basic stuff - maybe I should learn Javascript from the bottom?
Is native Javascript working in Animate? Sorry if it's a dumb question

This topic has been closed for replies.
Correct answer kglad

The letters are movieclips with instancenames "O1_container", "T_container" and so on.
Inside them there are other movieclips named "_1", "_2" and so on.
If it is indeed a shape, how come the mc.stop() work on the first O (O1_container) and not on the T?


you have a shape (the bg) on T that's not elsewhere.

use the timeline property to remedy:

if(mc.timeline){

mc.stop();

}

3 replies

shasimo
shasimoAuthor
Inspiring
September 21, 2017

Goodmorning
Now I'm getting somewhere: If I remove line 12 (mc.stop()), which is giving me the console error "not a function", the outer loop is working fine according to the console, and I am getting all the instancenames when doing "console.log(containermc);"

But when I run "console.log(mc);" from the inner loop, I get a bunch of this instead of the instancenames inside the letter movieclips:
"Object { _listeners: null, _captureListeners: null, alpha: 1, cacheCanvas: null, cacheID: 0, id: 88, mouseEnabled: true, tickEnabled: true, name: null, parent: Object, 30 more… }"

The instancenames are just an underscore followed by a number from 1 - 19.

I seams that I am making an error when targeting. In actionscript2 I would write _root.containermc._1.stop();  to stop the timeline in a specific mc.

shasimo
shasimoAuthor
Inspiring
September 21, 2017

So the error causing all my trouble is that I don't know how to write my stop function.
My variable mc is now containing the correct movieclip name "root.M_container._1" and so on.

But writing "mc.stop()", which in my logic should be the same as writing "root.M_container._1.stop()", does not work.

Inspiring
September 20, 2017

Your best friend in this situation is console.log(). You can make it write to the console inside the nested loop to check if the code actually runs or not. You can also check if mc is found, if you do console.log(mc), along with anything else you want to check.

To open the console you can press F12 and change to the console tab.

shasimo
shasimoAuthor
Inspiring
September 20, 2017

I already did that, and all the 'mc's in the first 'containermc' is found, so this is not my problem.
It's that the iteration of the outer loop stops finishing the inner loop, meaning that the outer loop does not continue to the next 'containermc'.

kglad
Community Expert
Community Expert
September 20, 2017

where's root defined?

shasimo
shasimoAuthor
Inspiring
September 20, 2017

Oh - in the beginning of my js. file: var root = this;

kglad
Community Expert
Community Expert
September 20, 2017

if that's on a different frame, it's not defined in the code you showed.