# \[AS2\] Keeping Player above ground problem

**URL:** <https://forum.kirupa.com/t/as2-keeping-player-above-ground-problem/327678>\
**Category:** programming\
**Created:** [March 19, 2012, 11:17pm UTC](https://forum.kirupa.com/t/as2-keeping-player-above-ground-problem/327678 "2012-03-19T23:17:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![silverneo188](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@silverneo188](https://forum.kirupa.com/u/silverneo188)\
**Post date:** [March 19, 2012, 11:17pm UTC](https://forum.kirupa.com/t/as2-keeping-player-above-ground-problem/327678/1 "2012-03-19T23:17:10Z")

</div>

Ok so I programming a character to when they press the UP key they jump and then gravity takes place. The only problem I have is whenever I force the player above the ground the gravity keeps taking place so the player jigs up and down constantly. How can I fix this? Here is my current code

Note that this code is on my player (named char) and is interacting with a dynamic floor movieclip called “ground”

```auto

onClipEvent(load){
	jumping = false;
	gravitySpeed = 3;
}

onClipEvent(enterFrame){
	walkSpeed = 5;
	
	if(Key.isDown(Key.LEFT)){
		this._x -= walkSpeed;
		this._xscale = -100;
	}
	if(Key.isDown(Key.RIGHT)){
		this._x += walkSpeed;
		this._xscale = 100;
	}
	
	touchingGround = _root.ground.hitTest(this._x, this._y, true);
	
	if(!touchingGround && !jumping){
		this._y += gravitySpeed;
	}
	
	if(Key.isDown(Key.UP) && !jumping){
		jumping = true;
		gravitySpeed = -15;
	}
	
	if(jumping){
		if(gravitySpeed < 15){
		gravitySpeed += 1;
		}
		
		this._y += gravitySpeed;
		
		if(touchingGround){
			jumping = false;
		}
	}
	
//This is the part of the code that puts the player above the ground
	if(_root.ground.hitTest(this._x, this._y + 1)){
		this._y -= 1;
	}
}

```
