23 Replies Latest reply on Jul 22, 2009 10:33 AM by kglad

    [as1, as2] Complex hitTest

    marcelozepgames Level 1
      Hello.
      I´d like to know if there´s a way to do a shape hitTest.
      Not as the normal hitTest would do, but shape colliding another shape, not the rectangle
      of a movieclip.
      It can be in a movieclip.
      If there´s this way, please make it so that our processors would thank you .

       

      Thank you 

       

      marcelozepgames
        • 1. Re: [as1, as2] Complex hitTest
          shintashi Level 1

          have you tried a series of tiny particle MCs along the border like connect the dots?

           

          you can set them in place then set the alpha to 0, and run a 'for loop' to detect collision.

          1 person found this helpful
          • 2. Re: [as1, as2] Complex hitTest
            marcelozepgames Level 1

            shintashi:

             

            Well, I´ve tried this, but thank you anyway )).

            Do you know what happens if you use this method in more than one movieclip?
            Well, probably it will make your processor overloaded )) and your movie´s overflow will get impaired

            Thank you for your reply shintashi .

            I´ve heard something about using the new as2 commands.

            marcelozepgames

            • 3. Re: [as1, as2] Complex hitTest
              kglad Adobe Community Professional & MVP

              check g skinner's shape-based hittest.

              1 person found this helpful
              • 4. Re: [as1, as2] Complex hitTest
                marcelozepgames Level 1

                Hello.

                I´ve hear about this class, seems to be very usefull.

                I just want to know what is needed to use gskiner´s class.

                It seems that if I want to use the code I need to put some

                credentials referring to its author.

                What is needed for me to use this class actually?

                Remember, this code has copyright.

                 

                marcelozepgames

                • 5. Re: [as1, as2] Complex hitTest
                  kglad Adobe Community Professional & MVP

                  he's made the code publically available:  http://www.gskinner.com/blog/archives/2005/10/source_code_sha.html

                   

                  but i use my own shape-based test that's faster and more flexible than gskinner's so i don't remember if there are caveats to its use.  download the files and read.

                  1 person found this helpful
                  • 6. Re: [as1, as2] Complex hitTest
                    marcelozepgames Level 1

                    Can you tell me the way you do the shape-based hitTest?
                    Please.

                    And then, can I use it on my code without entries of you?

                    Or maybe, I can learn from your code and do my own.

                    Thank you very much.

                     

                    marcelozepgames

                    • 7. Re: [as1, as2] Complex hitTest
                      dalcde Level 1

                      There is. But instead of object to object detection, it is object to point detection:

                      if(this.hitTest(_root.enemy._x, _root.enemy._x, true)){

                           ...

                      }

                       

                      At the code above, if the object( the player, for example) hits the registeration point of the enemy ( which is the enemy's _x and _y ), the code will be executed. The last true/false is for deciding if it detects shape or the rectangle ( shapeflag ). It can do what you want, but it is often not so convenient. ( but it's super useful in platform games )

                      • 8. Re: [as1, as2] Complex hitTest
                        marcelozepgames Level 1

                        Thank you dalcde.

                        But this I think is a simple hitTest.

                        I want one wich I can use for shape-based hitTest collision.

                        Thank you )

                        • 9. Re: [as1, as2] Complex hitTest
                          marcelozepgames Level 1

                          Please kglad, can you show your shape-based hitTest?

                          If yes, it works with rotating shapes on movieclips?

                          Well, I have two movieclips with complex shapes on each one.

                          They are rotated by a function that is called by a setInterval method, every 10 milliseconds.

                          I wanted to see what, this function (the one whose check collision detection), returns whether true or false, when the complex shapes collide, while they are rotating.

                          Thank you.

                          • 10. Re: [as1, as2] Complex hitTest
                            kglad Adobe Community Professional & MVP

                            here's a working demo:  www.kglad.com click snippets, hit test.

                             

                            here's the code but i'm not supporting it/explaining it:

                             

                            MovieClip.prototype.hitF = function(mc) {
                                if (this.hitTest(mc)) {
                                    if (!importedVar) {
                                        import flash.display.BitmapData;
                                        import flash.geom.*;
                                        importedVar = 1;
                                    }
                                    this.currentTrans = this.transform;
                                    this.currentMat = this.currentTrans.concatenatedMatrix;
                                    if (!this["bmp"] || this.previousFrame != this._currentframe || this.currentMat.a != this.previousMat.a || this.currentMat.b != this.previousMat.b || this.currentMat.c != this.previousMat.c || this.currentMat.d != this.previousMat.d) {
                                        this.createBmpF("bmp");
                                        this.previousMat = this.currentMat;
                                        this.previousFrame = this._currentframe;
                                    }
                                    mc.currentTrans = mc.transform;
                                    mc.currentMat = mc.currentTrans.concatenatedMatrix;
                                    if (!mc["bmp"] || mc.previousframe != mc._currentframe || mc.currentMat.a != mc.previousMat.a || mc.currentMat.b != mc.previousMat.b || mc.currentMat.c != mc.previousMat.c || mc.currentMat.d != mc.previousMat.d) {
                                        mc.createBmpF("bmp");
                                        mc.previousMat = mc.currentMat;
                                        mc.previousframe = mc._currentframe;
                                    }
                                    this.bo = this.getBounds(_root);
                                    mc.bo = mc.getBounds(_root);
                                    this["bmpHolder"]._x = this.bo.xMin;
                                    this["bmpHolder"]._y = this.bo.yMin;
                                    mc["bmpHolder"]._x = mc.bo.xMin;
                                    mc["bmpHolder"]._y = mc.bo.yMin;
                                    this.pt = new Point(this["bmpHolder"]._x, this["bmpHolder"]._y);
                                    mc.pt = new Point(mc["bmpHolder"]._x, mc["bmpHolder"]._y);
                                    if (this["bmp"].hitTest(this.pt, 250, mc["bmp"], mc.pt, 250)) {
                                        return true;
                                    } else {
                                        return false;
                                    }
                                } else {
                                    return false;
                                }
                            };
                            MovieClip.prototype.createBmpF = function(bmp) {
                                this.tempMat = this.currentMat.clone();
                                this.pbo = this.currentTrans.pixelBounds;
                                this.tempMat.tx -= this.pbo.x;
                                this.tempMat.ty -= this.pbo.y;
                                this[bmp] = new BitmapData(this._width, this._height, true, 0x44333333);
                                this[bmp].draw(this, this.tempMat);
                                this["bmpHolder"] = _root.createEmptyMovieClip("bmpHolder_"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
                                this["bmpHolder"].attachBitmap(this[bmp], 0);
                                this["bmpHolder"]._visible = 0;
                            };

                            • 11. Re: [as1, as2] Complex hitTest
                              marcelozepgames Level 1

                              Does it work, with rotating movieclips?

                              I did a function that moves these two movieclips, these two movieclips has complex shapes on it.

                              I tried to do a simple BitmapData.hitTest() but it didn´t work.

                              Ah, can I use this code without mention credits of you?

                              Thank you )

                              • 12. Re: [as1, as2] Complex hitTest
                                kglad Adobe Community Professional & MVP

                                you can use the code.

                                 

                                it works with rotating movieclips, movieclips whose shape(s) change when their timeline changes frame, movieclips that are nested etc.  check my website referenced above.

                                • 13. Re: [as1, as2] Complex hitTest
                                  marcelozepgames Level 1

                                  Thank you very much .

                                   

                                  I´ll see, now.

                                  • 14. Re: [as1, as2] Complex hitTest
                                    marcelozepgames Level 1

                                    kglad, can I use the code without mentions of you, I mean without copyright?

                                    Thank you.

                                    • 15. Re: [as1, as2] Complex hitTest
                                      kglad Adobe Community Professional & MVP

                                      my code's not copyrighted.

                                      • 16. Re: [as1, as2] Complex hitTest
                                        marcelozepgames Level 1

                                        Can I use your code in a licensed game, without mention credits to you?

                                        • 17. Re: [as1, as2] Complex hitTest
                                          kglad Adobe Community Professional & MVP

                                          the game is going to be licensed and people with be charged money for the game?

                                          • 18. Re: [as1, as2] Complex hitTest
                                            marcelozepgames Level 1

                                            Yes, it´s a licensed game. I am developing it.
                                            I´ll receive money for this game.

                                            So, can I use your code, without mention credits of you?
                                            Thank you ).
                                            • 20. Re: [as1, as2] Complex hitTest
                                              marcelozepgames Level 1

                                              Ok, thank you.

                                              Sorry, but, so why did you posted the code?

                                              And, why, did you said that your code has no copyright.

                                              Thus, doesn´t make the code free to use?

                                              Thank you.

                                              • 21. Re: [as1, as2] Complex hitTest
                                                kglad Adobe Community Professional & MVP

                                                if you want to steal my code it's there for you to steal.

                                                 

                                                the difference in my attitude between the time when i posted my code and now is because i didn't know you were intending to use this for a commercial product when i posted my code.

                                                 

                                                my feeling is i should be paid if my work is being used in a for-profit application.

                                                • 22. Re: [as1, as2] Complex hitTest
                                                  marcelozepgames Level 1

                                                  Oh, sorry.

                                                  That is not my intention, I´ll not use it, I´m sorry.

                                                  • 23. Re: [as1, as2] Complex hitTest
                                                    kglad Adobe Community Professional & MVP

                                                    thank you.