This site requires JavaScript, please enable it in your browser!
Greenfoot back
MuellerO
MuellerO wrote ...

2017/12/20

Actor doesnt act() after creation

MuellerO MuellerO

2017/12/20

#
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:
 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;
    }
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
danpost danpost

2017/12/21

#
I would, first off, think that line 4 would show an error and the project would therefore not compile. Also, line 15 looks like it may produce some errors during specific circumstances. It also looks like a single sheep could produce an abundance of new sheep when eating grass. Insufficient code was given to determine why you are getting your current behavior. Please show the code for the entire Sheep class.
You need to login to post a reply.