每当我点击编译时,它都会说无论 private 所在的位置都是非法的表达式开始。这是代码:
/**
* Act - do whatever the PlatformJumper wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(inTheAir)
{
fall();
} else {
getCommand();
}
move();
if(isTouching(snowball.class))
{
get (snowball.class);
}
private void run (String direction)
{
if(direction=="left")
deltaX = walkSpeed*-1;
else
deltaX = walkSpeed;
}
private void stop ()
{
deltaX = 0;
}
private void jump()
{
deltaY += jumpHeight;
inTheAir = true;
}
/*
* fall() will be called whenever BallGuy is in the air. Decreases the deltaY by 1, creating
* gravity.
*/
private void fall()
{
deltaY-=fallSpeed;
}
private void move()
{
double newX = getX() + deltaX;
double newY = getY() - deltaY;
Actor platformBelow = getOneObjectAtOffset(0, groundHeight + 5, Platform.class);
Actor platformAbove = getOneObjectAtOffset(0, -(groundHeight + 5), Platform.class);
Actor platformToRight = getOneObjectAtOffset(sideWidth+5, 0, Platform.class);
Actor platformToLeft = getOneObjectAtOffset(-(sideWidth+5), 0, Platform.class);
if(platformBelow!=null)
{
if(deltaY<0)
{
deltaY = 0;
inTheAir = false;
GreenfootImage platformImage = platformBelow.getImage();
int topOfPlatform = platformBelow.getY() - platformImage.getHeight()/2;
newY = topOfPlatform - groundHeight;
}
}else if(getY() >= worldHeight - groundHeight) {
if(deltaY < 0)
{
deltaY = 0;
inTheAir = false;
newY = worldHeight - groundHeight;
}
} else {
inTheAir = true;
}
if(platformAbove != null)
{
if(deltaY>0)
{
deltaY=0;
GreenfootImage platformImage = platformAbove.getImage();
int bottomOfPlatform = platformAbove.getY() + platformImage.getHeight()/2;
newY = bottomOfPlatform + groundHeight;
}
}
if(getX()<=sideWidth)
{
deltaX = Math.abs(deltaX);
}
if(getX()>=worldWidth-sideWidth)
{
deltaX = Math.abs(deltaX) * -1;
}
if(platformToRight!=null)
{
deltaX = Math.abs(deltaX) * -1;
}
if(platformToLeft!=null)
{
deltaX = Math.abs(deltaX);
}
setLocation((int)newX,(int)newY);
}
private void getCommand()
{
if(Greenfoot.isKeyDown("left"))
{
run("left");
} else if (Greenfoot.isKeyDown("right"))
{
run("right");
} else
{
stop();
}
if(Greenfoot.isKeyDown("up"))
{
jump();
}
}
}