6 Replies Latest reply on Oct 3, 2008 8:40 AM by Newsgroup_User

    attach movie in as3

    Slpixe
      Hey all, I'm fairly new to flash, but I can do a few things, I had a
      good flash program going in as1, which I'm trying to convert to as3.

      I had asked on irc for some help, but it was touching on subjects I have
      not covered on as, and even after looking up and applying, its causing
      countless problems.

      1) My application receives information currently from echoed information
      from a db onto a html page, which flash is able to feed in e.g. (echo
      &var+rowvar+&).
      I'm considering xml although I'm unsure about benefits or anything

      2)in as1 my application could simple do a for loop with the number being
      passed in via the number of rows in the database which was echoed onto
      the page then fed into flash.
      Do for loops act the same in as3?

      3)I was attaching a movie clip multiple times, which I need to get to
      act all independent of each other, so each one needs its own name, as1
      this was possible via "_root.attachMovie("button","btn"+i, 100+i);"

      and this is where the main problem is, as I was asking about this,
      attachmovie no longer works, and I got told to sort out "linkage" which
      I have done, then setting up class and using addchild. which I have
      tried... but it does not work and I cannot get around.

      reading up on it, documents say addchild should enable the object to be
      displayed. which for me isnt happening.

      Here are the list of problems I have been getting:
      -An ActionScript file must have at least one externally visible
      definition. so I looked it up, and I figured I have to put up a
      package and public class (athough I do not understand class's)

      which resulted in the error
      -TypeError: Error #1006: addChild is not a function. at global$init()
      which sounds great...

      messing around got me to
      -5008: The name of definition 'test' does not reflect the location of
      this file. Please change the definition's name inside this file, or
      rename the file. z:\location\index.as

      so I changed the class "test" to "index" where I encountered more
      problems...

      [code]
      var page1:page_mc = new page_mc ();
      this.addChild(page1);
      [/code]

      [code]
      package
      {
      import flash.display.MovieClip;

      public class index extends MovieClip
      {
      public function index()
      {
      trace('lol');
      }
      }
      }
      [/code]

      and by the way, I have tried using addchild on frame1 of the fla, with
      no results.

      I'm sure I'm missing something at the biginning because looking up every
      problem and getting more errors seams silly for something that was so
      easy in as1, that I would need so much code to stop problems of 1
      function..)


      Thanks in advance
      Slpixe
        • 1. Re: attach movie in as3
          Level 7
          Slpixe,

          > 1) My application receives information currently from
          > echoed information from a db onto a html page, which
          > flash is able to feed in e.g. (echo &var+rowvar+&).

          I'm with ya so far.

          > I'm considering xml although I'm unsure about benefits>
          > or anything

          If you're passing in a handful of name/value pairs, you probably don't
          need XML. By what you're describing, it sounds like you're using FlashVars,
          or passing these variables into the SWF as a query in the HTML. The benefit
          to FlashVars is that the SWF has access to these values immediately upon
          load. You don't have to take measures to load the data yourself, and then
          to respond to onLoad events, and so on -- they're just magically there.

          Prior to AS3, those variables are scoped to the main timeline, as if you
          had declared them there in frame 1. In AS3, you can use the same technique,
          but the variables are scoped as properties of the parameters property of a
          LoaderInfo instance associated with the main timeline. Sounds like a
          mouthful, but all it means is that you have to path to the incoming
          variables:

          this.loaderInfo.parameters.myVar1;
          this.loaderInfo.parameters.myVar2;
          this.loaderInfo.parameters.myVar3;

          If you have tons of hierarchical data, or data that don't lend
          themselves to simple name/value pairs, XML will help considerably. Of
          course, you'll have to load the XML, then navigate among your nodes to
          retrieve the information, but AS3's E4X syntax makes this much easier than
          it was to do the same with XML in AS1/AS2.

          > 2)in as1 my application could simple do a for loop with the
          > number being passed in via the number of rows in the database
          > which was echoed onto the page then fed into flash.
          > Do for loops act the same in as3?

          Yes.

          > 3)I was attaching a movie clip multiple times, which I need to
          > get to act all independent of each other, so each one needs
          > its own name, as1 this was possible via "_root.attachMovie
          >("button","btn"+i, 100+i);"

          Okay.

          > and this is where the main problem is, as I was asking about this,
          > attachmovie no longer works, and I got told to sort out "linkage"
          > which I have done,

          Meaning, instead of a linkage identifier, a linkage class.

          > then setting up class and using addchild. which I have tried... but
          > it does not work and I cannot get around.

          Heh. Well, it does work, I promise you that. ;) So it should just be
          a matter of stepping through the approach you're using to see what's going
          awry when.

          > reading up on it, documents say addchild should enable the
          > object to be displayed. which for me isnt happening.

          The addChild() method originates with the DisplayObjectContainer class,
          which means you need an instance of that class as a reference when you
          invoke the method. Makes sense, right? You couldn't instantiate an an
          array, for example, and do this:

          var arr:Array = new Array();
          arr.addChild(someVisualObject);

          ... because the Array class doesn't support the addChild() method. It's the
          DisplayObjectContainer class that does, so you can only add objects to the
          display list of an object that supports display lists.

          Fortunately, the main timeline is (usually) an instance of the MovieClip
          class, and MovieClip inherits much of its functionality from the
          DisplayObjectContainer class. Even when the main timeline is configured as
          a sprite, it still inherits the functionality in question, because the
          family tree goes like this: DisplayObjectContainer --> Sprite -->
          MovieClip.

          So all you really need, in theory, is to invoke that method on any movie
          clip symbol's instance name, or on a reference to the main timeline, and
          your object should show up.

          > Here are the list of problems I have been getting:
          > -An ActionScript file must have at least one externally visible
          > definition. so I looked it up, and I figured I have to put up a
          > package and public class (athough I do not understand class's)

          I'm assuming, then, that your code -- at least, currently -- all appears
          in keyframes?

          > which resulted in the error
          > -TypeError: Error #1006: addChild is not a function. at global$init()
          > which sounds great...

          Hrrrm.

          > messing around got me to
          > -5008: The name of definition 'test' does not reflect the location of
          > this file. Please change the definition's name inside this file, or
          > rename the file. z:\location\index.as

          So then, it sounds like you're putting your code inside a file named
          index.as, which means (by the rules of how Flash operates) that you're
          defining a class called index. If you're using the class keyword to define
          this class, it means you'll have to use the word "index" as the name of your
          class, because class names have to match the names of the files in which
          they're defined.

          > so I changed the class "test" to "index" where I encountered more
          > problems...
          >
          > [code]
          > var page1:page_mc = new page_mc ();
          > this.addChild(page1);
          > [/code]

          To me, it looks like you've put these first few lines ahead of your
          package and class declarations. That's now how class files work. All of
          your code must be sandwiched inside the package or (usually) class
          declaration. Otherwise, you're not writing a class at all (which is fine)
          ... but then, you need to use the include directive (just like AS1/AS2's
          #include), which simply pulls the code in at compile time as if it were
          typed in a timeline keyframe.

          In this code:

          > [code]
          > package
          > {
          > import flash.display.MovieClip;
          >
          > public class index extends MovieClip
          > {
          > public function index()
          > {
          > trace('lol');
          > }
          > }
          > }
          > [/code]

          You're defining a class named index that extends MovieClip. That means
          index *is* a movie clip, which means it has available to it all the
          functionality defined by the MovieClip class. If you were to invoke
          addChild() from some method *inside* this class declaration, it would work
          ... because the addChild() method is supported by movie clips.

          > and by the way, I have tried using addchild on frame1 of the fla,
          > with no results.

          Mixing and matching is fine. You can indeed use timeline code alone in
          AS3, or class code alone, or a combination of both. I wonder if you're just
          "in too deep" with the file you have? I recommend you brush away the
          distractions and start a fresh, new FLA file.

          In that file, draw a quick shape -- just a circle, say -- and convert it
          to a movie clip. Right-click on that symbol in the Library and choose
          Linkage. Select "Export for ActionScript" and give this thing a linkage
          class (for example, Circle).

          When you click OK to close the dialog box, Flash will warn you that no
          such class (Circle) exists, and offers to write that class for you. Click
          OK.

          Now enter frame 1 and use the Actions panel to type these lines:

          var orb:Circle = new Circle();
          addChild(orb);

          The variable orb (just an arbitrary name) is typed as Circle -- which is
          the class Flash just wrote for you -- and set to an instance of that class.
          In the Linkage dialog box, you will have seen that the Circle class extends
          MovieClip, so this isn't much different from instantiating a new movie clip
          in AS3.

          Finally, the DisplayObjectContainer method, addChild(), adds your Circle
          instance to the display list of the main timeline. You could precede the
          addChild() reference with the "this" keyword, but even without it, Flash
          understands that you're refering to the timeline in which this code appears.
          And because that timeline is a descendent of the DisplayObjectContainer
          class, the method works.


          David Stiller
          Co-author, The ActionScript 3.0 Quick Reference Guide
          http://tinyurl.com/2s28a5
          "Luck is the residue of good design."


          • 2. Re: attach movie in as3
            Level 7
            Slpixe,

            > Yeah your circle example worked great =D, I will learn
            > myself how to manipulate it to move it and resize etc.

            Cool. :)

            > so I went on and started on the declaring
            >
            > [code]
            > public class page extends MovieClip
            > {
            > public function page()
            > {
            > trace('created');
            >
            > var page1:page();
            > addChild(page1);
            > }
            > }

            It looks like you're trying to instantiate an object inside itself. Is
            this a document class?

            > which resulted in "1046: Type was not found or was not a
            > compile-time constant: ."

            Hrrm. That doesn't strike me as a useful message -- but that happens.
            ;) If this page class is a document class, then you'd want this document
            class to instantiate the Circle class I suggested earlier (or whatever
            linkage class[es] you're using).

            > some more solutions was people mentioning if your mc
            > has a text box in it, your need to import text.textfield
            > and stuff.

            Unfortunately, I'm not sure at this point what you're after. Can you
            rephrase, or maybe give break it down a bit more? If you're putting your
            code in timeline keyframes, you don't have to import any classes in the
            flash package -- and you can't use package or class keywords. If you're
            putting your code in class files, then you have to import everything -- with
            the exception of classes (such as Circle) written for you by Flash.

            > so I remembered you mention DisplayObjectContainer, and
            > Tried importing that, with no result.

            Importing it where? (This is where I'm losing you.) What an import
            statement does is clarify for the compiler what class definition you mean
            when you instantiate a class. If you're not referencing the
            DisplayObjectContainer class directly, you don't need to import it. (And I
            realize I may not be answering your question, so again ... tell me where I'm
            misunderstanding you.) Your class extends MovieClip (which inherits from
            DisplayObjectContainer), so simply by virtue of extending MovieClip, your
            class (page) has access to the addChild() method. If you don't have an
            import statement for the MovieClip class inside your package declaration,
            that might be the problem.

            package {
            import flash.display.MovieClip;
            public class page extends MovieClip {
            public function page() {
            trace("created");
            var orb:Circle = new Circle();
            addChild(orb);
            }
            }
            }


            David Stiller
            Adobe Community Expert
            Dev blog, http://www.quip.net/blog/
            "Luck is the residue of good design."


            • 3. Re: attach movie in as3
              clbeech Level 3
              Geeeeez! Dave! it's like your a book writer or something... LOL!!!! ;)
              • 4. Re: attach movie in as3
                Level 7
                clbeech,

                > Geeeeez! Dave! it's like your a book writer or
                > something... LOL!!!! ;)

                Heh. True. It's a weakness. :-p


                David Stiller
                Adobe Community Expert
                Dev blog, http://www.quip.net/blog/
                "Luck is the residue of good design."


                • 5. Re: attach movie in as3
                  Slpixe Level 1
                  >< alright this is getting fairly confusing, so for now, I will try explaining some of it.

                  My fla file, has.. 2 mc's inside it, 1 called "page_mc" which I linkaged to be "page", and 1 called "btn_btn" or something but the button dosn't matter right now.

                  I have the document class as "index" so that my index.as file in the same folder, is picked up.

                  in that .as file. I have(want) all the code to be there (so there is no as code in the frames and stuff).

                  in the .as file.

                  I have

                  [code]
                  package
                  {
                  import flash.display.MovieClip

                  public class page extends MovieClip
                  {
                  public function page()
                  {
                  trace('created');

                  var page1:page();
                  addChild(page1);
                  }

                  }
                  }
                  [/code]

                  As far as I read this(and want it)
                  It imports the information required to do movieclip functions (etc addchild)
                  and creats a function called "page" which inside will output the word "created" as a test
                  and I want it to put my "page_mc" which is linkaged to be called "page" on the stage, and have the page_mc called page1.

                  after I will put in a loop so I can have multiple page_mc's with different names eg page1 page2 page3

                  I will later be having text box's and pictures inside the page_mc's which I will also want to be able to call (hopefuly its nice and clean code like 'page1.text1.text = "workkkkkkk"'

                  etc.... Anyway I hope this helps.

                  p.s. sorry this is going on so long XD.
                  also, How come I keep getting rejected through newsgroup right now.

                  [quote]
                  ----- The following addresses had permanent fatal errors -----
                  <forums@adobe.com>
                  (reason: 550 5.1.1 <forums@inner-relay-2.adobe.com>... User unknown)
                  (expanded from: <forums@adobe.com>)

                  ----- Transcript of session follows -----
                  ... while talking to inner-relay-4.adobe.com.:
                  >>> >>> DATA
                  <<< 550 5.1.1 <forums@inner-relay-2.adobe.com>... User unknown
                  550 5.1.1 <forums@adobe.com>... User unknown
                  <<< 503 5.0.0 Need RCPT (recipient)
                  [/quote]


                  Thanks as always,
                  Slpixe
                  • 6. Re: attach movie in as3
                    Level 7
                    Slpixe,

                    > >< alright this is getting fairly confusing, so for now, I will
                    > try explaining some of it.

                    Sweet action.

                    > My fla file, has.. 2 mc's inside it, 1 called "page_mc" which
                    > I linkaged to be "page", and 1 called "btn_btn" or something
                    > but the button dosn't matter right now.

                    Okay, so we'll ignore the button. I'm assuming the following now:

                    a) your movie clip's Library name is page_mc
                    b) this symbol is in the Library, not the Stage
                    c) this symbol is set to export for ActionScript, with the linkage class
                    "page" (no quotes)

                    > I have the document class as "index" so that my index.as file
                    > in the same folder, is picked up.

                    I'm with ya.

                    > in that .as file. I have(want) all the code to be there (so there
                    > is no as code in the frames and stuff).

                    Yup.

                    > in the .as file.
                    >
                    > I have
                    >
                    > [code]
                    > package
                    > {
                    > import flash.display.MovieClip
                    >
                    > public class page extends MovieClip
                    > {

                    Okay, here's the first mistake. This file is named index.as, because
                    it's a class file. That means it provides the ActionScript that defines the
                    index class. For that reason, the name of this class must be index, not
                    page. (page is the automatically-written class that Flash creates on your
                    behalf, thanks to the linkage dialog box.)

                    So leave these lines as is, but change the word "page" there to "index":

                    public class index extends MovieClip
                    {

                    > public function page()
                    > {
                    > trace('created');

                    Here's the second issue. This function is called the constructor
                    function for this class. It must also be named index, because that's the
                    name of this class (and of the AS file that contains it). When you
                    instantiate any of the native classes -- e.g., var d:Date = new Date(); --
                    what you're doing is invoking this constructor function. For the Date
                    class, that function is public function Date(). In your case, it needs to
                    be:

                    public function index()
                    {

                    > var page1:page();
                    > addChild(page1);
                    > }

                    At this point, you're fine, provided you make the above two corrections.
                    Your index class (which is your FLA's document class) is now creating an
                    instance of the page class handled by your movie clip's linkage.

                    > As far as I read this(and want it)
                    > It imports the information required to do movieclip functions
                    > (etc addchild) and creats a function called "page" which inside
                    > will output the word "created" as a test

                    It won't, because this is the definition for the index class. What
                    you've done is create a class method named page() that never gets invoked
                    anywhere. (I'm repeating, at this point.) That constructor function needs
                    to be named index(), which means it will be invoked automatically when this
                    class is instantiated -- which happens as soon as the SWF loads, by virtue
                    of this class being the document class. The document class *is* the main
                    timeline, so as soon as the main timeline appears, the class performs its
                    duties.

                    > and I want it to put my "page_mc" which is linkaged to
                    > be called "page" on the stage, and have the page_mc
                    > called page1.

                    When you say it's called page1 ... it is, but maybe not the way you
                    think. You've declared a variable, page1, inside the index class's
                    constructor function. That means the page1 variable is scoped to that
                    function and only available to code inside that function.

                    If you want to be able to refer to page1 later in your class, you'll
                    have to declare page1 as a class property, like this:

                    package {

                    import flash.display.MovieClip;

                    public class index extends MovieClip {

                    private var page1:page;

                    public function index() {
                    trace("created");
                    page1 = new page();
                    addChild(page1);
                    }
                    }
                    }

                    Note that I added the "new" keyword, which is necessary to actually
                    instantiate the page class (I should have mentioned that earlier. The
                    point, though, is that page1 is declared as a variable scoped to the class
                    itself (outside of any function). That means you can write additional
                    functions later in this class and still refer to page1.

                    > after I will put in a loop so I can have multiple page_mc's
                    > with different names eg page1 page2 page3

                    That's fine. Just bear in mind, these variable names aren't instance
                    names in the sense of the name you type into the "instance name" field in
                    the Propery inspector. These are just variables that point to instances of
                    your (automatically written) page class.

                    > I will later be having text box's and pictures inside the
                    > page_mc's which I will also want to be able to call (hopefuly
                    > its nice and clean code like 'page1.text1.text = "workkkkkkk"'

                    Close. The way it works in AS3, because of the display list concept,
                    you may have to use a slightly different approach. I just talked about this
                    in another thread the other day. Assuming that movie clip -- the one in
                    your Library -- has the text field added manually (with the Text tool) and
                    given an instance name with the Property inspector, you'll have to do this:

                    var tf:TextField = TextField(page1.getChildByName("textFieldInstanceName"));

                    Then ...

                    tf.text = "workkkkkkk";

                    ... which really isn't so hard.

                    > p.s. sorry this is going on so long XD.

                    No worries. There's a really super, super long I replied to once ...
                    for days. It was incredible. This one doesn't come close.


                    David Stiller
                    Adobe Community Expert
                    Dev blog, http://www.quip.net/blog/
                    "Luck is the residue of good design."