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.
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.
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.
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.
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.
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();
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.