首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java Greenfoot,无法链接文件之间的方法

Java Greenfoot,无法链接文件之间的方法
EN

Stack Overflow用户
提问于 2011-10-19 00:47:45
回答 2查看 592关注 0票数 1

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

试图列出所有的球,但程序在第一个地图的末尾崩溃,并给我们错误报告,它无法加载负值。我认为是在它试图给枪装弹的时候,并且想要放一条if -语句,如果"allowedBallTypes != null“或零,那么它就应该给枪装弹。

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

bubbleworld类:

代码语言:javascript
复制
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());

映射类:

代码语言:javascript
复制
       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;
            }
        }
    }

Cannon类:

代码语言:javascript
复制
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());
    }
EN

回答 2

Stack Overflow用户

发布于 2011-10-19 20:25:21

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

票数 1
EN

Stack Overflow用户

发布于 2011-10-19 00:53:44

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7810810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档