# Problems with keyboard input, moving animation

**URL:** https://forum.kirupa.com/t/problems-with-keyboard-input-moving-animation/331548
**Category:** Uncategorized
**Created:** [August 26, 2013, 9:29am UTC](https://forum.kirupa.com/t/problems-with-keyboard-input-moving-animation/331548 "2013-08-26T09:29:12Z")
**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 26, 2013, 9:29am UTC](https://forum.kirupa.com/t/problems-with-keyboard-input-moving-animation/331548/1 "2013-08-26T09:29:12Z")

</div>

Hi, so I cannot for the life of me figure out what im doing wrong… its quite a long code, but it looks the same to me. for some reason I can move ‘hero’ upwards, but i cannot move left, right or down, even though the coding looks legit (to me). Here it is:

stage.focus = this;  
stage.addEventListener(KeyboardEvent.KEY\_DOWN , moveHero);

function moveHero( evt:KeyboardEvent ) : void  
{  
if ( evt.keyCode == Keyboard.UP )  
{  
hero.y -= 10;//moves up  
hero.scaleY=1;  
hero.gotoAndStop(‘walking2’);//plays animation walking up

```
		if(currentLabel == "town6")
		{
			gotoAndStop("wharf1");//changing screens
			hero.y = 600;//where he spawns
			for(var u = 0; u&lt;townWalls6.length; u++)//loop

			{
			if(hero.hitTestObject(townWalls6[u]))//cannot pass through walls

			{
				hero.y+=10;//stops walking
			}

			}

		}
	
		if(currentLabel == "town5")
		{
				gotoAndStop("town6");
				hero.y = 600;
				for(var v = 0; v&lt;townWalls5.length; v++)

			{
				if(hero.hitTestObject(townWalls5[v]))

				{
					hero.y+=10;
				}

			}
		
		}
	else if ( evt.keyCode == Keyboard.DOWN )
	{
		hero.y += 10;
		hero.scaleY=1;
		hero.gotoAndStop('walking2');
			
		if (currentLabel == "wharf1" && hero.y &lt; 0)
		{
			gotoAndStop("town6");
			hero.y = 600;
			
			for(var w = 0; w&lt;townWalls7.length; w++)
		{
			if(hero.hitTestObject(townWalls7[w]))
			{
				hero.y+=10;
			}
		}
	}
	
			
		if (currentLabel == "town6" && hero.y &lt; 0)
		{
			gotoAndStop("town5");
			hero.y = 600;
			
			for(var xx = 0; xx&lt;townWalls6.length; xx++)
			{
			if(hero.hitTestObject(townWalls6[xx]))
			{
				hero.y+=10;
			}
			}
		}

```

else if ( evt.keyCode == Keyboard.LEFT )  
{  
hero.x -= 10;  
hero.scaleX=1;  
hero.gotoAndStop(‘walking’);

```
		if (currentLabel == "town2" && hero.x &lt; 0)
		{
			gotoAndStop("town1");
			hero.x = 800;
			
		for(var e = 0; e&lt;townWalls2.width; e++)
		{
			if(hero.hitTestObject(townWalls2[e]))
			{
				hero.x-=10;
			}
		}
		}
		
		if (currentLabel == "town3" && hero.x &lt; 0)
		{
			gotoAndStop("town2");
			hero.x = 800;
			
			for(var f = 0; f&lt;townWalls3.width; f++)
			{
			if(hero.hitTestObject(townWalls3[f]))
			{
				hero.x-=10;
			}
			}
		}

```

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;
			
			for(var yyy = 0; yyy&lt;townWalls.width; yyy++)
			{
			if(hero.hitTestObject(townWalls[yyy]))
			{
				hero.x-=10;
			}
			}
		}
		if (currentLabel == "town2" && hero.x &gt; 800)
		{
			gotoAndStop("town3");
			hero.x = 0;
			
			for(var zzz = 0; zzz&lt;townWalls2.width; zzz++)
			{
			if(hero.hitTestObject(townWalls2[zzz]))
			{
				hero.x-=10;
			}
			}
		}

```

The code is muuuuuch longer than this, but this is just an example.
