ID:709988
 
Ok, so I searched for a whole week on jump codes and found they have never worked. The one that worked for me I had tweaked up a lot to get it to work.

I noticed every site was saying this.x and this.y, but I found out that this._x and this._y worked better for me. (For some odd unknown reason because I never took ActionScript classes, but I'm taking an Adobe Flash CS3 class and it included a few things like stop(); and gotoAndPlay();)

So anyways, here's the code:
onClipEvent(load) {
var jumping = false;
var jumpwait=0;
var fallwait=0;
var yPos = this._y;
}
onClipEvent(enterFrame) {
if(jumpwait >= 5)
jumping=true;
if(jumpwait < 5&&jumpwait!=0)
jumpwait+=1;
if(fallwait >=5)
jumping=false;
this._y=366;
if(fallwait < 5&&fallwait!=0)
fallwait+=1;
if(jumping == true){
jumpwait=0
this._y += jumprate;
jumprate --;
fallwait=1
jumping = false;
this.gotoAndPlay("stand");
}
// if (_root.jumping == false) {
if (this.jumping == false) {
if (Key.isDown(Key.UP)) {
this.gotoAndPlay("jump");
// jumping = true;
jumprate = 40;
jumpwait=1
this._y -= jumprate;

}
}

}


I'm basically trying to make it to where they jump smoothly instead of just flying up 40 pixels and down under the stage (Under where the Movie Clip Symbol originally was) then back to the original place it was in about the same time as sleep(3) in DM (Without changing the FPS var)

Is there any way I can do this? If you just paste in code, please try to explain it using the // note on each line or break the code down for me so I can understand it.

I am not very skilled in ActionScript and as I said, all I've learned in the class is mostly Movie Control (stop();, play();, gotoAndPlay();, gotoAndStop();, etc) although I have learned things like this from simply googling it. (Which was what I originally did for this code.)
I didn't entirely understand your explanation of what you want (or maybe that was of what it was doing currently), but this looks pretty good:

onClipEvent(load) {
var jumping = false; // Whether or not we're currently jumping
var velocity = 0; // Our current y-velocity
var groundY = this._y; // Our initial y-position will be our "ground" to land on
var jumpVelocity = -20; // The initial velocity when you jump (remember, negative is "Up")
var gravity = 1; // How quickly it loses its jump velocity (remember, positive is "Down")
}

onClipEvent(enterFrame) {
if (!this.jumping) { // If we aren't currently jumping
if (Key.isDown(Key.UP)) {
// Mark us as jumping
this.jumping = true;
// Start us off with a velocity
this.velocity = this.jumpVelocity;
// Play the jump animation
this.gotoAndPlay("jump");
}
}
else {
// We're currently jumping, so:
this._y += this.velocity; // Add the current velocity to our y-position
this.velocity += this.gravity; // Alter the velocity in the direction of gravity

if (this._y >= this.groundY) { // Check if we've passed the ground level (aka landed)
this._y = this.groundY; // Set us back to ground-level exactly (we might have passed it)
this.jumping = false; // We can jump again now
this.velocity = 0; // Reset velocity
this.gotoAndPlay("stand"); // Start the standing animation
}
}
}


Basically, when they press UP it marks them as jumping and gives them a y-velocity. Then while jumping, it alters that velocity by the amount of gravity, until it actually starts moving in the opposite direction. Then it falls back to the "ground" (passes its initial y-position) and we reset them. Keep in mind that _y=0 is at the top of the screen, and you have to increase _y to move "down" (that always screws me up).

I assume you already have stop(); codes in your movieclip timeline for the end of the jump sequence/standing sequence. If they're just one frame, you might want to use gotoAndStop(); instead of gotoAndPlay();

Also, it's _x in AS2 and just x in AS3, so you might have been reading an AS3 tutorial.

I tried to comment it as clearly as possible, but let me know if you want further explanation.
In response to DarkCampainger
Thanks! I tested out the code by itself and it worked, so I'll try and fix up the numbers a little bit, but this helped! I have trouble on this stuff because it doesn't seem to work as it should for me. (Which is why I like DM coding better in my opinion.)
In response to DarkCampainger
I changed the gravity var to 2 and it won't go back to the stand animation for some reason. Should I change any more numbers to make it do that?

Edit: Nevermind I changed something and it works.
The tricky thing about working with ActionScript 2 is that just about everything is dynamic. If you try to access a variable that doesn't exist (ie x instead of _x) it just creates it for you and goes on its merry way.

However, it does have a runtime debugger that lets you step through code, which can be very useful once you learn how to use it.
In response to DarkCampainger
Oh, ok. That's really strange. Good thing byond doesn't do that, or I wouldn't be standing here today fixing ActionScript stuff for the same exact issue that Byond doesn't have.