9 Replies Latest reply on Dec 13, 2006 4:26 PM by sk8morestudyless

    Collision detection help

    tangytom
      Hi,

      On my latest Flash project, i would like 2 movieclip objects to able to collide that would cause the project to jump back to frame 1, any advice on how i could make this happen?

      Thank You
        • 1. Re: Collision detection help
          Level 7
          tangytom,

          > On my latest Flash project, i would like 2 movieclip
          > objects to able to collide that would cause the project
          > to jump back to frame 1, any advice on how i could
          > make this happen?

          This goal can be broken down into two parts, both of which may be
          achieved by taking a look at the MovieClip class. In ActionScript, classes
          define objects, and just about everything in Flash is an object. Since
          you're dealing with movie clips (even the main timeline is a movie clip),
          you'll want to check out the MovieClip class, which defines the properties
          (characteristics), methods (things it can do) and events (things it can
          react to) of every movie clip in your SWF.

          To test for collisions (overlapping), see the MovieClip.hitTest()
          method.

          To send a timeline to a frame, see the MovieClip.gotoAndStop() method.

          See if that gets you started. :) Come back if you have questions.


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


          • 2. Re: Collision detection help
            tangytom Level 1
            Thank You i shall try this.
            • 3. Re: Collision detection help
              tangytom Level 1
              Sorry im still a little confused, i have one movieclip instance, and i would like the project to jump to the first frame when it is hit by another movieclip instance, how do i make the project jump to a frame?

              Thank You.
              • 4. Re: Collision detection help
                Level 7
                tangytom,

                > Sorry im still a little confused,

                's okay. :)

                > i have one movieclip instance, and i would like the
                > project to jump to the first frame when it is hit by
                > another movieclip instance,

                I'm with ya.

                > how do i make the project jump to a frame?

                If by "the project" you mean the main timeline, you can use the
                MovieClip.gotoAndStop() method, or MovieClip.gotoAndPlay(). These methods
                work on any instance of the MovieClip class, including the main timeline.
                So, for example, if you had a movie clip with the instance name yowza, and
                you wanted yowza to go to frame 15 on its timeline and stop, you'd use this
                ...

                yowza.gotoAndStop(15);

                If, instead, you wanted the main timeline to go to frame 15 and stop,
                you'd use this ...

                this.gotoAndStop(15);
                // assuming the code appears inside
                // the main timeline, where "this" would
                // be the relevant reference


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


                • 5. Re: Collision detection help
                  sk8morestudyless
                  or you can try this
                  onClipEvent(enterFrame){
                  if(this.hitTest(_root.???)){
                  _root.gotoAndStop(1)
                  }
                  }

                  this code should go in one of the mcs and the??? should be replaced with the instance name of what you want your mc to colide with
                  • 6. Re: Collision detection help
                    Level 7
                    sk8morestudyless,

                    > or you can try this
                    > onClipEvent(enterFrame){
                    > if(this.hitTest(_root.???)){
                    > _root.gotoAndStop(1)
                    > }
                    > }

                    Whoa, whoa! :) We have no idea how often this collision check needs to
                    occur. It *might* need to occur once every frame, but what if the collision
                    is the result of one movie being dragged over the other? onMouseMove would
                    be better in that case, in order to save processor cycles when nothing is
                    being dragged.

                    > this code should go in one of the mcs and the???
                    > should be replaced with the instance name of what
                    > you want your mc to colide with

                    While on() and onClipEvent() are still supported functions, the current
                    recommended best practice is to code it like this ...

                    MovieClipInstanceHere.onEnterFrame = function():Void {
                    // event handler code here
                    }


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


                    • 7. Re: Collision detection help
                      sk8morestudyless Level 1
                      ok i guess im not to sure what hes useing this for

                      but i dont realy see a differance in the code
                      all i see is that yours goes in the frame
                      so if you can help me out on what you mean by how its the best way to script it i would appreciate it
                      • 8. Re: Collision detection help
                        Level 7
                        sk8morestudyless,

                        > but i dont realy see a differance in the code
                        > all i see is that yours goes in the frame

                        That's a big difference!

                        > so if you can help me out on what you mean by how its
                        > the best way to script it i would appreciate it

                        To me, the biggest reasons to avoid on() and onClipEvent() are to a)
                        better organize all your code into one place and b) to have to opportunity
                        to add/change event handling on the fly.

                        http://www.quip.net/blog/2006/flash/museum-pieces-on-and-onclipevent


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


                        • 9. Re: Collision detection help
                          sk8morestudyless Level 1
                          thank ill try to start scripting that way
                          :)