我在学校的一个项目上工作,是编程的初学者,在制作Bubble Shooter时遇到了很大的问题,在更改为map2之前,我需要先获取地图的所有内容。
尝试列出所有球,但该程序在第一个映射的末尾崩溃,并向我们提供了错误报告,它无法加载负值。我认为这是当它试图装入枪支时,并且想要放置一个if语句,该语句表明如果“ allowedBallTypes!= null”或零(可能是零),则应该装入枪支。
找不到符号-getAllowedBallTypes();
greenfoot java方法类
bubbleworld类:
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());
}
最佳答案
假设您在Cannon类的粘贴代码段中遇到了该错误,该错误表明BubbleWorld的getMap()方法存在问题-您可以将其粘贴以便我们可以看到它吗?它可能没有返回正确的类型。通常,您需要粘贴更多代码,包括完整的类,并确切说出错误发生的位置。一种更简单的方法可能是将带有源代码的方案上载到greenfoot网站(www.greenfoot.org-使用Greenfoot中的共享功能,并确保选中源代码框)并提供指向该链接的链接。