Forum Topic: Help with movement script please

(162 views • 12 replies)

This topic is 1 page long.

<< < > >>
Questioning

4Cuties

Reply To Post Reply & Quote

Posted at: 1/29/06 02:17 PM

4Cuties LIGHT LEVEL 07

Sign-Up: 12/06/05

Posts: 12

I'm trying to get a MC to move when the arrow keys are pressed. I can get it to move at a constant speed with no problem, but i want it to slowly accelerate when the keys are pressed and slowly decelerate when the keys are released. Deceleration is my biggest problem. This is what i have so far:

onClipEvent (load) {
this.speed = 0;
this.accel = 0.5;
this.maxspeed = 5;
this.minspeed = 0.5;l
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT) || Key.isDown(Key.LEFT) || Key.isDown(Key.UP) || Key.isDown(Key.DOWN)) {
if (Key.isDown(Key.RIGHT)) {
this.speed += this.accel;
this._x = _x+this.speed;
if (this.speed>=this.maxspeed) {
this.speed = this.maxspeed;
}
}
if (Key.isDown(Key.LEFT)) {
this.speed += this.accel;
this._x = _x-this.speed;
if (this.speed>=this.maxspeed) {
this.speed = this.maxspeed;
}
}
if (Key.isDown(Key.UP)) {
this.speed += this.accel;
this._y = _y-this.speed;
if (this.speed>=this.maxspeed) {
this.speed = this.maxspeed;
}
}
if (Key.isDown(Key.DOWN)) {
this.speed += this.accel;
this._y = _y+this.speed;
if (this.speed>=this.maxspeed) {
this.speed = this.maxspeed;
}
}
} else if (!Key.isDown(Key.RIGHT) && !Key.isDown(Key.RIGHT) && !Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) {
}
}

I've tried putting in the opposite of the above functions in the else if function, but that obviously just gets it to stop, not decelerate. After a lot of trial and error... i just decided to ask for help. what should i put in the else if function? anybody's help would be appreciated. thanks


None

Johnny

Reply To Post Reply & Quote

Posted at: 1/29/06 02:27 PM

Johnny DARK LEVEL 20

Sign-Up: 04/17/04

Posts: 3,698

can you put

speed-- (substitue -- with -= .2 or something if it's slowing down too fast)

in onEnterFrame, and just make sure it doesn't drop below 0 with an else statment?


None

Johnny

Reply To Post Reply & Quote

Posted at: 1/29/06 02:30 PM

Johnny DARK LEVEL 20

Sign-Up: 04/17/04

Posts: 3,698

Nvm... forget I said that.

I need to wake up a bit.


None

4Cuties

Reply To Post Reply & Quote

Posted at: 1/29/06 02:32 PM

4Cuties LIGHT LEVEL 07

Sign-Up: 12/06/05

Posts: 12

no... it doesn't know which direction to slow down in. if you're pressing the right arrow key, and let go, that kind of script wouldn't determine which direction to go in, so it will either go instantly in the one direction you choose, or not move at all (if you script it for all four directions, they will all cancel each other out).


None

Johnny

Reply To Post Reply & Quote

Posted at: 1/29/06 03:03 PM

Johnny DARK LEVEL 20

Sign-Up: 04/17/04

Posts: 3,698

His code is using speed to universally speed up everything. Direction doesn't matter, it just needs to reduce speed by accel every frame until speed reaches 0 again, when a key is not being pressed.


None

4Cuties

Reply To Post Reply & Quote

Posted at: 1/29/06 03:16 PM

4Cuties LIGHT LEVEL 07

Sign-Up: 12/06/05

Posts: 12

yes, but once the speed value has been changed, the program won't know whether to add or subtract it from x or y. There's those four possibilities, and i don't know how to code it to know when to use which one.


None

Runouw

Reply To Post Reply & Quote

Posted at: 1/29/06 03:19 PM

Runouw LIGHT LEVEL 10

Sign-Up: 07/09/05

Posts: 4

I think this is what you mean...

onClipEvent (load) {
this.speed = 0;
this.accel = 0.5;
this.maxspeed = 5;
this.minspeed = 0.5;
this.decel = 0.8;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {
xspeed += accel;
xspeed = Math.max(xspeed, minspeed);
xspeed = Math.min(xspeed, maxspeed);
}
if (Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)) {
xspeed -= accel;
xspeed = Math.max(xspeed, -minspeed);
xspeed = Math.min(xspeed, -maxspeed);
}
if (Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) {
yspeed -= accel;
yspeed = Math.max(yspeed, -minspeed);
yspeed = Math.min(yspeed, -maxspeed);
}
if (Key.isDown(Key.DOWN) && !Key.isDown(Key.UP)) {
yspeed += accel;
yspeed = Math.max(yspeed, minspeed);
yspeed = Math.min(yspeed, maxspeed);
}
if (!Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {
xspeed *= decel;
}
if (!Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) {
yspeed *= decel;
}
_y += yspeed;
_x += xspeed;
}

I added a few changes to the code and made it multiply by a number to slow down, instead of subtract.


None

4Cuties

Reply To Post Reply & Quote

Posted at: 1/29/06 03:43 PM

4Cuties LIGHT LEVEL 07

Sign-Up: 12/06/05

Posts: 12

thanks mirage, i can see the concept that you're trying to apply, but for some reason it isn't working. I'm using mx 2004. would that make a difference?


None

Johnny

Reply To Post Reply & Quote

Posted at: 1/29/06 03:50 PM

Johnny DARK LEVEL 20

Sign-Up: 04/17/04

Posts: 3,698

He forgot to define xspeed and yspeed. I changed the bottom part and combined the !Key.isDowns.

This works. Just tested.

onClipEvent (load) {
this.xspeed = 0;
this.yspeed = 0;
this.accel = 0.5;
this.maxspeed = 5;
this.minspeed = 0.5;
this.decel = 0.8;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {
xspeed += accel;
xspeed = Math.max(xspeed, minspeed);
xspeed = Math.min(xspeed, maxspeed);
}
if (Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)) {
xspeed -= accel;
xspeed = Math.max(xspeed, -minspeed);
xspeed = Math.min(xspeed, -maxspeed);
}
if (Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) {
yspeed -= accel;
yspeed = Math.max(yspeed, -minspeed);
yspeed = Math.min(yspeed, -maxspeed);
}
if (Key.isDown(Key.DOWN) && !Key.isDown(Key.UP)) {
yspeed += accel;
yspeed = Math.max(yspeed, minspeed);
yspeed = Math.min(yspeed, maxspeed);
}
if (!Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)&&!Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) {
xspeed *= decel;
yspeed *= decel;
}
_y += yspeed;
_x += xspeed;
}


None

Johnny

Reply To Post Reply & Quote

Posted at: 1/29/06 03:53 PM

Johnny DARK LEVEL 20

Sign-Up: 04/17/04

Posts: 3,698

Acually, tested it again with the !Key.Downs combined and uncombined and his does transition better when you're moving to and from diagonals.


None

4Cuties

Reply To Post Reply & Quote

Posted at: 1/29/06 04:05 PM

4Cuties LIGHT LEVEL 07

Sign-Up: 12/06/05

Posts: 12

nearly works, but when going up and left, it doesn't accelerate, it goes instantly to the maximum speed. not sure why


None

4Cuties

Reply To Post Reply & Quote

Posted at: 1/29/06 05:48 PM

4Cuties LIGHT LEVEL 07

Sign-Up: 12/06/05

Posts: 12

Okay, i finally got it to work pretty much flawlessly. Thanks so much for your help guys


None

Runouw

Reply To Post Reply & Quote

Posted at: 1/29/06 06:08 PM

Runouw LIGHT LEVEL 10

Sign-Up: 07/09/05

Posts: 4

Oops that mas my fault there, you see in the part when it's going up, and left. Try changeing the "min" with the "max"

Heres the whole code again with the glitch fixed:

onClipEvent (load) {
this.speed = 0;
this.accel = 0.5;
this.maxspeed = 5;
this.minspeed = 0.5;
this.decel = 0.8;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {
xspeed += accel;
xspeed = Math.max(xspeed, minspeed);
xspeed = Math.min(xspeed, maxspeed);
}
if (Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)) {
xspeed -= accel;
xspeed = Math.min(xspeed, -minspeed);
xspeed = Math.max(xspeed, -maxspeed);
}
if (Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) {
yspeed -= accel;
yspeed = Math.min(yspeed, -minspeed);
yspeed = Math.max(yspeed, -maxspeed);
}
if (Key.isDown(Key.DOWN) && !Key.isDown(Key.UP)) {
yspeed += accel;
yspeed = Math.max(yspeed, minspeed);
yspeed = Math.min(yspeed, maxspeed);
}
if (!Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {
xspeed *= decel;
}
if (!Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) {
yspeed *= decel;
}
_y += yspeed;
_x += xspeed;
}

The !key.isdowns need to be separated for the _x and _y. Otherwise if you hold down one button after going diagonal, you still go diagonal.....

I think this should work. Sorry if it doesn't.


All times are Eastern Standard Time (GMT -5) | Current Time: 12:38 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!