我刚开始用Greenfoot编程,一路学习java。我已经熟悉了如何在类之间调用某些方法,以及静态和非静态方法之间的区别。
我在做一个游戏,你玩螃蟹,四处收集虫子。有一只龙虾随机地四处游荡,如果与螃蟹接触,螃蟹就会腐烂。每次你吃一条虫子,分数就会上升10。
它包含5个类,分别命名为螃蟹、龙虾、蠕虫、计数器和CrabWorld。为了您的利益,我只需要将详细的文档化的代码发布给您阅读。但是,我遇到困难的重要部分是调用一个方法,从龙虾到由CrabWorld创建的Crab实例。这种方法将改变螃蟹的生活。
我尝试过调用(( CrabWorld ) getWorld),但不需要访问CrabWorld。我需要从龙虾类访问Crab实例(如果这很重要的话,在CrabWorld中创建)。
克拉布世界:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Creates the crab Enviornment with a counter, crab and lobster. Also has methods to
* place random worms in the world over time. Also has a method to add the score
* To the Counter score
* @author Troy Bick
* @version 12/20/13
*/
public class CrabWorld extends World
{
private Actor playerCrab = new Crab();
private Counter score = new Counter("Score: ");
/**
* Constructor for objects of class CrabWorld.
*
*/
public CrabWorld()
{
super(560, 560, 1);
prepare();
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
addObject(score, 100, 540);
addObject(playerCrab, 280, 280);
addObject(new Lobster(), 100, 100);
}
/**
* Randomly places worms at random periods
*/
public void act()
{
if(Greenfoot.getRandomNumber(100)<0.5){
addObject(new Worm(), Greenfoot.getRandomNumber(540)+10, Greenfoot.getRandomNumber(540)+10);
}
}
public void eatenWorm()
{
score.add(10);
}
public void eatsCrab()
{
playerCrab.isEaten();
}螃蟹:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Crab extends Actor
{
private int wormsEaten = 0;
private int lives = 3;
/**
* Act - do whatever the Crab wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment. Run calls Act every
* frame.
*/
public void act()
{
moveAndTurn();
eat();
}
/**
* Determines the key pressed and moves/turns the crab.
*/
public void moveAndTurn()
{
move(3);
if(Greenfoot.isKeyDown("a")){
turn(3);
}
if(Greenfoot.isKeyDown("d")){
turn(-3);
}
}
/**
* Detects if the worm is close to the crab, and if so, eats it.
*/
public void eat()
{
Actor worm;
worm = getOneObjectAtOffset(0,0,Worm.class);
World world = getWorld();
if(worm != null)
{
world.removeObject(worm);
Greenfoot.playSound("eating.wav");
wormsEaten++;
((CrabWorld) getWorld()).eatenWorm();
}
}
/**
* Returns the number of worms eaten.
*/
public int getWormsEaten()
{
return wormsEaten;
}
/**
* Returns the number the lives the crab has left.
*/
public int getLivesCount()
{
return lives;
}
/**
* Subtracts a life from lives.
*/
public void eaten()
{
lives--;
}
}龙虾:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Lobster here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lobster extends Actor
{
/**
* Act - do whatever the Lobster wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
moveAround();
eat();
}
public void eat()
{
Actor crab;
crab = getOneObjectAtOffset(0,0,Crab.class);
if(crab != null)
{
World world = getWorld();
world.removeObject(crab);
((CrabWorld) getWorld()).eatsCrab();
}
}
public void moveAround()
{
move(3);
//Random Movements
if(Greenfoot.getRandomNumber(100) < 10)
{
turn(Greenfoot.getRandomNumber(40) - 20);
}
//World Edge Detection
if(getX() <= 10 || getX() >= getWorld().getWidth()-10)
{
turn(10);
}
if(getY() <= 10 || getY() >= getWorld().getHeight()-10)
{
turn(10);
}
}
}因此,当添加到世界上的龙虾吃螃蟹时,我希望那只螃蟹失去生命,但是当我试图编译时,我在CrabWorld类上得到一个错误,它找不到上面提到的方法。为什么会这样呢?
只是很困惑..。如果有人能帮我那就太好了。如果我漏掉了什么,我就修好它。
谢谢你,特洛伊
发布于 2013-12-23 08:56:40
问题在于,在CrabWorld中,您将playerCrab声明为
private Actor playerCrab = new Crab();因此,尽管playerCrab实际上是Crab的一个实例,但对于CrabWorld,它是一个Actor,因此您只能调用Actor定义的方法。
您可以将声明更改为
private Crab playerCrab = new Crab();这样,CrabWorld就知道playerCrab是Crab的一个实例,然后可以调用Crab定义的任何公共方法。考虑到您的成员变量名为playerCrab,似乎总是将其作为一个Crab来使用,因此将其声明为一个变量当然是合适的。
如果您控制Actor类型,另一种方法是向它添加一个loseLife()方法,如果您认为失去生命的概念对于扩展Actor的类是常见的。您的Lobster类目前还没有这个概念,但是如果您决定将其添加到其中,那么Actor上的方法将是一种适当的方法。
发布于 2013-12-20 17:10:58
看起来,在龙虾类中有一个getWorld方法,我假设它得到了整个世界。如果您在您的getCrab中添加了一个CrabWorld方法,那么您的龙虾可以访问螃蟹。
内部CrabWorld
public Crab getCrab(){
return playerCrab;
}然后在龙虾里
((CrabWorld) world).getCrab();https://stackoverflow.com/questions/20708443
复制相似问题