This site requires JavaScript, please enable it in your browser!
Greenfoot back
WithExtraDip
WithExtraDip wrote ...

2019/6/26

Error: missing return statement

WithExtraDip WithExtraDip

2019/6/26

#
This is at the end of my Main actor:
    public void touch()
    {
        if(facingThing()) {
            Greenfoot.playSound("Bump.mp3");
            
        }
        else if(facingEdge()) {
            Greenfoot.playSound("Bump.mp3");
            
    }
}
    public boolean facingEdge()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();
        Greenfoot.playSound("Bump.mp3");
    }                                                                           <--error here
    public boolean facingThing()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();
        Greenfoot.playSound("Bump.mp3");
        }                                                                          <--error here
    }
It says missing return statement I tried to write return; but then it says "incompatible types: missing return value"
danpost danpost

2019/6/26

#
Both your facingEdge and facingThing methods have a boolean (true/false) return type. You cannot exit either of these methods without returning a boolean value. Either:
return true;
or
return false;
The value returned can be an expression that results in a boolean value. Some examples of expressions resulting in a boolean value are: * isAtEdge() // method with a boolean return type (state) * getIntersectingObjects(Actor.class).size() == 0 // comparison * Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this) // event occurrence * getWorld() != null // checking a state Your methods currently are not really checking anything. They are just collecting a few values and playing a sound which would play anyway, if a true value was returned.
You need to login to post a reply.