6 Replies Latest reply on May 4, 2009 6:18 AM by kglad

    hitTest Two Movieclips, Character and Barrier.

    prodigalmaster Level 2
      Hi, here is a typical hitTest problem, I have a character, he stays in the center whilst the surroundings move instead (scrolling). There is a block and when you walk into it, I need it so you cannot walk through it or get stuck in it. Here is my code below but, its very buggy, this is obviously been done before and may be simple, once I learn it I wont need to ask again. Iv tried other things but.. they dont work. But please help. ;D
      ActionScript Code:
      collisionmap.onEnterFrame = function() {     if (this.hitTest(perso)) {         _root.speed = -3;     } else {         _root.speed = 3;     } };

       

      collisionmap = block
      perso = charcter
      speed = speed of character.

       

      Thanks for any help you give.
        • 1. Re: hitTest Two Movieclips, Character and Barrier.
          kglad Adobe Community Professional & MVP

          if(this.hitTest(perso)){

          _root.speed = -_root.speed;

          // and you may want to update perso's position here

          }

          • 2. Re: hitTest Two Movieclips, Character and Barrier.
            prodigalmaster Level 2

            Thank you so much, It works. Even though not perfectly, as it *bounces* slightly off the wall but, thats better than nothing... it was a quick good reply also unlike them other forums, thanks. Although im trying to properly understand it.. speed == 3 - 3 ? meh, thanks anyway. If I really need to I would ask how to get it to walk smoothly into the wall instead of juddering but, its fine by me anyway. cyu.

            • 3. Re: hitTest Two Movieclips, Character and Barrier.
              prodigalmaster Level 2

              Ok, Thanks for your help but.. as Stated in the reply above, I need it so its not as buggy. If you try, you can get stuck inside the block...

               

              Code:

              collisionmap.onEnterFrame = function() {
              if (this.hitTest(perso)) {
                _root.speed = -_root.speed;
                updateAfterEvent();
              } else {
                _root.speed = 3;
              }
              };

               

              I don't think the updateAfterEvent(); would do anything useful.. but i put it there anyway... maybe I could store the x, y co-ordinates of the block before moving, and then once hit(Test) move the block back to them co-ordinates. But i would find it difficuly as my charcter is locked in the center.. blah blah.. il try figuring it out... if you/anyone has some kinda idea the, please share it. thanks.

              • 4. Re: hitTest Two Movieclips, Character and Barrier.
                kglad Adobe Community Professional & MVP

                1.  you shouldn't use that else branch.

                 

                2.  updateAfterEvent() is unneeded.

                 

                3.  you should update the perso's position in that hittest so he doesn't get embedded in the wall.

                • 5. Re: hitTest Two Movieclips, Character and Barrier.
                  prodigalmaster Level 2

                  Hi, thanks for your help, the thing is, my characer stays in the same place, so i should update the blocks position?

                   

                   

                  .. do i need to add a listener then? hmm, im not totally good with them, how would I update the position? Im not that good with actionscript

                  var collisionListener:Object = new Object();

                   

                  collisionListener.onLoad = function() { ??

                  _root.collisionmap._x = ????

                  _root.collisionmap._y = ????

                  }; ??

                   

                  Im getting the character walking into the block and he just reverses, its better than my first code though.

                  thanks for the help.

                  • 6. Re: hitTest Two Movieclips, Character and Barrier.
                    kglad Adobe Community Professional & MVP

                    yes, update the postion of everything else because your perso is fixed.

                     

                    don't you have a loop or a keylistener that updates everything else's position?

                     

                    and what do you want to have happen if not reverse everything when perso hits something?