从其他类java调用方法

时间:2013-12-20 16:56:21

标签: java class methods

我刚开始用Greenfoot编写一些东西,沿途学习java。我已经熟悉如何在类之间调用某些方法,以及静态和非静态之间的区别。

我正在制作一个游戏,你在那里玩螃蟹并四处移动来收集蠕虫。有一只随机漫游的龙虾,如果与螃蟹接触,螃蟹会消失。每次吃蠕虫,分数都会上升10。

它包含5个名为Crab,Lobster,Worm,Counter和CrabWorld的类。为了您的利益,我将发布记录良好的代码供您阅读。但是我遇到麻烦的重要部分是调用从龙虾到CrabWorld创建的Crab实例的方法。这种方法会改变螃蟹的生命。

我试过调用((CrabWorld)getWorld),但我不需要访问CrabWorld。我需要从龙虾类访问Crab实例(在CrabWorld中创建,如果这很重要)。

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类上得到一个错误,它找不到提到的方法。那是为什么?

非常困惑......如果有人能帮助我那会很棒。如果我遗失任何东西,我会解决它。

先谢谢了, 特洛伊

2 个答案:

答案 0 :(得分:0)

所以看起来你的Lobster类中有一个getWorld方法,我假设它可以获得整个世界。如果你在CrabWorld中添加了一个getCrab方法,那么你的龙虾可以访问螃蟹。

Inside CrabWorld

public Crab getCrab(){
   return playerCrab;
}

然后在龙虾

((CrabWorld) world).getCrab();

答案 1 :(得分:0)

问题在于CrabWorld您将playerCrab声明为

private Actor playerCrab = new Crab();

因此尽管playerCrab实际上是Crab的一个实例,但对于CrabWorld来说它是一个Actor,因此你只能调用Actor定义的方法。

您可以将声明更改为

private Crab playerCrab = new Crab();

这样CrabWorld知道playerCrabCrab的一个实例,然后您可以调用Crab定义的任何公共方法。鉴于您的成员变量被称为playerCrab,您似乎总是将其作为Crab,因此将它声明为一个当然是合适的。

另一种方法是,如果您控制Actor类型,则可以添加loseLife()方法,如果您认为丢失生命的概念对于扩展{{1}的类来说很常见}。您的Actor类目前没有这个概念,但是如果您决定将其添加到该类,则Lobster上的方法将是一种适当的方法。