gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:28 25 4
gpt4 key购买 nike

在学校做一个项目,我是编程初学者,我在制作《Bubble Shooter》时遇到了很大的问题,我需要在 map 更改为map2之前获取 map 上的所有球..

尝试列出所有球,但程序在第一个 map 末尾崩溃,并向我们提供错误报告,指出它无法加载负值。我认为这是当它试图给枪装弹并想要添加一个 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());

map 类:

       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 中的共享功能,并确保勾选源代码框)并提供指向该网站的链接。

关于Java Greenfoot,无法在文件之间链接方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7810810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com