7 Replies Latest reply on Oct 8, 2008 6:13 PM by shintashi

    how does attachmovie work?

    shintashi Level 1
      this is what I understand...

      MovieClip.(idName,NewName,depth)
      for example:
      (object the Library movie clip is being loaded to).(idName,NewName,depth)

      I do not know what the rest of it really means, like the difference between idName and NewName, or where to enter some of these values, other than "linkage properties". Help?

        • 1. Re: how does attachmovie work?
          Rothrock Level 5
          Looks like you understand it better than you think! :)

          The only value you need to enter before hand in the linkage ID which goes in the linkage panel for a movieclip that is set to export for actionscript.

          That linkage id (spelled exactly the same) is how you tell attachmovie which item you want to pull from the library. It is usually a string, but it could also be a variable, but a variable that held a string.

          The new name is just that. If you are going to attach only one instance of something then the new name can just be a string, perhaps even the same string as the linkage ID. This is the same idea as giving a movieclip instance name at authortime, but this is how you do it when using attachMovie.

          Often you might be attaching several copies of something in a for loop. Then you might do something like:

          for(var i=0;i<10;i++){
          this.attachMovie("RedBall","redBall"+i,100+i);
          }

          That says pull out a copy of RedBall and make a copy of it on this timeline with the name of redBall0 and put it at depth 100. Then do it again for redBall1 at 101 and so on up to redBall9 at 109.

          • 2. how does attachmovie work?
            shintashi Level 1
            If I make an MC, for example, called RedBall, following your example, would I put anything in instance name? And does "Class" have anything to do with attachmovie or do I leave that line blank?

            Finally,
            in the string "for(var i=0;i<10;i++){"

            what is "for" ?

            I assume it means "For variable integer equals 0; integer less than 10; integer increase" or something like that... where would code like this go? in the frame of the Fla or in the MC being attached to, or in the code of a button? I presume any of the three would work.

            Sorry for so many questions.

            Edit: it worked in an MC using mouse down, so I suppose it would work in any. Still confused about some of the code's meaning though
            • 3. Re: how does attachmovie work?
              Rothrock Level 5
              Don't confuse the name you give something in the Library with the export name. They can be the same or different, but when it comes to attachMovie on the one in the linkage will work!

              If you right click on a MovieClip in the library you will see linkage in the context menu. You can select that and make sure to check Export for Actionscript. I'm not sure what instance name is. I don't see that in the options.

              Class does have something to do with attachMovie. You can write external class files and if you put the path to the as file in there you can attach the movie as an instance of that class. But you probably shouldn't do that until you've got the basics down.

              Well the for isn't a string, per se. "for" is one way of doing a repeating loop of actions. Look it up in the help files. The first argument says, "we are going to use this variable as a counter." Traditionally i is used for "index" but you can use anything you want. The next bit says, "Keep doing this until this condition isn't true anymore." And finally, in this case we are saying each time we do the code increase the variable by one.

              Then the part between the curly braces, which can be many lines or few depending, will happen over and over.

              In this case, if you had made your RedBall in the library you could put the code I gave on a frame in the main timeline. It would attach 10 copies of RedBall and given them the different names. Of course you won't be able to tell since they will all attach at x=0 and y=0. So here is a bit more.

              or(var i=0;i<10;i++){
              this.attachMovie("RedBall","redBall"+i,100+i,{_x:Math.random()*Stage.width,_y:Math.random( )*Stage.height});
              }

              That will attach 10 copies at random locations all over the stage.
              • 4. how does attachmovie work?
                shintashi Level 1
                ------------ The Question ----------------
                With the flame thrower/rain drop multiple attach movie clip effect, how do I get the instance name, since it keeps changing with "i"?
                [code]
                I tried something I found from 2001, namely
                turning "redBall"+i
                into [redBall[ i ]]
                but that didn't work (no surprise).


                quote:

                -----previous progress... ------
                So I've figured out from the above examples and more how to do stuff like spitting out a bunch of movie clips, and modified the above to choose basically how far from center and how wide the instances would be,
                example:
                //
                if (Key.isDown(Key.SPACE)){
                for(var i=0;i<30;i++){
                this.attachMovie("RedBall","redBall"+i,100+i,{_x:Math.random()*200 + 60,_y:Math.random()*40 + -20});
                }
                }
                //
                which is probably good for a flame thrower effect with a smaller y variable and for rain with smaller icons and a much wider math random.


                I also figured out multiple internal layer/tree/sprite collision detection (I don't know the name for it) and it looks like this:

                //
                if(_root.lance_mc.tip_mc.hitTest(_root.bolt_mc.pad_mc)) {
                bolt_mc._rotation +=ovelocity;
                }
                //

                which when rendering a light saber on/off effect looked like this:
                //
                if (Key.isDown(Key.SPACE)){
                this.attachMovie("Red_beam","beam_mc",1);
                beam_mc.filters = new Array(beam_filter);
                }

                if(Key.isDown(Key.DELETEKEY))
                // delete this movie clip
                { beam_mc.removeMovieClip(); }
                //


                with an array for glowing beam filter like this:

                //
                import flash.filters.GlowFilter;
                var color:Number = 0xff0000;
                var alpha:Number = .6;
                var blurX:Number = 45;
                var blurY:Number = 25;
                var strength:Number = 4;
                var quality:Number = 4;
                var inner:Boolean = false;
                var knockout:Boolean = false;
                var beam_filter:GlowFilter = new GlowFilter(color, alpha, blurX, blurY, strength, quality, inner, knockout);
                //

                - I noticed like instance name, filters have to be loaded in a special way.

                • 5. Re: how does attachmovie work?
                  Rothrock Level 5
                  Sorry there is a lot of code and discussion there but I'm not sure what the question is. If it is how do I reference a movie attached like that, well there are two ways.

                  Check the help files for attachMovie, you will notice there is a section for "Returns." This tells you that when you use attachMovie it returns a reference to the newly attached clip. You can assign that to a variable and reference it later.

                  var someName:MovieClip=this.attachMovie("linkage","newName",100);
                  trace(someName);
                  someName.gotoAndStop(5);
                  etc.

                  That is great if you need to do some stuff right after you attach it.

                  Otherwise you might need to just recreate the name using the array access operator. You were close with your example above.

                  var someName:MovieClip=this["redBall"+i]

                  Hope that answers your questions.
                  • 6. Re: how does attachmovie work?
                    Rothrock Level 5
                    Sorry there is a lot of code and discussion there but I'm not sure what the question is. If it is how do I reference a movie attached like that, well there are two ways.

                    Check the help files for attachMovie, you will notice there is a section for "Returns." This tells you that when you use attachMovie it returns a reference to the newly attached clip. You can assign that to a variable and reference it later.

                    var someName:MovieClip=this.attachMovie("linkage","newName",100);
                    trace(someName);
                    someName.gotoAndStop(5);
                    etc.

                    That is great if you need to do some stuff right after you attach it.

                    Otherwise you might need to just recreate the name using the array access operator. You were close with your example above.

                    var someName:MovieClip=this["redBall"+i]

                    Hope that answers your questions.
                    • 7. Re: how does attachmovie work?
                      shintashi Level 1
                      I'm not certain what I did wrong but it didn't work.


                      Code used in target MC:
                      --------------------------------------------------------------------------------------
                      onClipEvent(enterFrame){
                      var flames:MovieClip=RedBall_mc["redBall"+i]
                      var x:Number = 6;
                      if(flames.hitTest(this)){
                      _root.sap_mc-=x;
                      }
                      }
                      --------------------------------------------------------------------------------------
                      code used in flame thrower MC

                      onClipEvent(enterFrame) {
                      if (Key.isDown(Key.SPACE)){
                      for(var i=0;i<30;i++){
                      this.attachMovie("RedBall","redBall"+i,100+i,{_x:Math.random()*200 + 60,_y:Math.random()*40 + -20});
                      }
                      }
                      //spin code 2.0
                      if (Key.isDown(Key.SHIFT)){
                      this._rotation -=15;
                      }
                      if (Key.isDown(Key.CONTROL)){
                      this._rotation +=15;
                      }
                      }
                      --------------------------------------------------------------------------------------