# MovieClip hitTest

**URL:** <https://forum.kirupa.com/t/movieclip-hittest/33930>\
**Category:** flash\
**Created:** [October 25, 2003, 10:13am UTC](https://forum.kirupa.com/t/movieclip-hittest/33930 "2003-10-25T10:13:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luster](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@luster](https://forum.kirupa.com/u/luster)\
**Post date:** [October 25, 2003, 10:13am UTC](https://forum.kirupa.com/t/movieclip-hittest/33930/1 "2003-10-25T10:13:52Z")

</div>

Hi,

I’ve created a moving MC (responding on Up,Left,…).  
But I want I to NOT move if it’s underneath something like a wall.  
I’ve named the wall “muur”. Any suggestions to stop the MC moving if it’s right underneath/above/ left to/right to the wall, so you’ll have to go around the wall. I tried some hitTest stuff, but it didn’t work. Though I think it’s related with hitTest.  
Anyone?

Luster,

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 25, 2003, 10:20am UTC](https://forum.kirupa.com/t/movieclip-hittest/33930/2 "2003-10-25T10:20:58Z")

</div>

post the .fla

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 25, 2003, 10:32am UTC](https://forum.kirupa.com/t/movieclip-hittest/33930/3 "2003-10-25T10:32:22Z")

</div>

Here it is.

PS= Only the red one is a wall, the other is a block, it’s supposed to move when pushed against.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 26, 2003, 6:29am UTC](https://forum.kirupa.com/t/movieclip-hittest/33930/4 "2003-10-26T06:29:18Z")

</div>

name the red square as wall and then paste this script on the dude mc:

[AS]onClipEvent (enterFrame) {  
if (Key.isDown(Key.RIGHT)) {  
this.\_x +=20;  
this.gotoAndStop( “rechts” );  
if(this.hitTest(\_parent.wall)){  
this.\_x=\_root.wall.\_x-18;  
}

}

```
 if (Key.isDown(Key.LEFT)) {
	 this._x -=20;
	 this.gotoAndStop( "links");
	 if(this.hitTest(_parent.wall)){
		 this._x=_root.wall._x+18;
	 }

```

}  
if (Key.isDown(Key.UP)) {  
this.\_y -=50;  
this.gotoAndStop( “boven” );  
if(this.hitTest(\_parent.wall)){  
this.\_y=\_root.wall.\_y+50;  
}

}  
if (Key.isDown(Key.DOWN)) {  
this.\_y +=50;  
this.gotoAndStop( “onder” );  
if(this.hitTest(\_parent.wall)){  
this.\_y=\_root.wall.\_y-50;  
}

}

}  
[/AS]

hope that helps:rambo: :cap:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 26, 2003, 7:37am UTC](https://forum.kirupa.com/t/movieclip-hittest/33930/5 "2003-10-26T07:37:24Z")

</div>

Thanks, but it doesn’t sem to work perfectly. If I’m standing under it and I press right it goes to the left and stuff.  
Please help.

PS = Why do you use the value “18” and not “20”?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 26, 2003, 7:49am UTC](https://forum.kirupa.com/t/movieclip-hittest/33930/6 "2003-10-26T07:49:48Z")

</div>

Try this:

[AS]onClipEvent (enterFrame) {  
if (Key.isDown(Key.RIGHT)) {  
this.\_x +=20;  
this.gotoAndStop( “rechts” );  
if(this.hitTest(\_parent.wall)){  
this.\_x=\_root.wall.\_x-22;//\<–this is the distance from the wall when its touched pressing the right arrow key

```
            }
            
            
            
    }
    
    
    if (Key.isDown(Key.LEFT)) {
            this._x -=20;
            this.gotoAndStop( "links");
            if(this.hitTest(_parent.wall)){
                    this._x=_root.wall._x+22;
            }
            
            
    }
    if (Key.isDown(Key.UP)) {
            this._y -=50;
            this.gotoAndStop( "boven" );
            if(this.hitTest(_parent.wall)){
                    this._y=_root.wall._y+53;
            }
            
    }
    if (Key.isDown(Key.DOWN)) {
            this._y +=50;
            this.gotoAndStop( "onder" );
            if(this.hitTest(_parent.wall)){
                    this._y=_root.wall._y-53;
            }
            
    }

```

}[/AS]

did it worked?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 26, 2003, 8:02am UTC](https://forum.kirupa.com/t/movieclip-hittest/33930/7 "2003-10-26T08:02:42Z")

</div>

No, I’m sorry it didn’t work, but I think I know why.  
In the original code the pinguin’s position was set. You could move around and get back on EXACTLY the same position where you started. (Because it uses “20” and “50” values). If you change one of that values (like “50” into “53”) it will run badly, because all the blocks have been set on locations like ( X=40; Y=350) or (X=120;Y=200). The position of all objects on the Y axis must be a multiple of 50 and on the X axis a multiple of 20.  
But how do you stop the pinguin without changing its position a single pixel?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 26, 2003, 9:54am UTC](https://forum.kirupa.com/t/movieclip-hittest/33930/8 "2003-10-26T09:54:05Z")

</div>

I think I have a possibility, but to use it I need to know how you set in an if " if (conditon1) but NOT (condition2)"  
Anyone?  
Please?
