Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling methods from other classes java
    primarykey
    data
    text
    <p>I have just started programming some things with Greenfoot, learning java along the way. I have become familiar on how to call certain methods between classes, and also differences between static and non-static.</p> <p>I'm makeing a game where you play the crab and move around to collect worms. There is a lobster that randomly roams around and if comes in contact with the crab, the crab dissapears. Every time you eat a worm, the score goes up by 10.</p> <p>It contains 5 classes named Crab, Lobster, Worm, Counter, and CrabWorld. For your sake, I'll just post the well documented code for you to read. But the important part that I am having trouble with is calling a method from the lobster to the Crab instance created by the CrabWorld. This method would change the lives of the crab. </p> <p>I have tried calling ((CrabWorld) getWorld), but I don't need to access the CrabWorld. I need to access the Crab instance (created in the CrabWorld, if that matters) from from the Lobster Class.</p> <p>Crabworld:</p> <pre><code>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)&lt;0.5){ addObject(new Worm(), Greenfoot.getRandomNumber(540)+10, Greenfoot.getRandomNumber(540)+10); } } public void eatenWorm() { score.add(10); } public void eatsCrab() { playerCrab.isEaten(); } </code></pre> <p>Crab:</p> <pre><code>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--; } } </code></pre> <p>Lobster:</p> <pre><code>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) &lt; 10) { turn(Greenfoot.getRandomNumber(40) - 20); } //World Edge Detection if(getX() &lt;= 10 || getX() &gt;= getWorld().getWidth()-10) { turn(10); } if(getY() &lt;= 10 || getY() &gt;= getWorld().getHeight()-10) { turn(10); } } } </code></pre> <p>So when the lobster that has be added to the world eats the crab, I want that crab to lose a life, but when I try to compile, I get an error on the CrabWorld class that it could not find the method mentioned. Why is that?</p> <p>Just very confused... If anyone could help me that would be great. If I'm missing anything, I fix it. </p> <p>Thanks in advanced, Troy</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload