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

Restarting and variable scope

Explorer ,
Nov 07, 2016 Nov 07, 2016

Copy link to clipboard

Copied

Hello,

   I am using canvas and am setting some variables in the first frame of an animation. I want to be able to replay the animation and restart all of the children, but it seems that the vars I am setting are getting reset back to their original state when I rewind. Is there a clean way to not have the variables I am initializing change on restart?

function restart() {

   

    //console.log(doReplay);

    if (stage && stage.children) {

        var i, l = stage.children.length;

        for (i = 0; i < l; i++) {

            var child = stage.children;

            if ("gotoAndPlay" in child)

                stage.gotoAndPlay(0);

        }

    }

   

}

Views

354

Translate

Translate

Report

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

LEGEND , Nov 08, 2016 Nov 08, 2016

The variable ought to have automatically become a window variable, but it does seem to fail. Try setting it as a window variable:

if (!window.iHaveBeenInitialized) {

     window.iHaveBeenInitialized = true;

     alert("hi");

}

Votes

Translate

Translate
LEGEND ,
Nov 07, 2016 Nov 07, 2016

Copy link to clipboard

Copied

Then start your animation on the second frame instead of the first. Sometimes the simplest solution is the best.


If you simply must return to the same frame as your initialization code, when wrap it in a conditional like this:

if (!iHaveBeenInitialized) {

     iHaveBeenInitialized = true;

     initialization code...

}

Votes

Translate

Translate

Report

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
Explorer ,
Nov 08, 2016 Nov 08, 2016

Copy link to clipboard

Copied

Yes, I tried this, but it doesn't work because if you set iHaveBeenInitialized to false initially (you have to set it to something otherwise you get an undefined error) and then you rewind, it resets to false again.

There has to be a way to initialize global variables that don't reset on replay?

Votes

Translate

Translate

Report

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
LEGEND ,
Nov 08, 2016 Nov 08, 2016

Copy link to clipboard

Copied

The variable ought to have automatically become a window variable, but it does seem to fail. Try setting it as a window variable:

if (!window.iHaveBeenInitialized) {

     window.iHaveBeenInitialized = true;

     alert("hi");

}

Votes

Translate

Translate

Report

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
Explorer ,
Nov 08, 2016 Nov 08, 2016

Copy link to clipboard

Copied

LATEST

Thank you, this worked!

Votes

Translate

Translate

Report

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