Hey,
i have some trouble solving my programming problem. It's kinda easy, but i couldnt figure out why it does not work,
I have a Sheep class wich is moving around the world, the code for that is in the act()-method:
So the Sheep is moving around and can eat grass to get fat. if its to fat its producing a new sheep, because thats how its working. :)
My Problem is, the newly created sheep does not move like the first few wich are on the world at the creation.
Could anyony tell how i can start the act()-method for the newly created sheep so that it can move around?
I would appreciate every answer.
kind regards,
Mueller
public void act()
{
// Get a reference to the field that the sheep is living on.
Field theField = getWorld();
// Move the sheep to its new position.
int oldX = getX();
int oldY = getY();
int newX = oldX + dir.rightSteps;
int newY = oldY + dir.upSteps;
// Check if the new location is empty or not.
if(theField.isEmpty(newX, newY))
{
// New location is empty. Move there.
this.full = this.full -3;
setLocation(newX, newY);
} else
{
// New location is not empty.
// Check if there is a solid object at the new location.
// (Sheep and bricks are solid objects, grass is not a solid object in this exercise.)
if(theField.hasSolidAt(newX, newY))
{
// New location contains a solid object already.
// Change the direction of motion for sheep as if its reflecting from the solid at the new location.
bounceFromSolid(theField, oldX, oldY, newX, newY);
} else if(theField.hasGrassAt(newX, newY))
{
// New location does not contain a solid object.
// Instead, it contains grass.
// Move to this location.
setLocation(newX, newY);
// Eat the grass here.
theField.eatGrassAt(newX, newY);
this.full = this.full + 100;
}
} // end 'isEmpty' of if-else
if (full > 500) createNewSheep();
if (full <= 0) getWorld().removeObject(this);
} // end of act() private void createNewSheep(){
Sheep sheep = new Sheep();
getWorld().addObject(sheep, getX(), getY());
full = 100;
}
