绿脚错误,“非法开始表达”

时间:2015-09-30 22:48:09

标签: java greenfoot

每当我点击编译时,它会说私有的地方非法开始表达。这是代码:

    /**
     * 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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

import greenfoot.*;

/**
 * Write a description of class BallGuy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BallGuy extends
{
    public BallGuy()
    {
    }

代码顶部应该有类似的内容。现在,编译器认为代码底部有额外的括号。我假设你把这个留给了那个问题。

ALSO:

在第一个私有方法上方的行上添加一个括号。 act()方法未关闭。

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;
}

就像那样。