15 Replies Latest reply on Aug 15, 2006 9:30 AM by Newsgroup_User

    attachMovie not working!!

    VaporAction
      I can't get the attachMovie function to work and I set the Linkage on the symbol so it's not that...
      here's my code...
        • 1. Re: attachMovie not working!!
          Level 7
          VaporAction,

          > I can't get the attachMovie function to work and I
          > set the Linkage on the symbol so it's not that...

          It's important you realize the distinction between functions and
          methods. attachMovie() is a method -- specifically, the
          MovieClip.attachMovie() method. Functions, on the other hand, don't belong
          to a particular class. You can think of functions as "free floaters."

          So, attachMovie() only works when you invoke it on an instance of the
          MovieClip class, commonly called a movie clip.

          > Element1_btn.onRelease = function () {

          If Element1_btn is a movie clip symbol, then you have a MovieClip
          instance. If Element1_btn is a button symbol, then you have a Button
          instance. As it happens, both classes, MovieClip and Button, feature an
          onRelease event, so this would work in either cases.

          > this.attachMovie ("Element", "ElementFe_mc", this.getNextHighestDepth);

          Here, the global "this" property refers to the timeline or object it's
          in. If this is a movie clip symbol, then "this" points to a MovieClip
          instance -- and bingo, the MovieClip class features an attachMovie() method.
          If this is a button symbol, then "this" refers to a Button instance -- and
          the Button class does not feature an attachMovie() method.


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


          • 2. attachMovie not working!!
            VaporAction Level 1
            quote:

            Here, the global "this" property refers to the timeline or object it's
            in. If this is a movie clip symbol, then "this" points to a MovieClip
            instance -- and bingo, the MovieClip class features an attachMovie() method.
            If this is a button symbol, then "this" refers to a Button instance -- and
            the Button class does not feature an attachMovie() method.


            So how would I attachMovie with a button?
            • 3. Re: attachMovie not working!!
              kglad Adobe Community Professional & MVP
              to what timeline do you want to attach "Element"? if you want to attach it to the timeline that contains your button or _root use:

              • 4. Re: attachMovie not working!!
                Level 7
                VaporAction,

                > Does this mean that you can't use the attachMovie method
                > if the triggering object is a button and not a movieclip?

                The answer to this question (and more) is the following overriding
                principle: object functionality depends entirely on the class that defines
                it. If you're dealing with a button symbol, you're dealing with a Button
                instance (that is, an object instantiated from the Button class). If you're
                dealing with a movie clip, you're dealing with a MovieClip instance.
                Dealing with sound? ... instance of the Sound class. You get the idea. ;)

                Look up "Button class" in the ActionScript 2.0 Language Reference to see
                if buttons feature an attachMovie() method ( ... they don't).

                > I know I can make the button into a movieclip, I'm just curious...

                If you do, you'll suddenly have access to all the properties, methods,
                and events dictated by the MovieClip class, which include attachMovie(). :)


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


                • 5. Re: attachMovie not working!!
                  VaporAction Level 1
                  well, I got rid of the whole attachMovie thing and just did it on the timeline...anyway, I have a new problem now...
                  I have a button called "Pour Water" that when pressed performs a gotoAndPlay, where an animation plays depicting water pouring from a glass...the only problem is that you can keep clicking on the button and have the glass eternally pouring water...
                  I tried disabling the button once it reached the last frame of the animation by removing that piece of code in my Actions layer at the last frame...but it doesn't seem to pay any attention to the new keyframe in the Actions layer that omits the button's functionality...(sorry, if that was confusing...)
                  Here's the code for the button and then on the last frame of the animation, I simply omit it....
                  • 6. Re: attachMovie not working!!
                    kglad Adobe Community Professional & MVP
                    try:

                    • 7. Re: attachMovie not working!!
                      Level 7
                      > try:
                      >
                      > PourWater_btn.onRelease = function () {
                      > if(this._currentframe==1){ // assuming you only want to trigger your goto
                      > statement when _currentframe is 1.
                      > gotoAndPlay ("PourWater_Fe");
                      > }
                      > }

                      That's a good one. Or delete (or set to null) the onRelease handler of
                      PourWater_btn down the line. There are usually a number of ways to solve
                      your problems in ActionScript. If you "get" the idea of classes and
                      objects, you'll find you have a wealth of information right at your finger
                      tips. The _currentframe property kglad mentioned, for example ... it's a
                      property of the MovieClip class. Check out these classes, VaporAction! :)


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


                      • 8. Re: attachMovie not working!!
                        Level 7
                        Even though you have now taken a different approach, in addition to David
                        and Kglad's responses, this:

                        > this.attachMovie ("Element", "ElementFe_mc", this.getNextHighestDepth);

                        should of course be:

                        this.attachMovie ("Element", "ElementFe_mc", this.getNextHighestDepth());

                        --
                        -------------------------------
                        Remove '_spamkiller_' to mail
                        -------------------------------


                        • 9. Re: attachMovie not working!!
                          Level 7
                          >> this.attachMovie ("Element", "ElementFe_mc", this.getNextHighestDepth);
                          >
                          > should of course be:
                          >
                          > this.attachMovie ("Element", "ElementFe_mc", this.getNextHighestDepth());

                          But it's not working, I tell you!! ;)

                          The devil is in the details, and you caught one of many, Peter.


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


                          • 10. Re: attachMovie not working!!
                            VaporAction Level 1
                            Hey kglad, Peter and David,
                            Just wanted to say thanks for the help...I ended up using (PourWater_btn.enable = false) on the appropriate frame in order to disable the button...
                            That worked and guess what? I have a new problem and this one is kind've embarrassing because I thought even with my extremely small knowledge of ActionScript that I would be capable of this without help...anyway here's my new problem...
                            I want ElementFe_mc to fall to a table top when released...didn't think it would be hard to do, well, here's what I've tried...
                            • 11. sRe: attachMovie not working!!
                              Level 7
                              VaporAction,

                              > I want ElementFe_mc to fall to a table top when released...
                              > didn't think it would be hard to do, well, here's what I've
                              > tried...

                              Okay.

                              > ElementFe_mc.onRelease = function(): Void {
                              > this.stopDrag();
                              > if (ElementFe_mc._y < 412.4) {
                              > this._y++
                              > }
                              > }

                              Well, here's what you've got. The Button.onRelease or
                              MovieCip.onRelease event is dispatched when the user releases the mouse
                              after having clicked it. This only occurs once, unless the user clicks and
                              releases again.

                              So ... when the user releases, the following occurs: ElementVe_mc stops
                              dragging and its MovieClip._y property is incremented by one pixel (if it
                              happens to fall beneath 412.4 pixels at the time of the event).

                              I susped you want to increase the _y position as often as necessary,
                              rather than just once. To accomplish this, you'll have to continue
                              incrementing its value over time. You might, for example, set up a
                              MovieClip.onEnterFrame event handlers inside this onRelease handler ...

                              ElementFe_mc.onRelease = function(): Void {
                              this.stopDrag();
                              if (this._y < 412.4) {
                              this.onEnterFrame = function() {
                              this._y++;
                              }
                              }
                              }

                              Of course, you'll want a second if statement in there to determine when to
                              delete the onEnterFrame function. Make sense?


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


                              • 12. Re: sRe: attachMovie not working!!
                                VaporAction Level 1
                                works like a charm!
                                Now, I need to make it so you can drop an element into a glass cylinder, however, once it's in the cylinder I don't want the user to be able to drag it out the sides of the cylinder...
                                I tried creating an invisible object with which the element performed a hitTest and if true then stopDrag but the problem with that is then you could no longer drag it (sorta like it was stuck to the invisible object)...

                                • 13. Re: sRe: attachMovie not working!!
                                  Level 7
                                  VaporAction,

                                  > works like a charm!

                                  :)

                                  > Now, I need to make it so you can drop an element
                                  > into a glass cylinder, however, once it's in the cylinder
                                  > I don't want the user to be able to drag it out the
                                  > sides of the cylinder...

                                  You already have the answer, if you think carefully about what you
                                  already know. ;)

                                  > I tried creating an invisible object with which the
                                  > element performed a hitTest and if true then stopDrag
                                  > but the problem with that is then you could no longer
                                  > drag it (sorta like it was stuck to the invisible object)...

                                  Well, sure ... you don't want to *stop* dragging the object, you merely
                                  want to constain it. If the MovieClip.startDrag() method isn't powerful
                                  enough, you can do without it and code your dragging in a completely
                                  different way.

                                  Rather than startDrag(), use your MovieClip.onEnterFrame handlers to
                                  simply set the movie clip's _x and _y properties to the mouses's position --
                                  that amounts to the same thing. Only that way, you have as much control as
                                  you like over when to adjust the _x and _y properties, and when not to (for
                                  example, if the object is up against the sides of the glass.

                                  Check out this recent article on my blog. It doesn't answer your
                                  question directly, but if provides insight you can use to handle what you're
                                  after.

                                  http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle

                                  By the way, for something like this ...

                                  > ElementFe_mc.onEnterFrame = function(): Void {
                                  > var StopHitTest:Boolean = this.hitTest(Stop_mc);
                                  > if (StopHitTest) {
                                  > this.stopDrag();
                                  > }
                                  > }

                                  ... you may as well combine a few lines, here.

                                  ElementFe_mc.onEnterFrame = function(): Void {
                                  if (this.hitTest(Stop_mc)) {
                                  this.stopDrag();
                                  }
                                  }

                                  That amounts to the same thing, yet you don't have to declare the
                                  variable.


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


                                  • 14. Re: sRe: attachMovie not working!!
                                    VaporAction Level 1
                                    Impressive blog and resume!
                                    Unfortunately, after reading your article 3 times I still cannot see how this would help me control the movement of the Element_mc because it's movement parameters would vary depending on whether it was inside or outside the glass cylinder...
                                    I've also noticed some weird thing with using hitTest to stop the Element_mc. It seems like once you've dragged through the hitTest it permanently breaks it....
                                    • 15. Re: sRe: attachMovie not working!!
                                      Level 7
                                      VaporAction,

                                      > Unfortunately, after reading your article 3 times I
                                      > still cannot see how this would help me control
                                      > the movement of the Element_mc because it's
                                      > movement parameters would vary depending on
                                      > whether it was inside or outside the glass cylinder...

                                      That's where logic steps in, bro. You have the tools at your disposal,
                                      so now you've got to use them creatively. ;)

                                      You know at this point that you don't need the MovieClip.startDrag()
                                      method in order to perform dragging. You can, instead, set the movie clip
                                      programmatically to the mouse's position inside a loop of some kind --
                                      onEnterFrame, onMouseMove, setInterval(), etc.

                                      That's how your dropping animation occurs, and you said that one worked
                                      for you.

                                      At this point, you might use hitTest() to determine if you're in or
                                      outside the glass. If inside, continue setting the clip's position to the
                                      mouse's; if not, go back to the dropping routine (incrementing the _y
                                      property until it hits bottom). Very likely, this will be a series of if()
                                      statements and more than one looping mechanism (one for the dragging, one
                                      for the falling).

                                      > I've also noticed some weird thing with using hitTest
                                      > to stop the Element_mc. It seems like once you've
                                      > dragged through the hitTest it permanently breaks it....

                                      It does what you tell it to. ;) In a previous post, you invoked
                                      MovieClip.stopDrag() on the clip when MovieClip.hitTest() returned true.
                                      You did nothing to *start* dragging again.


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