1 Reply Latest reply on Oct 7, 2006 6:04 PM by ITBytez

    AS 2.0 variable declaration queston: super basic but need help!

      I've been teaching myself AS 2.0 and doing OK with it -- read both Moock books (I'd love it if there were a comprehensive book for AS 2.0 as there is for MX -- the "Essentials" book is great, but not enough).

      In any case, I don't fully understand how authored instances interact with AS 2.0.

      If I put an instance on the stage manually, onto the main timeline, within the IDE, and then give it an instance name, e.g. "container_movie_mc", then I can simply refer to it from the first frame on the same timeline as "container_movie_mc" and get properties/methods of it? Can I call it this from any class called from the code on the main timeline? This doesn't always seem to work, e.g. I sometimes can't seem to do something as simple as container_move_mc.createNewEmptyClip() (or whatever the exact call is -- yes, I still rely on the popup code hints…). Do I need to reference it as _root.container_movie_mc ? Is it available like that from all classes?

      Also, if I have an instance, container_movie_mc on my main timeline, and then declare

      var container_movie_mc:MovieClip;

      on the first frame of the main timeline, will this still reference the IDE instance? It seems to -- but I would think it would make a new variable, at least according to what I understand of scoping rules.

      In short, I understand scope pretty well (except for callbacks), and referencing timeline variables using dot notation, but the basic nature of these undeclared (or implicity declared) variables floating around confuses me -- plus I don't understand why if I declare a variable with the same name it seems to remain the same variable as the one authored in the IDE, instead of overriding it? (Obviously the former behavior is preferable, I just don't understand it).

      Please help ASAP if you can!


        • 1. Re: AS 2.0 variable declaration queston: super basic but need help!
          Well, I can attempt to answer a couple of your questions.
          First off, when doing callbacks I almost always use the Delegate class.

          When it comes to declaring variables that have the same name as a timeline object; The IDE should either require the declaration, or prevent it. in my experience, there is no need to declare it, and "sometimes" it will muck* things up if you do. So the solution is simple... Don't do it.
          Sometimes it is nice to declare all of your variables (even for timeline objects) so that you get those handly little "popup code hints".
          in this case I use something like this:
          var mc_container_movie:MovieClip = container_movie; //container_movie is the name of the timeline object
          Someone else may say that's silly, but it works for me.

          As for the availability of timeline objects... Anytime that a movie clip is instantiated, on the timeline, or in code, then you can reference it from anywhere. ie. _root.container_movie
          However if your timeline object is only on the stage from frame 1 to 15, then it is no longer a valid reference on frame 16. Well actually it is a valid reference, but it returns "undefined".

          Let me caution you on the use of _root. If you decide to use a movie clip inside of another clip, then your references to _root will now be pointing to the new root! I'm not saying you shouldn't use _root, I use it frequently. Just make sure that you are thinking about future uses of your movieclip. Sometimes* it is better to use the _parent property instead of _root.

          Good luck and welcome to ActionScripting!