# hitTestObject with walking man

**URL:** https://forum.kirupa.com/t/hittestobject-with-walking-man/331538
**Category:** flash
**Created:** [August 22, 2013, 8:20am UTC](https://forum.kirupa.com/t/hittestobject-with-walking-man/331538 "2013-08-22T08:20:45Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![HugeEuge](https://avatars.discourse-cdn.com/v4/letter/h/ee7513/32.png) [@HugeEuge](https://forum.kirupa.com/u/HugeEuge)
#### Post date: [August 22, 2013, 8:20am UTC](https://forum.kirupa.com/t/hittestobject-with-walking-man/331538/1 "2013-08-22T08:20:45Z")

</div>

Hi, I am quite new to hitTestObject…

So im making a game for an assignment, its in top down view and is not tile based. to stop players from walking where they are not allowed i’ve added about 5 or more walls to the stage as movie clips on each different frame. I would like to use hitTestObject to make ‘hero’ stop when he interacts with them.

Question: how can i do this? Do I need to make an array for the walls? what do i put in it to make the man keep walking but not be able to pass?

A side question: Can i put elements in an array without them being present on the stage? I would like to put all ‘walls’ inside one array so that I can keep using the same one but I am afraid it wont work… (I have about 200 walls all up)

Here is my code: (the ‘hero’ has 3 different states, one for walking right and left and one for walking up and down and one for standing still)

import flash.events.Event;

stop();

var town1Walls:Array=new Array(wall1,wall2,wall3,wall4,wall5,wall7)

//creates keyboard movement  
stage.addEventListener(KeyboardEvent.KEY\_DOWN , moveHero);

function moveHero( evt:KeyboardEvent ) : void  
{  
if ( evt:keyCode == Keyboard.UP )  
{  
hero.y -= 10;  
hero.scaleY=1;  
hero.gotoAndStop(‘walking2’);  
for(var u = 0; u\<town1Walls.length; u++)  
{  
if(hero.hitTestObject(town1Walls[u]))  
{  
hero.y+=10;  
}  
}  
}  
}

```
 if ( evt.keyCode == Keyboard.DOWN )
	{
		hero.y += 10;
		hero.scaleY=1;
		hero.gotoAndStop('walking2');
		for(var d = 0; d&lt;town1Walls.length; d++)
		{
			if(hero.hitTestObject(town1Walls[d]))
			{
				hero.y-=10;
			}
		}
			}
	else if ( evt.keyCode == Keyboard.LEFT )
	{
		hero.x -= 10;
		hero.scaleX=1;
		hero.gotoAndStop('walking');
		
		//checks when left key is pressed whether hero is at screen's edge
		if (currentLabel == "town2" && hero.x &lt; 0)
		{
			gotoAndStop("town1");
			hero.x = 800;
		}
		
		hero.x -= 10;
		
		if (currentLabel == "town3" && hero.x &lt; 0)
		{
			gotoAndStop("town2");
			hero.x = 800;
		}
		
	}
	else if ( evt.keyCode == Keyboard.RIGHT )
	{
		hero.x += 10;
		hero.scaleX=-1;
		hero.gotoAndStop('walking');
		
		if (currentLabel == "town1" && hero.x &gt; 800)
		{
			gotoAndStop("town2");
			hero.x = 0;
		}
		if (currentLabel == "town2" && hero.x &gt; 800)
		{
			gotoAndStop("town3");
			hero.x = 0;
		}
	}
	else 
{
	hero.gotoAndStop('still');
}

```

I am getting errors saying ‘undefined property, keyCode…’

Any help is appreciated… Thanks!
