Java Greenfoot, cannot link method between files

618 Views Asked by At

Working on a project in school, I'm a beginner to programming and I have big problems with the making of Bubble Shooter, I need to get all of the balls of the map before it changes to map2..

Trying to make it with listing all of the balls but the program crashes at the end of the first map and gives us the error-report that it can't load a negative value. I figured it was when it was trying to load the gun and wanted to put an if-statement that says that if "allowedBallTypes != null" or zero, as it might be, than it should load the gun.

cannot find symbol - getAllowedBallTypes(); greenfoot java method class

The bubbleworld class:

public BubbleWorld() 
{    
        super(Map.MAX_WIDTH*Map.COLUMN_WIDTH, Map.MAX_HEIGHT*Map.ROW_HEIGHT, 1,false); 

        // Max speed. We use time-based animation so this is purely for smoothness,
        // because Greenfoot is plain stupid. I can't find a way to get 60 Hz so this is
        // what we have to do. Note: Exporting the game seems to cap this to some value < 100. :(
        Greenfoot.setSpeed(100);

        // Load the map.
        map = new Map(this, testMap1);

        // Update the allowed ball types. (i.e. we don't want to spawn a
        // certain color of balls if the map doesn't contain them!)
        map.updateAllowedBallTypes();

        // Create the cannon.
        Cannon cannon = new Cannon();
        addObject(cannon, getWidth()/2, getHeight());

The map class:

       public int[] getAllowedBallTypes()
    {
        return allowedBallTypes;
    }


public void updateAllowedBallTypes()
    {
        int allowedCount = 0;
        boolean[] allowed = new boolean[Ball.typeCount];

        // Only ball types that exist in the map RIGHT NOW as attached balls will be allowed.
        for(Cell c : cells)
        {
            if(c != null && c.getBall() != null && c.isAttached())
            {
                int type = c.getBall().getType();

                if(!allowed[type])
                    allowedCount++;

                allowed[type] = true;
            }
        }

        allowedBallTypes = new int[allowedCount];
        int writeIndex = 0;
        for(int type = 0; type < Ball.typeCount; ++type)
        {
            if(allowed[type])
            {
                allowedBallTypes[writeIndex++] = type;
            }
        }
    }

The Cannon class:

private void prepareBall()
    {
        // Get a random ball type from the list of allowed ones. Only balls currently in the map
        // will be in the list.
        int[] allowedBallTypes = ((BubbleWorld)getWorld()).getMap().getAllowedBallTypes();
        int type = allowedBallTypes[Greenfoot.getRandomNumber(allowedBallTypes.length)];

        // Create it and add it to the world.
        ball = new Ball(type);
        getWorld().addObject(ball, getX(), getY());
    }
2

There are 2 best solutions below

0
On

Assuming you are getting that error in the pasted snippet of the Cannon class, the error suggests that there is a problem with the getMap() method of BubbleWorld -- can you paste that in so we can see it? It might not be returning the correct type. In general, you need to paste in more code, including complete classes, and say exactly where the error occurs. An easier way might be to upload your scenario with source code to the greenfoot website (www.greenfoot.org -- use the share function in Greenfoot, and make sure to tick the source code box) and give a link to that.

4
On

Based on your code, your map class doesn't seem to have a class-level variable declaration of int[] allowedBallTypes;