【问题标题】:Java Greenfoot, cannot link method between filesJava Greenfoot,无法在文件之间链接方法
【发布时间】:2011-12-10 06:45:39
【问题描述】:

在学校做一个项目,我是编程的初学者,我在制作 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());
    }

【问题讨论】:

    标签: java class methods greenfoot


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 它给出了错误:找不到符号 - 方法 getAllowedBallTypes()
      • 我一直在帮他做这个项目,但是在此之前我没有接触过JAVA和这个程序,所以我建议他在这里问。
      • 我是初学者,不能 100% 确定您对类级别变量声明的含义,请向我解释一下 :)
      • 你还没有发布你的整个地图课;您在地图中的方法引用数组“allowedBallTypes”,但您没有显示任何您实际将“allowedBallTypes”声明为int []的地方。没有比这更具体的了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 2014-09-22
      • 2017-11-26
      • 1970-01-01
      相关资源
      最近更新 更多