1

在学校做一个项目,我是编程的初学者,我在制作 Bubble Shooter 时遇到了很大的问题,我需要在它变成 map2 之前得到地图的所有球。

试图列出所有的球,但程序在第一张地图的末尾崩溃,并给我们一个错误报告,它不能加载负值。我想它是在它试图给枪上膛的时候,并想放一个 if 语句,说如果“allowedBallTypes != null”或零(可能是),那么它应该给枪上膛。

找不到符号 - getAllowedBallTypes(); greenfoot java方法类

泡泡世界类:

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());

地图类:

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

大炮类:

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

2 回答 2

1

假设您在 Cannon 类的粘贴片段中遇到该错误,该错误表明 BubbleWorld 的 getMap() 方法存在问题——您可以将其粘贴进去以便我们查看吗?它可能没有返回正确的类型。通常,您需要粘贴更多代码,包括完整的类,并准确说明错误发生的位置。一种更简单的方法可能是将带有源代码的场景上传到 greenfoot 网站(www.greenfoot.org - 使用 Greenfoot 中的共享功能,并确保勾选源代码框)并提供链接。

于 2011-10-19T12:25:21.480 回答
0

根据您的代码,您的地图类似乎没有 int[] allowedBallTypes; 的类级变量声明;

于 2011-10-18T16:53:44.060 回答