Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Nested loop not working

Contributor ,
Sep 20, 2017 Sep 20, 2017

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

3.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 22, 2017 Sep 22, 2017

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

use the timeline property to remedy:

if(mc.timeline){

mc.stop();

}

Translate
Community Expert ,
Sep 20, 2017 Sep 20, 2017

where's root defined?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 20, 2017 Sep 20, 2017

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 20, 2017 Sep 20, 2017

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 20, 2017 Sep 20, 2017

It's not; all the code is in the first frame.
And the function works, it pics up the movieclip ('containermc') targeted in the outer loop, and all the movieclips ('mc') inside of the 'containermc', but the looping stops and does not continue to the next 'containermc'.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 20, 2017 Sep 20, 2017

It's stopping because of the error, which may or may not be caused by a bug. Have you tried putting the code on the second frame, and letting it play the first frame?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 20, 2017 Sep 20, 2017

No... I'm not sure what you mean...? Should I move this function to frame 2?
And by error - do you mean the mc.stop(); error I'm getting?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 20, 2017 Sep 20, 2017

Yes, you can move all the code to frame 2 as well as add this.top(); at the top, just to see if that fixes it

And yes, any error that your code generates will stop it from running.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 20, 2017 Sep 20, 2017

I'll try to do so tomorrow morning (it's late now and I need some dinner and sleep )
I'll get back with the results - thanks for your help so fare!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 20, 2017 Sep 20, 2017

if root's defined, the code is ok.  it's probably a letters issue that you need to check.

ie, find for which j does you code fails and then check for the object.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 20, 2017 Sep 20, 2017

Okay - I'll try to work on that tomorrow, and hopefully get back with a positive report on the results
Thanks for now!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 20, 2017 Sep 20, 2017

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 20, 2017 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 20, 2017 Sep 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'.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 21, 2017 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 21, 2017 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 22, 2017 Sep 22, 2017

That's actually normal. It's logging the object and all it's properties.

The way you are stopping the movieclips should work though, so could you upload the project and link it?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 22, 2017 Sep 22, 2017

I've put a copy here: motion

Please don't laugh out to loud - I know it's quit ugly coding, but it's come together as chunks of ideas and has been used for practicing and learning.


It's highly CPU consuming which I guess is caused by my many timelines in the gears? Or is it the ugly coding or something with caching?


Strange thing: If you take a look at function O1clicked(), with the console opened, you'll see that it does not return an error from the mc.stop()  as it does in my initial problem with the loops. On function Tclicked() it does!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 22, 2017 Sep 22, 2017

I can't see the code that you wrote from the page you linked, I would need the project file to see it.

I get the error you mentioned, but only when clicking on the T but it still works as expected it seems, other than that I don't get any errors. If you're getting errors in other cases, it may be because of your operating system and browser.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 22, 2017 Sep 22, 2017

Oh - I thought you would be able to see it in the console...
I put a zip here

The reason the pausebutton doesn't return an error anymore will be obvious when you see the file
Definitely not a very good piece of code!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2017 Sep 22, 2017

you're trying to apply a stop() to a shape.  ie, the child 0 is a shape.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 22, 2017 Sep 22, 2017

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2017 Sep 22, 2017

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

use the timeline property to remedy:

if(mc.timeline){

mc.stop();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 22, 2017 Sep 22, 2017

Aha! Because I have the background i the T it gives me the error on the mc.stop()?


And if I write your code instead of just mc.stop(); it will work?
Will it also do the trick on my nested loop on the pausebtn?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2017 Sep 22, 2017

if you see the same error, yes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines