Character Programming

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.

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.

use else ifs;
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.

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

yeah, that will be the easiest way

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

Can anyone help me out? Please?

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.

Oh why do i love flash actionscript editor <3.


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 :slight_smile:

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. :wink:
lol just messing with you Joppe. Nice code. :slight_smile:

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

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

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.

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

Here’s the code:


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.

Great! Now write a tutorial about it. :wink:

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


// 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…

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.

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

lol it’s quite alright man, I appreciate it it alot :slight_smile: