0
    onClipEvent(enterFrame){
    speed = 8;
    jump = 20;
    gravity = 10;

    // gravity
    this._y+=gravity;
    _root.cam._y = this._y;

// movement
    if(Key.isDown(68)){
        play();
        _root.cam._x += speed;
        this._x += speed;
        _xscale=90
       }
     if(Key.isDown(65)){
         play();
        _root.cam._x -= speed;
        this._x -= speed;
        _xscale=-90
       } 

       if(Key.isDown(Key.SPACE)){
        this._y -= jump;
        jump--;
       }


      if(this, hitTest(_root.wall)){
          this._y-=(gravity);
       }

}   //END 

So I'm trying to make an movie clip that I have "jump" whenever i hit space. It should decrement the jump speed when i hit space, but it doesnt seem to do that. Can anyone tell me why? (the code here is directly on my mc)

  • _root.cam is the camera code :3 – user569322 Mar 18 '11 at 21:34
1

You need to introduce a new variable to keep track of the y-velocity. Then, instead of incrementing the y-position by gravity each frame, you need to increment the y-velocity by gravity. At the end of each frame, you can increment y-position by y-velocity.

Whenever you want to jump, its a matter of setting the y-velocity to a negative value.

Pseudocode:

dy+=GRAVITY;
if(jump pressed)
    dy=-JUMP_SPEED;
y+=dy;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy