Skip navigation
sachin_mak
Currently Being Moderated

Perfact hitTest

Jun 27, 2012 9:20 PM

hello every one

 

var rightArow:Boolean;

var leftArow:Boolean;

var upArow:Boolean;

var downArow:Boolean;

var speed:int = 10;

 

 

stage.addEventListener(KeyboardEvent.KEY_DOWN, keypress);

stage.addEventListener(KeyboardEvent.KEY_UP, keyrelease);

stage.addEventListener(Event.ENTER_FRAME, everyframe);

 

 

 

function keypress(event:KeyboardEvent):void {

          if (event.keyCode == Keyboard.RIGHT) {

                    rightArow = true;

          }

          if (event.keyCode == Keyboard.LEFT) {

                    leftArow = true;

          }

          if (event.keyCode == Keyboard.UP) {

                    upArow = true;

          }

          if (event.keyCode == Keyboard.DOWN) {

                    downArow = true;

          }

}

function keyrelease(event:KeyboardEvent):void {

          if (event.keyCode == Keyboard.RIGHT) {

                    rightArow = false;

          }

          if (event.keyCode == Keyboard.LEFT) {

                    leftArow = false;

          }

          if (event.keyCode == Keyboard.UP) {

                    upArow = false;

          }

          if (event.keyCode == Keyboard.DOWN) {

                    downArow = false;

          }

}

 

 

function everyframe(event:Event):void {

          if (rightArow) {

                    if(plyr2.hitTestObject(r1)||plyr2.hitTestObject(r2)||plyr2. hitTestObject(r3)||plyr2.hitTestObject(r4)||plyr2.hitTestObject(r5)||p lyr2.hitTestObject(r6)||plyr2.hitTestObject(r7)||plyr2.hitTestObject(r 8)||plyr2.hitTestObject(r9))

                    {

                              plyr2.x -= 2;

                    }

                    else

                    {

                              plyr2.x += 2;

                    }

          }

          if (leftArow) {

                    if(plyr2.hitTestObject(r1)||plyr2.hitTestObject(r2)||plyr2. hitTestObject(r3)||plyr2.hitTestObject(r4)||plyr2.hitTestObject(r5)||p lyr2.hitTestObject(r6)||plyr2.hitTestObject(r7)||plyr2.hitTestObject(r 8)||plyr2.hitTestObject(r9))

                    {

                              plyr2.x += 2;

                    }

                    else

                    {

                              plyr2.x -= 2;

                    }

          }

          if (upArow) {

                    if(plyr2.hitTestObject(r1)||plyr2.hitTestObject(r2)||plyr2. hitTestObject(r3)||plyr2.hitTestObject(r4)||plyr2.hitTestObject(r5)||p lyr2.hitTestObject(r6)||plyr2.hitTestObject(r7)||plyr2.hitTestObject(r 8)||plyr2.hitTestObject(r9))

                    {

                              plyr2.y += 2;

                    }

                    else

                    {

                              plyr2.y -= 2;

                    }

          }

          if (downArow) {

                    if(plyr2.hitTestObject(r1)||plyr2.hitTestObject(r2)||plyr2. hitTestObject(r3)||plyr2.hitTestObject(r4)||plyr2.hitTestObject(r5)||p lyr2.hitTestObject(r6)||plyr2.hitTestObject(r7)||plyr2.hitTestObject(r 8)||plyr2.hitTestObject(r9))

                    {

                              plyr2.y -= 2;

                    }

                    else

                    {

                              plyr2.y += 2;

                    }

          }

}

--------------------

i have used the above code to check hitTest and stop the object where the collision is detect. well... this is working..but not in proper way....

 

when i press right arrow my object moves in left side....and whenever it goes out of road_mc(i.e. r1, r2...r9) it works fine...that is moves right side...

 

 

i need solution for this.. plz help me.

 
Replies
  • kglad
    72,340 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 28, 2012 6:00 AM   in reply to sachin_mak

    it looks everything is the opposite of what you want.  ie

     

    plyr.x+=2

     

    should be

     

    plyr.x-=2

     

    etc

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 29, 2012 5:35 AM   in reply to sachin_mak

    Hi. I have tried to update your code. It is definately some Wild-West code, and I have to emphasize, that this answer is not the proper way to do it :-)

     

    The main problem with your solution is, that you are using hittest on a clip that you want to be on. This hittest gives false, when you are out of the tile, and you then have a lot of trouble getting back in again.

     

    The code I made was this:

     

    import flash.display.DisplayObjectContainer;
    import flash.display.MovieClip;
    import flash.geom.Rectangle;
    
    
    var rightArow:Boolean;
    var leftArow:Boolean;
    var upArow:Boolean;
    var downArow:Boolean;
    var speed:int = 5;
    var road:Array = ["r1","r2","r3","r4","r5","r6","r7","r8","r9"];
    
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keypress);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyrelease);
    
    
    function keypress(event:KeyboardEvent):void
    {
              if (event.keyCode == Keyboard.RIGHT) rightArow = true;
              if (event.keyCode == Keyboard.LEFT) leftArow = true;
              if (event.keyCode == Keyboard.UP) upArow = true;
              if (event.keyCode == Keyboard.DOWN) downArow = true;
    
    
              updatePos();
    }
    function keyrelease(event:KeyboardEvent):void
    {
              if (event.keyCode == Keyboard.RIGHT) rightArow = false;
              if (event.keyCode == Keyboard.LEFT) leftArow = false;
              if (event.keyCode == Keyboard.UP) upArow = false;
              if (event.keyCode == Keyboard.DOWN) downArow = false;
    }
    
    
    function isInside(element:*, index:int, arr:Array):Boolean
    {
              var plyrPos:Rectangle = plyr2.getBounds(stage);
              var base:DisplayObjectContainer = stage.getChildAt(0) as MovieClip;
              var tmpRect:Rectangle = MovieClip(getChildByName(element)).getBounds(stage);
              if(tmpRect.containsRect(plyrPos))
              {
                        return true;
              }
              return false;
    }
    function updatePos():void
    {
              if (rightArow)
              {
                        plyr2.x +=  speed;
                        if(!road.some(isInside))
                                  while(!road.some(isInside))
                                            plyr2.x -= 1;
              }
              if (leftArow)
              {
                        plyr2.x -=  speed;
                        if(!road.some(isInside))
                                  while(!road.some(isInside))
                                            plyr2.x += 1;
              }
              if (upArow)
              {
                        plyr2.y -=  speed;
                        if(!road.some(isInside))
                                  while(!road.some(isInside))
                                            plyr2.y += 1;
              }
              if (downArow)
              {
                        plyr2.y +=  speed;
                        if(!road.some(isInside))
                                  while(!road.some(isInside))
                                            plyr2.y -= 1;
              }
    }
    

     

    I placed all your movie clips inside an array, so I could use the Array.some() method. It uses a function on the array, and keeps iterating until it reaches a true in return. You can read about it at http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Arr ay.html#some()

     

    In the function isInside() I make two rectangles, and test for their bounds. The Rectangle class has a ot of useful methods like Rectangle.containsRect() that test to see if a rectangle object is fully contained inside another object. Read about it at http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fla sh/geom/Rectangle.html

     

    The last thing I can mention is that I made a cosmetic while loop, that subtracts 1 pixel from your player, so the rectangle fit perfectly to the edge of the squares. Because of different sizes, you may end up with a player that can't go all the way to the edge.

     

    Please note, that this method calculate on rectangles, so if you have very odd-shaped elements you may look for other solutions.

     

    I have made a working solution that you can see at:

     

    http://eksempler.hjaelpmignu.dk/wwwhelp/flash/road.html

     

    You can get the fla-file at:

     

    http://eksempler.hjaelpmignu.dk/wwwhelp/flash/road.fla

     

    Good luck

    /ockley

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points