# hitTest - Left and Right collision detection

**URL:** <https://forum.kirupa.com/t/hittest-left-and-right-collision-detection/241519>\
**Category:** flash\
**Created:** [October 15, 2007, 7:56am UTC](https://forum.kirupa.com/t/hittest-left-and-right-collision-detection/241519 "2007-10-15T07:56:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fm47](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@fm47](https://forum.kirupa.com/u/fm47)\
**Post date:** [October 15, 2007, 7:56am UTC](https://forum.kirupa.com/t/hittest-left-and-right-collision-detection/241519/1 "2007-10-15T07:56:20Z")

</div>

So on the Stage I’ve two walls, one named wall\_1 and the other wall\_2. Along the two walls is a ball.

```auto

onClipEvent (load) {
 var ballMove:Object = new Object();
 var speed:Number = 10;
}
onClipEvent (enterFrame) {
 ballMove.onKeyDown = function() {
  switch (Key.getCode()) {
  case Key.LEFT :
   _x -= speed;
   break;
  case Key.RIGHT :
   _x += speed;
   break;
  case Key.UP :
   _y -= speed;
   break;
  case Key.DOWN :
   _y += speed;
  }
 };
 Key.addListener(ballMove);
 trace(this._x)
 if (this.hitTest(_root.wall_1) && this._x < _root.wall_1._x) {
  this._x = _root.wall_1._x - this._width;
 }
 if (this.hitTest(_root.wall_1) && this._x >_root.wall_1._x+_root.wall_1._width) {
  this._x = _root.wall_1._x + _root.wall_1._width+this._width;
 }
}

```

So what I was hoping for was that when the ball collides with the wall on the left side, it would stop, and if it collides with the right side, it’s stop, but so far I have only been able to do detection on one side to make the ball stop. I can’t accomplish getting both to detect.

What is wrong with my code?

Also, I noticed this method of object movement is rather sluggish, what other methods might one suggest?
