• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Perfact hitTest

Explorer ,
Jun 27, 2012 Jun 27, 2012

Copy link to clipboard

Copied

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)||plyr2.hitTestObject(r6)||plyr2.hitTestObject(r7)||plyr2.hitTestObject(r8)||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)||plyr2.hitTestObject(r6)||plyr2.hitTestObject(r7)||plyr2.hitTestObject(r8)||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)||plyr2.hitTestObject(r6)||plyr2.hitTestObject(r7)||plyr2.hitTestObject(r8)||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)||plyr2.hitTestObject(r6)||plyr2.hitTestObject(r7)||plyr2.hitTestObject(r8)||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.

TOPICS
ActionScript

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2012 Jun 28, 2012

Copy link to clipboard

Copied

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

plyr.x+=2

should be

plyr.x-=2

etc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2012 Jun 28, 2012

Copy link to clipboard

Copied

Yes...everything is working opposite!!

i have also made the changes you suggest me...since it is not working!!! Dunno.. my logic is wrong or not.!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 29, 2012 Jun 29, 2012

Copy link to clipboard

Copied

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/Array.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/flash/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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 08, 2012 Jul 08, 2012

Copy link to clipboard

Copied

LATEST

thanks ockley..

the code you suggest me is working...

thank you so much

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines