0

So for my class I have to use the Greenfoot IDE. And my goal is to:

“the pig eats all the mushrooms currently in the barrel”

The pig will have to call the barrel’s getMushroom() method to find out how many mushrooms the barrel currently stores. The pig can add this amount to its count of mushrooms eaten. Remember, the mushrooms are already gone from the world - this happened when the barrel “stored them.”

However if I try and go into the Pig class and use Barrel.getMushrooms(); it says non-static method getMushrooms() cannot be referenced from a static context.

But when I try using stuff like

Barrel b1 = new Barrel();
b1.getMushrooms();

My counter with shrooms never works out right..

Class Barrel

 import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 import java.util.List;

 /**
  * Write a description of class Barrel here.
  * 
  * @author (your name) 
  * @version (a version number or a date)
  */
 public class Barrel extends Actor
 {
     private final double SCALE_FACTOR_5 = 1.05;
     private final double SCALE_FACTOR_25 = 1.25;
     public int mushroomsStored;
     private int ns;

     public Barrel() {
         mushroomsStored = 0;
         ns = 0;
     }

     /**
      * Main method of Barrel
      */
     public void act() {
         followMouse();
         storeMushrooms();
         reset();
     }  

     /**
      * Follows mouse drag-and-drop motion.
      */
     public void followMouse() {
         if(Greenfoot.mouseDragged(this)) {
             MouseInfo mouse = Greenfoot.getMouseInfo();
             setLocation(mouse.getX(), mouse.getY());
         }
     }

     /**
      * Eats nearby mushrooms when dropped.
      * Increases its current image scale by 5% when it eats one mushroom.
      * Increases its current image scale by 25% when it eats five mushrooms.
      * If this barrel stores more than 10 mushrooms, this barrel has itself removed from
      * this world.
      */
     public void storeMushrooms() {
         if(Greenfoot.mouseDragEnded(this)) {
             List<Mushroom> nearby = getObjectsInRange(75, Mushroom.class);
             ns = nearby.size();
             for(Mushroom m : nearby) {
                 getWorld().removeObject(m);
                 mushroomsStored++;
             }
             if(ns < 5 ) {
                 GreenfootImage img = getImage();
                 int width = (int)(img.getWidth() * SCALE_FACTOR_5);
                 int height = (int)(img.getHeight() * SCALE_FACTOR_5);
                 img.scale(width, height);
             }
             if (ns >= 5) {
                 GreenfootImage img = getImage();
                 int width = (int)(img.getWidth() * SCALE_FACTOR_25);
                 int height = (int)(img.getHeight() * SCALE_FACTOR_25);
                 img.scale(width, height);
             }
             if(mushroomsStored == 10) {
                 getWorld().removeObject(this);
             }
         }
     }

     /**
      * Returns this barrel to its original (x,y) location and its
      * original image scale.
      */
     public void reset() {
         if(Greenfoot.mouseClicked(this)) {
             MouseInfo mouse = Greenfoot.getMouseInfo();
             if(mouse.getButton() == 3) {
                 this.setLocation(565, 350);
                 setImage(new GreenfootImage("barrel.png"));
                 mushroomsStored = 0;
             }
         }
     }

     /**
      * Returns how many mushrooms this barrel has stored.
      */
     public int getMushrooms() {
         return mushroomsStored;
     }

     /**
      * Automatically called by Greenfoot whenever a Barrel object
      * is placed in a world. In this assigment, we use it to remember
      * the initial state of this barrel - its (x,y) position and its
      * original image size.
      */
     public void addedToWorld(World world) {
         GreenfootImage img = new GreenfootImage("barrel.png");
         setImage(img);
         final int originalX = getX();
         final int originalY = getY();
         final int originalWidth = img.getWidth();
         final int originalHeight = img.getHeight();
     }
 }

Class Pig

 import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

 /**
  * Write a description of class Pig here.
  * 
  * @author (your name) 
  * @version (a version number or a date)
  */
 public class Pig extends Actor
 {
     /** Keeps track of how many mushrooms this pig has eaten. */
     private int shrooms;

     /** 
      * Constructs a Pig object and initializes it as having
      * eaten no mushrooms. 
      */
     public Pig() {
         shrooms = 0;
     }

     /**
      * Follows the mouse movement and eats mushrooms on mouse clicks.
      * Stops the scenario once this pig has eaten at least 15 mushrooms.
      */
     public void act() 
     {
         followMouse();
         eatMushrooms();
         getMS();
     }

     public void getMS() {
         b
     }

     public void followMouse() {
         if (Greenfoot.mouseMoved(null)) {
             MouseInfo mouse = Greenfoot.getMouseInfo();
             setLocation(mouse.getX(), mouse.getY());
         }
     }

     public void eatMushrooms() {
         if (Greenfoot.mouseClicked(null)) {
             Mushroom m = (Mushroom) getOneIntersectingObject(Mushroom.class);
             if (m != null) {
                 shrooms++;
                 getWorld().removeObject(m);
             }
         }
         if (shrooms > 29) {
             Greenfoot.stop();
         }
     }
 }
skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
  • @Bruce - Please stop suggesting edits that have backticks splattered all over. Backticks should only be used for inline code. The edit you made on this question was a *good* use of backticks, because you used them for code. Many of your other edits have backticks around file names and other things that don't need them. Also, there was no need for the quote block you put in this question. Everyone else can ignore this comment - I'm just using this question to provide some feedback to an editor. – skrrgwasme Oct 16 '15 at 04:48
  • @skrrgwasme - thanks for the helpful information. – Bruce Oct 17 '15 at 03:51

2 Answers2

0

When calling the method with the class name like

Barril.getMushrooms()

Your method should be coupled to your class and not an instance. How would you do that? By making it a static method.

I assume that you want to have an instance method used in this context so the right way to do it is the one you presented :

Barril b = new Barril();
b.getMushrooms();

But think about it. What is the value of mushroomsStored if you just created your object?

... You guessed it. You did not even initialized it so it will return the value by default : 0

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Exactly! I knew I was suppose to use Barrel b = new Barrel(); but my value for mushroom stored was all messed up.. – Spencer M. Oct 09 '15 at 12:11
0

Your pig needs a reference to the barrel class. Typically in Greenfoot, class A gets a reference to class B in one of two ways:

  1. Class A is passed the reference to B on creation. This is typical if B creates A: for example if a gun fires bullets, the gun passes a gun reference to the bullet.

  2. Class A collides with class B, for example your player character runs into a collectable, or an enemy collides with your player.

I'm not certain, but I suspect you fall under case 2, in which case you want to use:

Barrel b = (Barrel)getOneIntersectingObject(Barrel.class);

The b variable will be null when you're not touching a barrel, and otherwise it will be a Barrel object which you can call methods on.

Neil Brown
  • 3,558
  • 1
  • 27
  • 36