PDA

View Full Version : Using keyboard for movement - advanced script? Please help...



Agent Subterfug
October 21st, 2004, 10:00 PM
Hey all,

http://www.kirupa.com/developer/actionscript/xymove.htm

Kirupa's tutorial here was something I ran across while trying to learn how to move an MC using the keyboard. However, I'm looking to do a motion that is a bit more complicated than that - and I'm somewhat stumped. (Yup, Im new to actionscripting)

I'd like to be able to move the MC 100 pixels (and that part I've figured out), but I need the motion to be smooth - with inertia. I searched and searched, and this is a link I found that looked useful in demonstrating the effect:

http://experiments.coldstorageonline.com/

Click the 4th example - 'Inertia, Single plane of movement'

Now, instead of having the _xmouse determine the position, I want to hit the 'forward/backward' keys on the keyboard, and make it hop smoothly forward or backward a 100 pixels. Im just confused where to add what, and I am continuing to work on it.

This is being programmed in Flash MX 2004.

If anyone could offer any help, Id really appreciate it.

Thanks,

Haley

Agent Subterfug
October 22nd, 2004, 02:59 PM
Please, anyone...?

SmurfMX
October 22nd, 2004, 04:56 PM
I made something up for it it's attached I hope it helps.

The vx and vy variables are your x and y velocities. All you do is divide them by a number to 'ease' them when no directional buttons are being pressed.

Agent Subterfug
October 22nd, 2004, 08:07 PM
Thank you for responding, Smurf!

Unfortunately, I think that file might have been created in an earlier version of flash, because it doesnt seem to open over here.

Would you be willing to post the code?

Thank you, once again. I appreciate your help.

Zelwyn
October 22nd, 2004, 08:33 PM
Okay, here's how you should code it.


//on the mc that's moving
onClipEvent (load) {
endX = _x;
//if you want it to move along the Y axis too, uncomment this (endY = _y;)
speed = 5; //this works well
}
onClipEvent (enterFrame) {
_x += (endX-_x)/speed;
//same deal as before (_y += (endY-_y)/speed;)
}

//on another mc, make code that detects button presses
onClipEvent (enterFrame) {
/*same deal as before for the up and down (if (Key.isDown(Key.DOWN)) {
_root./*your mc*/.endY += 100;
}
if (Key.isDown(Key.UP)) {
_root./*your mc*/.endY -= 100;
})
if (Key.isDown(Key.LEFT)) {
_root./*your mc*/.endX -= 100;
}
if (Key.isDown(Key.RIGHT)) {
_root./*your mc*/.endX += 100;
}
}

Agent Subterfug
October 23rd, 2004, 07:42 PM
Thank you all so much for your help! I optimized it as best as I was able and Im posting the file here for anyone who wishes to use it in the future.

Again, thank you so much. : )

Haley