# Character Programming

**URL:** <https://forum.kirupa.com/t/character-programming/184024>\
**Category:** programming\
**Created:** [April 1, 2006, 1:31am UTC](https://forum.kirupa.com/t/character-programming/184024 "2006-04-01T01:31:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![scarce](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@scarce](https://forum.kirupa.com/u/scarce)\
**Post date:** [April 1, 2006, 1:31am UTC](https://forum.kirupa.com/t/character-programming/184024/1 "2006-04-01T01:31:52Z")

</div>

Ok, this is a little issue I’ve had for awhile. I found a pretty decent way to code a walking sprite. My only issue is, when I hit a second arrow key, he starts to strife.

```auto
MovieClip.prototype.mover = function(moveX, moveY, facing) {
	this._x += moveX;
	this._y += moveY;
};
this.onEnterFrame = function() {
	if (Key.isDown(Key.LEFT)) {
		man.mover(-5, 0, 0);
	}
	 if (Key.isDown(Key.RIGHT)) {
		man.mover(+5, 0, 0);
	}
	 if (Key.isDown(Key.UP)) {
		man.mover(0, -5, 0);
	}
	 if (Key.isDown(Key.DOWN)) {
		man.mover(0, +5, 0);
	}
};

```

what can I do to this to make it go one direction at a time. i.e. Walking left, hit up then he walks up (while still holding left). Please help me, this has been an issue for months and i feel the answer is SO simple.

---

<div class="post-metadata">

**Author:** ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)\
**Post date:** [April 1, 2006, 1:40am UTC](https://forum.kirupa.com/t/character-programming/184024/2 "2006-04-01T01:40:41Z")

</div>

use else ifs;  
[http://www.n99creations.com/?pID=tutorials&col=Blue&tut=basics\_of\_actionscript\_for\_flash&p=4&l=Flash\_MX\_04](http://www.n99creations.com/?pID=tutorials&col=Blue&tut=basics_of_actionscript_for_flash&p=4&l=Flash_MX_04)

---

<div class="post-metadata">

**Author:** ![scarce](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@scarce](https://forum.kirupa.com/u/scarce)\
**Post date:** [April 1, 2006, 1:51am UTC](https://forum.kirupa.com/t/character-programming/184024/3 "2006-04-01T01:51:28Z")

</div>

> [@nathan99](#):
>
> use else ifs;  
> [http://www.n99creations.com/?pID=tutorials&col=Blue&tut=basics\_of\_actionscript\_for\_flash&p=4&l=Flash\_MX\_04](http://www.n99creations.com/?pID=tutorials&col=Blue&tut=basics_of_actionscript_for_flash&p=4&l=Flash_MX_04)

When I use else ifs, only a couple direction changes work because it’s following the order of the buttons pressed in conjunction to the arrangement of “Key.isDowns”. That closely fixes it, but not totally what I’m wanting, I tried working with the else/ifs prior to posting the first post, had no avail.

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 1, 2006, 1:55am UTC](https://forum.kirupa.com/t/character-programming/184024/4 "2006-04-01T01:55:53Z")

</div>

there is a simple way around it just by setting some boolean values, and when a key is down make sure another key isn’t down

---

<div class="post-metadata">

**Author:** ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)\
**Post date:** [April 1, 2006, 2:28am UTC](https://forum.kirupa.com/t/character-programming/184024/5 "2006-04-01T02:28:31Z")

</div>

yeah, that will be the easiest way

---

<div class="post-metadata">

**Author:** ![scarce](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@scarce](https://forum.kirupa.com/u/scarce)\
**Post date:** [April 2, 2006, 6:56am UTC](https://forum.kirupa.com/t/character-programming/184024/6 "2006-04-02T06:56:09Z")

</div>

Well, I tried Booleans, still no avail, think possibly could give me an example on how you think it would work?

---

<div class="post-metadata">

**Author:** ![scarce](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@scarce](https://forum.kirupa.com/u/scarce)\
**Post date:** [April 3, 2006, 5:45am UTC](https://forum.kirupa.com/t/character-programming/184024/7 "2006-04-03T05:45:14Z")

</div>

Can anyone help me out? Please?

---

<div class="post-metadata">

**Author:** ![scarce](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@scarce](https://forum.kirupa.com/u/scarce)\
**Post date:** [April 4, 2006, 2:48pm UTC](https://forum.kirupa.com/t/character-programming/184024/8 "2006-04-04T14:48:17Z")

</div>

Ok, this problem that I am having, I have searched the forums, tutorials, other forums, other utts and I can’t find anyhting on how to do this, can someone please help me? I know it’s a decently simple resolution and not to mention I provided the code which would make it even easier. Everything else I already have figured out, this is the only thing I’m asking. From my experience here over the years, people are usually helpful.

---

<div class="post-metadata">

**Author:** ![Joppe](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Joppe](https://forum.kirupa.com/u/Joppe)\
**Post date:** [April 4, 2006, 3:37pm UTC](https://forum.kirupa.com/t/character-programming/184024/9 "2006-04-04T15:37:04Z")

</div>

Oh why do i love flash actionscript editor \<3.

```auto

MovieClip.prototype.mover = function(moveX, moveY, facing) {
	this._x += moveX;
	this._y += moveY;
};
onEnterFrame = function () {
	if (Key.isDown(Key.LEFT)) {
		if (!left && !right && !up && !down) {
			man.mover(-5, 0, 0);
			trace('left');
			left = true;
		}
	} else {
		left = false;
	}
	if (Key.isDown(Key.RIGHT)) {
		if (!left && !right && !up && !down) {
			trace('rihy');
			right = true;
			man.mover(+5, 0, 0);
		}
	} else {
		right = false;
	}
	if (Key.isDown(Key.UP)) {
		if (!left && !right && !up && !down) {
			man.mover(0, -5, 0);
			trace('up');
			up = true;
		}
	} else {
		up = false;
	}
	if (Key.isDown(Key.DOWN)) {
			if(!left && !right && !up && !down){
		man.mover(0, +5, 0);
		trace('down');
			down = true;
			}
	}else{
	down = false;
	}
};

```

Try that 🙂

---

<div class="post-metadata">

**Author:** ![SeiferTim](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/seifertim/32/188_2.png) [@SeiferTim](https://forum.kirupa.com/u/SeiferTim)\
**Post date:** [April 4, 2006, 4:16pm UTC](https://forum.kirupa.com/t/character-programming/184024/10 "2006-04-04T16:16:24Z")

</div>

```auto
if (Key.isDown(Key.RIGHT)) {
        if (!left && !right && !up && !down) {
            trace('rihy');
            right = true;
            man.mover(+5, 0, 0);
        }

```

Man, I love making Rihy turns. 😉  
lol just messing with you Joppe. Nice code. 🙂

---

<div class="post-metadata">

**Author:** ![Joppe](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Joppe](https://forum.kirupa.com/u/Joppe)\
**Post date:** [April 4, 2006, 4:19pm UTC](https://forum.kirupa.com/t/character-programming/184024/11 "2006-04-04T16:19:34Z")

</div>

:lol: That what happends when typing with right hand:P

---

<div class="post-metadata">

**Author:** ![scarce](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@scarce](https://forum.kirupa.com/u/scarce)\
**Post date:** [April 4, 2006, 4:48pm UTC](https://forum.kirupa.com/t/character-programming/184024/12 "2006-04-04T16:48:44Z")

</div>

> [@joppe](#):
>
> Oh why do i love flash actionscript editor \<3.
> 
> ```auto
> 
> MovieClip.prototype.mover = function(moveX, moveY, facing) {
> this._x += moveX;
> this._y += moveY;
> };
> onEnterFrame = function () {
> if (Key.isDown(Key.LEFT)) {
> if (!left && !right && !up && !down) {
> man.mover(-5, 0, 0);
> trace('left');
> left = true;
> }
> } else {
> left = false;
> }
> if (Key.isDown(Key.RIGHT)) {
> if (!left && !right && !up && !down) {
> trace('rihy');
> right = true;
> man.mover(+5, 0, 0);
> }
> } else {
> right = false;
> }
> if (Key.isDown(Key.UP)) {
> if (!left && !right && !up && !down) {
> man.mover(0, -5, 0);
> trace('up');
> up = true;
> }
> } else {
> up = false;
> }
> if (Key.isDown(Key.DOWN)) {
> if(!left && !right && !up && !down){
> man.mover(0, +5, 0);
> trace('down');
> down = true;
> }
> }else{
> down = false;
> }
> };
> 
> ```
> 
> Try that 🙂

thanks very much for helping, ummm, when I puti n the code, it only moves one pixel then stops, it doesn’t continuously move. Plus, while I’m hold down a button, I can’t move in the direction of the other arrow key i hit. What might have caused that? I do understand where your going at with this code though, it just doesn’t seem to be going right

---

<div class="post-metadata">

**Author:** ![scarce](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@scarce](https://forum.kirupa.com/u/scarce)\
**Post date:** [April 4, 2006, 5:15pm UTC](https://forum.kirupa.com/t/character-programming/184024/13 "2006-04-04T17:15:38Z")

</div>

```auto
if (Key.isDown(Key.LEFT)) {
		if (!right && !up && !down) {
			man.mover(-5, 0, 0);
			left = true;
		}
	} else {
		left = false;
	}

```

I did this to it (removed the direction that I’m actually hitting, and now it moves smooth and constant, BUT I’m still not able to change the direction I’m walking whenever I’m holding down a direction. I.E: When I hold left, THEN hold up or down, it doesn’t switch to that direction.

---

<div class="post-metadata">

**Author:** ![scarce](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@scarce](https://forum.kirupa.com/u/scarce)\
**Post date:** [April 4, 2006, 5:52pm UTC](https://forum.kirupa.com/t/character-programming/184024/14 "2006-04-04T17:52:01Z")

</div>

SWEET! I figured it out…it’s probibly more then needed, but it works so whatever, lol.

Here’s the code:

```auto

MovieClip.prototype.mover = function(moveX, moveY, facing) {
	this._x += moveX;
	this._y += moveY;
};
onEnterFrame = function () {
	if (Key.isDown(Key.LEFT)) {
		if (!right && !up && !down) {
			man.mover(-3, 0, 0);
			left = true;
		} else if (Key.isDown(Key.UP)) {
			man.mover(-3, +3, 0);
		} else if (Key.isDown(Key.DOWN)) {
			man.mover(-3, -3, 0);
		}
		
	} else {
		left = false;
	}
	if (Key.isDown(Key.RIGHT)) {
		if (!left && !up && !down) {
			right = true;
			man.mover(+3, 0, 0);
		} else if (Key.isDown(Key.UP)) {
			man.mover(+3, +3, 0);
		} else if (Key.isDown(Key.DOWN)) {
			man.mover(+3, -3, 0);
		}
	} else {
		right = false;
	}
	if (Key.isDown(Key.UP)) {
		if (!left && !right && !down) {
			man.mover(0, -3, 0);
			up = true;
		} else if (Key.isDown(Key.RIGHT)) {
			man.mover(-3, -3, 0);
		} else if (Key.isDown(Key.LEFT)) {
			man.mover(+3, -3, 0);
		}
	} else {
		up = false;
	}
	if (Key.isDown(Key.DOWN)) {
		if (!left && !right && !up) {
			man.mover(0, +3, 0);
			down = true;
		} else if (Key.isDown(Key.RIGHT)) {
			man.mover(-3, +3, 0);
		} else if (Key.isDown(Key.LEFT)) {
			man.mover(+3, +3, 0);
		}
	} else {
		down = false;
	}
};

```

thanks guys for your long awaited help, you’ve restored my faith in these forums, lol.

---

<div class="post-metadata">

**Author:** ![SeiferTim](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/seifertim/32/188_2.png) [@SeiferTim](https://forum.kirupa.com/u/SeiferTim)\
**Post date:** [April 4, 2006, 8:41pm UTC](https://forum.kirupa.com/t/character-programming/184024/15 "2006-04-04T20:41:41Z")

</div>

> [@scarce](#):
>
> SWEET! I figured it out…it’s probibly more then needed, but it works so whatever, lol.
> 
> \<code removed to save space\>
> 
> thanks guys for your long awaited help, you’ve restored my faith in these forums, lol.

Great! Now write a tutorial about it. 😉

---

<div class="post-metadata">

**Author:** ![optixburn](https://avatars.discourse-cdn.com/v4/letter/o/82dd89/32.png) [@optixburn](https://forum.kirupa.com/u/optixburn)\
**Post date:** [April 4, 2006, 10:03pm UTC](https://forum.kirupa.com/t/character-programming/184024/16 "2006-04-04T22:03:05Z")

</div>

i know it might be a little late … but here is an alternative way to make your code work … so you don’t have to deal with so many if statements

```auto

// create a flag for the first key down
var firstKeyDown = false;
// create a key reference variable
var key_reference = undefined;
// create a key listner for the keys being pressed
var game_key_listner = new Object();
//
game_key_listner.onKeyDown = function() {
	// check to make sure the no other keys have been pressed
	if (!firstKeyDown) {
		// place the key that was pressed into the reference
		key_reference = Key.getCode();
		trace(key_reference+" was the first key pressed");
		// set the firstKeyDown to be true
		firstKeyDown = true;
	} else {
		// incase the user presses another button and holds it down while 
		// they release the first button we need to switch the
           // key_reference
		if (!Key.isDown(key_reference)) {
			key_reference = Key.getCode();
		}
		if (key_reference != Key.getCode()) {
			trace("Key "+key_reference+" is being pressed");
			trace("Will not use other Key "+Key.getCode());
		}
	}
};
game_key_listner.onKeyUp = function() {
	// check to make sure it was the same key that was pressed down
	if (key_reference == Key.getCode()) {
		// flag the firstKeyDown variable to false
		firstKeyDown = false;
		// and make sure the key_reference is taken out  
           // so you don't keep moving that direction
		key_reference = undefined;
	}
};
MovieClip.prototype.mover = function(moveX, moveY, facing) {
	this._x += moveX;
	this._y += moveY;
};
onEnterFrame = function () {
	switch (key_reference) {
	case 37 :
		// 37 == left
		man.mover(-3, 0, 0);
		break;
	case 38 :
		// 38 == up
		man.mover(0, -3, 0);
		break;
	case 39 :
		// 39 == right
		man.mover(+3, 0, 0);
		break;
	case 40 :
		// 40 == down
		man.mover(0, +3, 0);
		break;
	default :
		// trace("My key reference wasn't in the switch : "+key_reference);
		break;
	}
	// double check to see if the key's have been lifted up
	// NOTE: this can also be done like 
	// if(!Key.isDown(37) && !Key.isDown(38) && !Key.isDown(39) && !Key.isDown(40))
	// but for optimizing and speed it was done the following way
	//
	if(!Key.isDown(37)){
		if(!Key.isDown(38)){
			if(!Key.isDown(39)){
				if(!Key.isDown(40)){
					key_reference = undefined;
				}
			}
		}
	}
};
// add the game_key_listner to the Key listner list
Key.addListener(game_key_listner);
// don't have to have it stop on this frame but it's my coding habits
stop();

```

hope that helps some…

---

<div class="post-metadata">

**Author:** ![scarce](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@scarce](https://forum.kirupa.com/u/scarce)\
**Post date:** [April 5, 2006, 2:31am UTC](https://forum.kirupa.com/t/character-programming/184024/17 "2006-04-05T02:31:17Z")

</div>

> [@SeiferTim](#):
>
> Great! Now write a tutorial about it. 😉

Haha, you think I should? How about this, if a few people try it and like it, then I’ll make a tut about it, screenshots and all.

---

<div class="post-metadata">

**Author:** ![scarce](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@scarce](https://forum.kirupa.com/u/scarce)\
**Post date:** [April 5, 2006, 2:43am UTC](https://forum.kirupa.com/t/character-programming/184024/18 "2006-04-05T02:43:16Z")

</div>

> [@optixburn](#):
>
> i know it might be a little late … but here is an alternative way to make your code work … so you don’t have to deal with so many if statements
> 
> ```auto
> 
> // create a flag for the first key down
> var firstKeyDown = false;
> // create a key reference variable
> var key_reference = undefined;
> // create a key listner for the keys being pressed
> var game_key_listner = new Object();
> //
> game_key_listner.onKeyDown = function() {
> // check to make sure the no other keys have been pressed
> if (!firstKeyDown) {
> // place the key that was pressed into the reference
> key_reference = Key.getCode();
> trace(key_reference+" was the first key pressed");
> // set the firstKeyDown to be true
> firstKeyDown = true;
> } else {
> // incase the user presses another button and holds it down while 
> // they release the first button we need to switch the
> // key_reference
> if (!Key.isDown(key_reference)) {
> key_reference = Key.getCode();
> }
> if (key_reference != Key.getCode()) {
> trace("Key "+key_reference+" is being pressed");
> trace("Will not use other Key "+Key.getCode());
> }
> }
> };
> game_key_listner.onKeyUp = function() {
> // check to make sure it was the same key that was pressed down
> if (key_reference == Key.getCode()) {
> // flag the firstKeyDown variable to false
> firstKeyDown = false;
> // and make sure the key_reference is taken out  
> // so you don't keep moving that direction
> key_reference = undefined;
> }
> };
> MovieClip.prototype.mover = function(moveX, moveY, facing) {
> this._x += moveX;
> this._y += moveY;
> };
> onEnterFrame = function () {
> switch (key_reference) {
> case 37 :
> // 37 == left
> man.mover(-3, 0, 0);
> break;
> case 38 :
> // 38 == up
> man.mover(0, -3, 0);
> break;
> case 39 :
> // 39 == right
> man.mover(+3, 0, 0);
> break;
> case 40 :
> // 40 == down
> man.mover(0, +3, 0);
> break;
> default :
> // trace("My key reference wasn't in the switch : "+key_reference);
> break;
> }
> // double check to see if the key's have been lifted up
> // NOTE: this can also be done like 
> // if(!Key.isDown(37) && !Key.isDown(38) && !Key.isDown(39) && !Key.isDown(40))
> // but for optimizing and speed it was done the following way
> //
> if(!Key.isDown(37)){
> if(!Key.isDown(38)){
> if(!Key.isDown(39)){
> if(!Key.isDown(40)){
> key_reference = undefined;
> }
> }
> }
> }
> };
> // add the game_key_listner to the Key listner list
> Key.addListener(game_key_listner);
> // don't have to have it stop on this frame but it's my coding habits
> stop();
> 
> ```
> 
> hope that helps some…

I went ahead and tried yours since you were kind enough to do that, but it still didn’t do what I was wanting. When I’m holding down a left, and hit up or down, it doesn’t change direction, it stays going left. Same with every other direction.

---

<div class="post-metadata">

**Author:** ![optixburn](https://avatars.discourse-cdn.com/v4/letter/o/82dd89/32.png) [@optixburn](https://forum.kirupa.com/u/optixburn)\
**Post date:** [April 5, 2006, 1:59pm UTC](https://forum.kirupa.com/t/character-programming/184024/19 "2006-04-05T13:59:01Z")

</div>

> [@scarce](#):
>
> I went ahead and tried yours since you were kind enough to do that, but it still didn’t do what I was wanting. When I’m holding down a left, and hit up or down, it doesn’t change direction, it stays going left. Same with every other direction.

haha … i misread the thread …

sorry about that, when i get some time later i will revise it and post a different version

---

<div class="post-metadata">

**Author:** ![scarce](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@scarce](https://forum.kirupa.com/u/scarce)\
**Post date:** [April 5, 2006, 2:40pm UTC](https://forum.kirupa.com/t/character-programming/184024/20 "2006-04-05T14:40:15Z")

</div>

> [@optixburn](#):
>
> haha … i misread the thread …
> 
> sorry about that, when i get some time later i will revise it and post a different version

lol it’s quite alright man, I appreciate it it alot 🙂
