I've had tons of problems with trying to figure it out. But when it increases the speed and you release it just slides and dosn't really stop.
boolean aboutFace;
boolean onGround;
boolean atWorldEdge;
int ySpeed = 0, xSpeed = 0;
final int jSpeed = 20
private void move()
{
ySpeed++; // adds gravity
if (xSpeed != 0 && onGround) xSpeed +=aboutFace?1:-1; // add friction
setLocation(getX()+xSpeed/10, getY()+ySpeed/2);
// check for change in horizontal direction
if((xSpeed>0 && aboutFace) || (xSpeed<0 && !aboutFace))
{
getImage().mirrorHorizontally();
aboutFace = !aboutFace;
}
// check for obstacles
onGround=false; // initialize value
// check below the actor
while(getOneObjectAtOffset(0, getImage().getHeight()/2+1, null)!=null)
{
setLocation(getX(), getY()-1);
onGround=true;
ySpeed=0;
}
// check above the actor
while(getOneObjectAtOffset(0, -getImage().getHeight()/2-1, null)!=null)
{
setLocation(getX(), getY()+1);
ySpeed = 0;
}
// check to right of actor
while(getOneObjectAtOffset(getImage().getWidth()/2+1, 0, null)!=null)
{
setLocation(getX()-1, getY());
xSpeed = 0;
}
// check to left of actor
while(getOneObjectAtOffset(-getImage().getWidth()/2-1, 0, null)!=null)
{
setLocation(getX()+1, getY());
xSpeed = 0;
}
}
public void movement()
{
if (Greenfoot.isKeyDown("left")) xSpeed-=2;
if (Greenfoot.isKeyDown("right")) xSpeed+=2;
if (Greenfoot.isKeyDown("up") && onGround)
{
ySpeed -= jSpeed;
}
}
