
June 28th, 2005
04:06 AM
Neverside Newbie
Status: Offline!
moving a mc with arrow keys
i followed the tutorial at http://www.kirupa.com/developer/mx/movement_keys.htm but encountered one lil' problem. when you press and hold the key the bug jumps a little bit, then stalls, then keeps moving. Does anybody know why it does this and/or how to prevent it from doing so, so that the mc moves smoothly?

June 28th, 2005
04:24 AM
Neverside Newbie
Status: Offline!
agh, and another thing - not only does pressing an arrow key move the bug, but also everything else thats right there with it. If you put anything else in there with the bug, those move as well. I'm startin to think that his entire approach to the whole movement thing is a bit screwie. If anybody else has a way to move something with arrow keys and JUST that object, not EVERYTHING, then please offer it.
Also. is there a way to make the object snap back in one direction after you release the keys?

June 29th, 2005
12:59 AM
Neverside Newbie
Status: Offline!
The code in here may help you alone...
http://www.kirupa.com/developer/isometric/key_movement.htm
Using on(keyPress) is wonky, meaning that it reacts to the keyboard's inherent buffer (press a button down and it registers once, then the typomatic repeating starts after a second).
You're best to approach this using a key handler that detects whether a key is down, checks it's qualifications (ie up, down, left, right, fire) and does the appropriate action...
onClipEvent(keyDown){
if (Key.isDown(Key.UP)){
_x += 20;
_y -= 10;
}else if (Key.isDown(Key.DOWN)){
_x -= 20;
_y += 10;
}else if (Key.isDown(Key.LEFT)){
_x -= 20;
_y -= 10;
}else if (Key.isDown(Key.RIGHT)){
_x += 20;
_y += 10;
}
}
You can use flags for stuff like making the user press a key each time you want an event to happen (ie you don't want the user to have rapid fire all the time)...
Your second issue has to do with the referencing within the above.. you are probably referencing the entire stage with the x/y changes that are happening with a keypress...
If your bug has an instance name of bug.. then you would do _root.bug._x+=5, etc.
Hope this helps..
~Trip
___________________


June 29th, 2005
07:44 PM
Neverside Newbie
Status: Offline!
thanks. but i've noticed one more thing. When pressing multiple keys at once i'v noticed that the keys have priority over eachother depending on the order in which they're listed. For instance, because left is listed before right, if you hold down right and then hold left, while keeping right down, the left order is followed, not the right, however, when done left THEN right, the left follows through and the direction doesn't change. I believe this is because of the "else if" order. Is there some way to make it so that no key has priority over another, so that the key that you press most recently decides which direction the mc travels, not so that, for example, left overrules right.
AND...the mc still jumps when the key is pressed. I understand that this has to do with the keyboard, but in many flash games that i'v seen this does not occur

June 29th, 2005
07:52 PM
Neverside Newbie
Status: Offline!
http://www.flashplayer.com/games/raidenx.html
here... this is the type of movement that i'm looking for. If you notice, you can press multiple keys without disrupting each other. For example, if you press left or right in addition to up or down the ship moves horizontally. In addition to that, if you press both left and right at the same time the ship doesn't move at all, which is much more desirable than having it move in one specific direction. In addition, the ship can fire without interupting the path it moves in, while I tried to make my mc fire using the system explained earlier, but when it fired it stopped moving. Does anybody know how to make movement in the fashion of that game to which i linked?

July 6th, 2005
10:23 PM
Neverside Newbie
Status: Offline!
I figured out that you just take out the else and it works like the game above, but it still jumps when you press the key, unlike the smooth movement of the game above. Does anybody know how to make it not jumpey?

July 7th, 2005
12:18 AM
Neverside Newbie
Status: Offline!
You could use flags (leftpressed, rightpressed, uppressed, downpressed, etc), that would be checked within a function to move the object.. that would not fall prey to the jerkiness caused by the if statements above...
You would then run this function on an interval or in the enterFrame event if you don't know intervals...
onClipEvent(keyDown) {
thekey = Key.getCode();
if (thekey == Key.UP){
uppressed = 1;
}
}
onClipEvent(keyUp) {
thekey = Key.getCode();
if (thekey == Key.UP){
uppressed = 0;
}
}
onClipEvent(enterFrame) {
if (uppressed) {
//do something
}
}
___________________

Last edited by TripleFX, July 7th, 2005 12:19 AM (Edited 1 times)

July 7th, 2005
04:14 AM
Neverside Newbie
Status: Offline!
interesting...i think it may have actually just been the onClipEvent that was causing the jerkiness or something like that (i dont know why, im just looking for a scapegoat) Because in the tutorial http://www.actionscripts.org/tutorials/beginner/games_in_flash/index.shtml it follows nearly the same actionscripting as shown earlier, but makes it a function and leaves out the onClipEvent, and it's as smooth as butter.

July 7th, 2005
07:47 AM
Neverside Newbie
Status: Offline!
It's probably done through an interval, which can run the function every X milliseconds instead of using the timeline's main framerate..
___________________
