0

The problem I'm having is that I don't know how to make the character stop jumping.

For example, when I constantly hit space it keeps jumping and jumping. When I press space I want it to jump and while the MC is jumping I want to disable the space button (or if possible could you also tell me how to disable MouseEvents) so that while in the air the MC can only jump once.

var gravity = 0.8;
var floor = 251;
player.y = floor;
player.speedY = 0;
player.impulsion = 10;
stage.addEventListener(Event.ENTER_FRAME, enterframe);
function enterframe(e:Event) {
    player.speedY += gravity;
    player.y += player.speedY;
    if(player.y > floor) {
        player.speedY = 0;
        player.y = floor

    }

}
stage.addEventListener(KeyboardEvent.KEY_DOWN, space);
function space(e:KeyboardEvent) {
    if(e.keyCode == Keyboard.SPACE) {
        player.speedY = -player.impulsion
    }
}
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
user1666767
  • 117
  • 3
  • 13

1 Answers1

2

I usually just have a Boolean variable like "onFloor" which is set to true when the layer lands, and set to false when they jump. Then only let them jump if they are onFloor. So here I've set it to true inside your if(player.y > floor), and false after if(e.keyCode == Keyboard.SPACE && onFloor):

var gravity = 0.8;
var floor = 251;
var onFloor:Boolean = false;

player.y = floor;
player.speedY = 0;
player.impulsion = 10;
stage.addEventListener(Event.ENTER_FRAME, enterframe);
function enterframe(e:Event) {
    player.speedY += gravity;
    player.y += player.speedY;
    if(player.y > floor) {
        player.speedY = 0;
        player.y = floor;
        onFloor = true;
    }

}
stage.addEventListener(KeyboardEvent.KEY_DOWN, space);
function space(e:KeyboardEvent) {
    if(e.keyCode == Keyboard.SPACE && onFloor) {
        player.speedY = -player.impulsion;
        onFloor = false;
    }
}
David Mear
  • 2,254
  • 2
  • 13
  • 21