# hitTest and Collision problems

**URL:** <https://forum.kirupa.com/t/hittest-and-collision-problems/264098>\
**Category:** flash\
**Created:** [June 23, 2008, 6:24am UTC](https://forum.kirupa.com/t/hittest-and-collision-problems/264098 "2008-06-23T06:24:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremywilms](https://avatars.discourse-cdn.com/v4/letter/j/7cd45c/32.png) [@jeremywilms](https://forum.kirupa.com/u/jeremywilms)\
**Post date:** [June 23, 2008, 6:24am UTC](https://forum.kirupa.com/t/hittest-and-collision-problems/264098/1 "2008-06-23T06:24:52Z")

</div>

I’m having a lot of trouble with this hitTest method. Well… I’ll explain it to you. I made a wall, and if my player collides into that wall I want him to stop and not be able to move. So I can check for a hitTest before I move. If I go :

if(Player.hitTest(wall))  
return;

thus causing it to ignore all movment controls, the problem with that is that I can’t move, So I tried another method.

if(player.hitTest(wall)  
{  
player.\_x-=ThePlayerSpeed;  
}

The problem with that is that if I hold the up button my player will keep bouncing off the wall, and I want him to not even move going up, but still be able to move going down, right, and left.

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [June 23, 2008, 9:44am UTC](https://forum.kirupa.com/t/hittest-and-collision-problems/264098/2 "2008-06-23T09:44:38Z")

</div>

I’ve never worked with MX2004, but maybe you could have a set of four Boolean variables.

canMoveRight  
canMoveLeft  
canMoveUp  
canMoveDown

To start with, all of those would be ‘true’.

Then whenever you try to move your character, you’d check to see whether movement in  
a particular direction was allowed.

When they hit the wall, they lose the ability to move up

if(Player.hitTest(wall))  
{  
canMoveUp = false;  
}

but movement in the other directions is still allowed.

If you have other obstacles that take away their ability to move in a particular direction  
you could simply set the appropriate Boolean to false in your other hit test(s).

---

<div class="post-metadata">

**Author:** ![jeremywilms](https://avatars.discourse-cdn.com/v4/letter/j/7cd45c/32.png) [@jeremywilms](https://forum.kirupa.com/u/jeremywilms)\
**Post date:** [June 23, 2008, 1:27pm UTC](https://forum.kirupa.com/t/hittest-and-collision-problems/264098/3 "2008-06-23T13:27:29Z")

</div>

I was going to do that, but I thought it would have been sloppy coding. However I trust you have far more experiance in this language then I, so I’ll trying using an array of booleans. Thanks.

---

<div class="post-metadata">

**Author:** ![utsav](https://avatars.discourse-cdn.com/v4/letter/u/e19b73/32.png) [@utsav](https://forum.kirupa.com/u/utsav)\
**Post date:** [June 26, 2008, 4:12pm UTC](https://forum.kirupa.com/t/hittest-and-collision-problems/264098/4 "2008-06-26T16:12:47Z")

</div>

You could do something like this.

if( !\_root.wall.hitTest(\_x,\_y))  
{  
this.\_x+=1;  
}  
else  
{  
this.\_x = \_x;  
}

Hope this works 👼
