Итак, для моего класса я должен использовать IDE Greenfoot. И моя цель состоит в том, чтобы:Статические методы в greenfoot
«свинья ест все грибы в настоящее время в бочке»
Свиньи придется вызывать getMushroom()
метод ствола, чтобы узнать, сколько грибов ствола в настоящее время магазины , Свиньи могут добавить это количество в количество съеденных грибов. Помните, что грибы уже ушли из мира - это случилось, когда ствол
Однако если попытаться перейти в класс свиней и использовать Barrel.getMushrooms();
он говорит, не статический метод getMushrooms()
нельзя ссылаться из «хранить их.» статический контекст.
Но когда я попробовать использовать такие вещи, как
Barrel b1 = new Barrel();
b1.getMushrooms();
Мой счетчик с Shrooms никогда не работает правильно ..
Класс 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();
}
}
Класс 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();
}
}
}
@Bruce - Пожалуйста, прекратите предлагать изменения, у которых есть обратные образы, разбрызганные по всему. Backticks следует использовать только для встроенного кода. Редактирование, сделанное вами по этому вопросу, было * хорошим * использованием backticks, потому что вы использовали их для кода. Многие из ваших других изменений имеют обратные ссылки вокруг имен файлов и других вещей, которые им не нужны. Кроме того, не было необходимости в блоке цитат, который вы задали в этом вопросе. Все остальные могут игнорировать этот комментарий - я просто использую этот вопрос, чтобы предоставить некоторые отзывы редактору. – skrrgwasme
@skrrgwasme - спасибо за полезную информацию. – Bruce