8 Replies Latest reply: May 28, 2012 6:02 AM by kglad RSS

    How to set boundaries??

    sachin_mak Community Member

      hello every one,

       

      I want a man_mc to walk just on  the road. But i dunno how to set boudaries so that mine man_mc doesn't go outside of road.

       

      Plz help.

        • 1. Re: How to set boundaries??
          kglad CommunityMVP

          if your road is close to rectangular or can be approximated by several rectangular shapes you can use the hitTest to determine when your character goes outside the rectangular shapes.

           

          if you want more accuracy or can't use rectangular shapes, you can use the bitmapData's hittest.

          • 2. Re: How to set boundaries??
            sachin_mak Community Member

            thank you.

             

            but actually i dunno how to check hit of one object with another object.!

             

            my road is in rectangular shape. my man_mc is on road_mc. now how to add hit test and where to put it?

            • 3. Re: How to set boundaries??
              kglad CommunityMVP

              use:

               

              this.onEnterFrame=function(){

              if(!man_mc.hitTest(road_mc)){

              // do whatever.  your man is completely off the road

              }

              }

              • 4. Re: How to set boundaries??
                sachin_mak Community Member

                right now i have just add an alert box as shown below to check whether the script is working or not.

                 

                this.onEnterFrame=function(){

                if(!man_mc.hitTest(road_mc)){

                Alert.show("Hit!!!");

                }

                };

                 

                 

                But it is not working...i have added this script on instance of man_mc.

                • 5. Re: How to set boundaries??
                  prodigalmaster Community Member

                  try:

                   

                  trace("Does not hit!!!");

                   

                  instead, it will work.

                   

                  Yes, the ! means NOT

                  so:

                   

                  IF NOT HITTEST = TRUE THEN

                  TRACE "Not hitting"

                  END IF

                  • 6. Re: How to set boundaries??
                    kglad CommunityMVP

                    use:

                     

                    trace(man_mc);

                    trace(road_mc);

                    this.onEnterFrame=function(){

                    if(!man_mc.hitTest(road_mc)){

                    trace("off");

                    }

                    }

                     

                    and move your man_mc completely off of road_mc.  copy and paste the first few lines of the text in your output panel.

                    • 7. Re: How to set boundaries??
                      sachin_mak Community Member

                      thanks.

                       

                      i tried and edit your code.and i got success... but it is on keypress event... i want it on mouse click event!!!

                      onClipEvent (enterFrame) {

                                if (Key.isDown(Key.RIGHT)) {

                                this._x = this._x+4;

                                }

                                else if (Key.isDown(Key.LEFT)) {

                                this._x = this._x-4;

                                }

                                if (Key.isDown(Key.UP)) {

                                          if (hitTest(_level0.l1)==true) {

                                          _y=_y+2;

                                }

                                else {

                                this._y = this._y-4;

                                }

                                }

                                else if (Key.isDown(Key.DOWN)) {

                      //          this._y = this._y+4;

                                          if (hitTest(_level0.l2)==true) {

                                          _y=_y-2;

                                }

                       

                       

                                else {

                                this._y = this._y+4;

                                }

                                }

                       

                      this is my code i am recently i am using...can you plz tell me how to move a man on mouse click and check the hit test on mouse click

                      • 8. Re: How to set boundaries??
                        kglad CommunityMVP

                        1.  you should mark helpful/correct answers.

                         

                        2.  what do you want to click to trigger a hittest?

                         

                        3.  what do you want to click to move your man_mc?

                         

                        4.  in what direction do you want to move man_mc when whatever you answer in 3 is clicked?