-3
我正試圖編程龍蝦可以被鵜鶘吃掉的東西,但鵜鶘在吃完龍蝦之後必須消失。什麼代碼會從世界中刪除鵜鶘?如何用Java中的方法刪除對象?
當前的(全)代碼:
import greenfoot.*;
/**
* Write a description of class Pelican here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Pelican extends Animal
{
/**
* Act - do whatever the Pelican wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private boolean lobsterEaten=false;
public void act()
{
randomTurn();
turnAtEdge();
lookForLobster();
move();
}
public void randomTurn()
{
if(Greenfoot.getRandomNumber(100)<10)
{
turn(Greenfoot.getRandomNumber(91)-45);
}
}
public void turnAtEdge()
{
if(atWorldEdge())
{
turn(17);
}
}
public void lookForLobster()
{
eat(Lobster.class);
}
}
動物代碼:
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;
/**
* Animal. This is the base class for all animals. In addition to the standard Actor
* methods, it provides the ability to move and turn.
*
* @author Michael Kolling
* @version 1.0
*/
public class Animal extends Actor
{
private static final double WALKING_SPEED = 5.0;
/**
* Constructor for Animal - nothing to do.
*/
public Animal()
{
}
/**
* Act - empty method. Animals have no default action.
*/
public void act()
{
}
/**
* Turn 'angle' degrees towards the right (clockwise).
*/
public void turn(int angle)
{
setRotation(getRotation() + angle);
}
/**
* Move forward in the current direction.
*/
public void move()
{
double angle = Math.toRadians(getRotation());
int x = (int) Math.round(getX() + Math.cos(angle) * WALKING_SPEED);
int y = (int) Math.round(getY() + Math.sin(angle) * WALKING_SPEED);
setLocation(x, y);
}
/**
* Test if we are close to one of the edges of the world. Return true is we are.
*/
public boolean atWorldEdge()
{
if(getX() < 20 || getX() > getWorld().getWidth() - 20)
return true;
if(getY() < 20 || getY() > getWorld().getHeight() - 20)
return true;
else
return false;
}
/**
* Return true if we can see an object of class 'clss' right where we are.
* False if there is no such object here.
*/
public boolean canSee(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
return actor != null;
}
/**
* Try to eat an object of class 'clss'. This is only successful if there
* is such an object where we currently are. Otherwise this method does
* nothing.
*/
public void eat(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
if(actor != null) {
getWorld().removeObject(actor);
}
}
}
請發佈代碼。 –
我不知道你在問什麼,或者你可能會遇到什麼問題。請查看[幫助]以及[如何提出良好問題](http://stackoverflow.com/help/how-to-ask)章節,以獲取有關如何改進問題和增加您的問題的更多信息有可能獲得體面的幫助。 –
你的意思是什麼*消失*?你的意思是所有提及的對象將被遺失嗎? – Blip