Odd problem with shapeflag and hitTest

I’ve been working on getting slopes working properly for a horribly long amount of time.
I’ve almost got it working, but it’s still annoyingly difficult. Here’s the current problem.

Moving the character without moving the stage works fine, no problems there. So I tried the exact same code, but changed it so that it moves the stage instead of the character. This is where I got stuck.
When I start the game, the character falls onto the stage, hits the solid ground, and bounces all the way to the top of the bounding box, then falls again. It seems like when the character is falling, shapeflag is true. But when he hits the ground, shapeflag somehow becomes false during the while loop.


onClipEvent (load) {
    speed = 5;
    gravity = 0;
    jumping = false;
    jumpheight = 10;
    lock = false;
}
onClipEvent (enterFrame) {
    if (gravity>=11) {
        gravity = 10;
    }
    if (Key.isDown(Key.RIGHT)) {
        if (lock == true) {
            _root.ground._x -= speed;
        } else {
            this._x += speed;
        }
    }
    if (Key.isDown(Key.LEFT)) {
        if (lock == true) {
            _root.ground._x += speed;
        } else {
            this._x -= speed;
        }
    }
    if (_root.ground.hitTest(_x-12.5, _y-3, true)) {
        if (lock == true) {
            _root.ground._x -= speed;
        } else {
            this._x += speed;
        }
    }
    if (_root.ground.hitTest(_x+12.5, _y-3, true)) {
        if (lock == true) {
            _root.ground._x += speed;
        } else {
            this._x -= speed;
        }
    }
    if (Key.isDown(Key.SPACE) && !jumping) {
        gravity = -jumpheight;
        jumping = true;
    }
    if (!_root.ground.hitTest(_x, _y-1+this._height/2, true)) {
        gravity++;
        if (lock == true) {
            _root.ground._y -= gravity;
        } else {
            this._y += gravity;
        }
    } else {
        jumping = false;
        gravity = 0;
    }
    while (_root.ground.hitTest(_x, _y-1+this._height/2, true)) {
        jumping = false;
        if (lock == true) {
            _root.ground._y++;
        } else {
            this._y--;
        }
    }
}