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) { |
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.)
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.