12 Replies Latest reply on Mar 14, 2016 2:31 PM by ClayUUID

    variable in moviClip

    lukash55376775 Level 1

      I have a movieClip called M1 and in that movieclip I define a variable like this.

      this.foo = 100;

       

      In M1 I put an other movieclip called M2.

      In M2 I try to get the variable foo from M1 like this.

       

      console.log(this.parent.foo);

       

      The result is this.parent not defined.

       

      What Im I doning worng?

        • 1. Re: variable in moviClip
          little fat girl Level 1

          I think you want MovieClip(parent).foo.

           

          Try that. I don't think they really want you doing stuff like that, but most of it is still there.  You just need to know the workarounds.

          • 2. Re: variable in moviClip
            ClayUUID Adobe Community Professional

            I suspect that what you're doing wrong is not doing exactly what you're saying you're doing, because what you're saying you've done should work perfectly. "this.parent" is fundamentally valid JavaScript, and should never, ever raise an error.

            • 3. Re: variable in moviClip
              Colin Holgate MVP & Adobe Community Professional

              MovieClip(parent).foo would be the AS3 solution.

               

              For me it's working as you have it. Make sure you're showing the Logs tab, and not the Errors tab.

              • 4. Re: variable in moviClip
                lukash55376775 Level 1

                Yee It works but only if you put the code console.log(this.parent.foo); on the first frame in M2.

                Relly stange if you put it on another frame it cant fint this.parent

                 

                check this two examples

                 

                Sprend

                • 5. Re: variable in moviClip
                  ClayUUID Adobe Community Professional

                  Well you seem to be writing Canvas code, but the thread has the AS3 solution marked as the correct answer, so I don't know what you're doing.

                  • 6. Re: variable in moviClip
                    lukash55376775 Level 1

                    Well Clay I'm writing canvas code and I have not markt this thread as solved. Someone else did.

                    Check out my examples two files one testW.fla working perfect and testF.fla dont work just the different that the in the M2 clipp i put the this.parent at frame 10

                    • 7. Re: variable in moviClip
                      Colin Holgate MVP & Adobe Community Professional

                      I don't know who marked me as being correct, though I probably am! ;-)

                       

                      Where are your example files?

                      • 8. Re: variable in moviClip
                        Colin Holgate MVP & Adobe Community Professional

                        Without seeing your example files yet, one guess is that you are referencing a movieclip in the first frame that it exists. The frame script happens before the movieclip exists, so either have the script on the frame after the movieclip first appears, or put the script into the movieclip itself.

                        • 9. Re: variable in moviClip
                          ClayUUID Adobe Community Professional

                          Uh-oh, looks like we have a rogue moderator on the loose.

                           

                          Since your testF file works if I change the gotoAndPlay(12) to gotoAndPlay(11), I'd say this is a race condition between the parent clip getting initialized and the child clip code executing. This is especially counterintuitive here since the init code clearly comes before the play code, but lord only knows how the execution order is being managed by CreateJS.

                           

                          The lesson to be learned here is never, ever, attempt to access freshly declared variables in another clip on the same frame that they're declared. This was a bad idea in ActionScript, and it's still a bad idea. Always provide at least one frame of slack for clips to fully initialize themselves.

                           

                          Also, clips reaching up into their parents to retrieve data should generally be avoided as well. It's MUCH simpler to debug and keep track of things when you have a single, consolidated chunk of parent code setting all clip properties.

                          • 10. Re: variable in moviClip
                            Colin Holgate MVP & Adobe Community Professional

                            As a point of interest, the race condition was true in Flash Player 9, and was changed in Flash Player 10 so that you could safely talk to a movieclip that only just existed. It looks like Canvas is from the Flash Player 9 school of thinking!

                            • 11. Re: variable in moviClip
                              lukash55376775 Level 1

                              Thanks for your answers Clay and Colin it was really helpful and informative.

                              I have some questions

                              1. Is all the code generated by animate a part of the library CreateJS. In other words, do you find documentation of all the problems you can run into in the createJS documentation?

                              2. In my example, is it possible from the M2 clip to get access to other clips on the scene/top-level with out using the parent selector. In ActionScript there was a statement/selector named ROOT (correct me if im wrong). Is there a equivalent in canvas?

                              • 12. Re: variable in moviClip
                                ClayUUID Adobe Community Professional

                                Yes, in Canvas mode Flanimate is entirely an IDE for the CreateJS library. Any functionality not provided by CreateJS will have to come from the core JavaScript language, or other third-party libraries.

                                 

                                I'd guess the closest analogue of _root in Canvas mode is the Stage object. This code, executed on the main timeline, yields true:

                                 

                                console.log(this.parent===stage);

                                 

                                However, as you can see stage sits one level above the main timeline, and there's no way to name the main timeline, so you can't (easily) deep reference anything via stage. So unless I've overlooked something, you'll have to create your own root reference by coding _root = this; on the main timeline.