Need help with fixing/optimizing game

OK, this is my first attempt at making a game with flash, and I decided to make a simple it based on super metroid. So far i have taken care of most of the code for the movement, but i ran into these strange problems.

  • Collision detection constantly switches between true and false, even if the character is standing still.

  • I want the character to accelerate when the right/left keys are pressed, but to stop inmedatly after these keys are released. The way i tried causes movement to frequently even if the the keys are still down, so im using a gradual slow-down for now.

-I cant find out how to make the character change from ball to crouching, then to standing, and finally then to jump when the UP key is pressed. Right now it does this sequence almost inmediatly. I want it to have to release the up key and then press it again to change one stance at a time.

Here is the AS code for the main character:

**onClipEvent(load){
//ā€œSā€ is how fast samus accelerates in the x axis
//ā€œYā€ **is how fast accelerating the y axis
**//this sets gravity
G=1.5
ground=false
}

onClipEvent(enterFrame){

if (!ground && Y<30)Y+=G;

//this should check for collision between this and the floors
for (i=1; i<6; ++i){
if( eval("_root.bar"+i).hitTest(this._x-18,this._y,true) || eval("_root.bar"+i).hitTest(this._x+18,this._y,true) && Y>0 ){
S=2
Y=0;
this._y=eval("_root.bar"+i )._y + 1;
ground=true;

         //gotoAndStop(1+D);
         break;
     } else ground=false;

}

if (X && ground){
if(X<0)X+=1;
if(X>0)X-=1;
X=Math.floor(X)
}
if (!X && ground) gotoAndStop(1+D);

if (Key.isDown(Key.RIGHT)){
if (X<=10){
X+=S;
D=0;
}
switch (this._currentframe){
case 1:
gotoAndStop(5);
break;
case 2:
gotoAndStop(5);
break;
case 3:
gotoAndStop(5);
break;
case 4:
gotoAndStop(3);
break;
case 5:
break;
case 6:
gotoAndStop(5);
break;
case 7:
break;
case 8:
gotoAndStop(7);
break;
case 9:
break;
case 10:
gotoAndStop(9);
break;
case 11:
break;
case 12:
gotoAndStop(11);
break;
}

}
if (Key.isDown(Key.LEFT)){
if (X>=-10){
X-=S;
D=1
switch (this._currentframe){
case 1:
gotoAndStop(6);
break;
case 2:
gotoAndStop(6);
break;
case 3:
gotoAndStop(4);
break;
case 4:
gotoAndStop(6);
break;
case 5:
gotoAndStop(6);
break;
case 6:
break;
case 7:
gotoAndStop(8);
break;
case 8:
break;
case 9:
gotoAndStop(10);
break;
case 10:
break;
case 11:
gotoAndStop(12);
break;
case 12:
break;
}
}
}

if (Key.isDown(Key.UP)){
if (Math.abs(X)>2)gotoAndStop(7+D);
else gotoAndStop(11+D);
if (ground){
ground=false;
X*=.6
Y=-25;
S=.2

 }

}

if (Key.isDown(Key.DOWN)){
if (this._currentframe!=3+D ) gotoAndStop(3+D);
else if (this._currentframe!=9+D ) gotoAndStop(9+D);
}

this._x += X
this._y += Y
}**

Any suggestion on how to fix these error or how to optimize the code will be REALLY appreciated.

Thanks.