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

2018/3/9

Having problems with a "missing return statement"

DeRock DeRock

2018/3/9

#
I'm making my own game for an assignment in my Digital Media class and i'm having this issue where at the end of the public boolean isAtEdge code, it gives the error "missing return statement". If somebody can show me whats wrong I would be extremely grateful. Btw i'm completely new to coding so if its obvious what the problem is please don't judge.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Bullet here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        move(4);
        impact();
        isAtEdge();
    }
    public void impact()
    {
        Actor Enemy1;
        Enemy1 = getOneObjectAtOffset(0, 0, Enemy1.class);
        if (Enemy1 != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Enemy1);
        }
        Actor Enemy2;
        Enemy2 = getOneObjectAtOffset(0, 0, Enemy2.class);
        if (Enemy2 != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Enemy2);
        }
        Actor Enemy3;
        Enemy3 = getOneObjectAtOffset(0, 0, Enemy3.class);
        if (Enemy3 != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Enemy3);
        }
        Actor Enemy4;
        Enemy4 = getOneObjectAtOffset(0, 0, Enemy4.class);
        if (Enemy4 != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Enemy4);
        }
    }
    public boolean isAtEdge()
    {
        World world;
        world = getWorld();
        world.removeObject(this);
    } // Error Occurs Here
}
davmac davmac

2018/3/9

#
Your method is declared to return a boolean:
1
public boolean isAtEdge()
But, it doesn't return anything - it doesn't even have a return statement. If you don't want it to return anything, declare it as void, not boolean. (However, there is a logical problem as well. You call isAtEdge() from act(), and isAtEdge() removes the actor from the world. So the actor will always immediately remove itself from the world).
DeRock DeRock

2018/3/9

#
I probably should have mentioned this before but i'm trying to make it so that the actor disappears at the edge of the world, couldn't figure it out so i tried to do that, as I said i'm a complete noob at coding so whatever helps is good.
davmac davmac

2018/3/9

#
Ah. Actually, you were probably trying to use the existing "isAtEdge" method (which does indeed return a boolean). Instead what you've done is overridden it with your own version. Code that looks like this:
1
public boolean isAtEdge()
is declaring a method - that is, it's saying that the method exists, has a particular return type (boolean in this case), and is public. I think what you really wanted was to call the method, which you can do from the act method. So, remove the isAtEdge() method that you have declared, and in the act method, write:
1
2
3
4
5
if (isAtEdge()) {
        World world;
        world = getWorld();
        world.removeObject(this);
}
You can in fact shorten that down to:
1
2
3
if (isAtEdge()) {
    getWorld().removeObject(this);
}
DeRock DeRock

2018/3/12

#
Thank you so much, worked perfectly!
You need to login to post a reply.