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.2K
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
Contributor ,
Sep 22, 2017 Sep 22, 2017

But NOW it works!

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

        containermc = letters;

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

            //mc = ("root." + [containermc] + "._" + );

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

            console.log(mc);   

            if (mc.timeline) {

                mc.stop();

            };           

        }


Thanks very much for your help - both of you!

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

But... now I'm faced with the fact, that I don't really know what I'm doing with "timeline" because it doesn't work with the play button.
I guess it's because the timelines have been stopped? What to do... can I put some 'not' operator in to fix it?

function playAll() {

    var j;

    var containermc;

    var mc;

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

        containermc = letters;

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

        console.log(mc);

        if (mc.timeline) {

            mc.play();

        };

    }

}

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

what's wrong with that 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
Contributor ,
Sep 22, 2017 Sep 22, 2017

I'f I used the pause button to stop all the mc's, then I can not start them again using that code. How come?

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

what code are you using to re-start them?

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

This:

function playAll() {

    var j;

    var containermc;

    var mc;

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

        containermc = letters;

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

        console.log(mc);

        if (mc.timeline) {

            mc.play();

        };

    }

}

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

It start the O1_container, which has a timeline in it, but non of the gears are playing.

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

where's the inner loop?

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 dear... how embarrassing...

Sorry for that, and thanks again!

Can you tell me if it is possible to ad some sort of delay, so the gears will stop/start in a sequence?

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

yes, but you might find it easier to just add an increasing number of frames at the start of each gear where they do not tween.

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

Thanks, but that won't have the desired effect I'm afraid, because they are placed in different orders in the letters, so the result would be very randomly looking. I wanted it to look like they stopped from the left to the right.
I hoped there was some easy way to attach some milliseconds for the loop to iterate - or something like that

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 could use code but the child gears would need to be at the depth you want to get the effect you want:

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

        containermc = letters;

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

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

            if (mc.timeline) {

            setTimeout(stop_gearF,500*i,mc);

            };           

        }

function stop_gearF(mc){

mc.stop();

}

likewise with play()

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

Thanks a lot!

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
LATEST

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