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

2013/1/12

make lobster fatter

ctgreenfoot ctgreenfoot

2013/1/12

#
hi, I want to make the lobster a tiny bit fatter each time he eats the crab. how do i do this?
Gevater_Tod4711 Gevater_Tod4711

2013/1/12

#
You could change his image like this:
//in your lobster class;

public void act() {
    if (wormEaten()) {
        getImage().scale(getImage().getWidth()*110, getImage().getHeight()*110);
    }
}

//or you can use this method:
//you call the method like this: scaleImage(getImage(), 110); or any other size (in percent);
public void scaleImage(GreenfootImage img, int size) {
    img.scale((img.getWidth()*size/100), (img.getHeight()*size/100));
}
ctgreenfoot ctgreenfoot

2013/1/12

#
its says found void but expected boolean
Gevater_Tod4711 Gevater_Tod4711

2013/1/12

#
could you post your code?
ctgreenfoot ctgreenfoot

2013/1/12

#
import greenfoot.*;

public class Lobster extends Animals
{
    public Lobster()
    {
    }
    
    public void act() 
    {
        moveAround();
        eatCrab();
        if (eatCrab())
            getImage().scale(getImage().getWidth()*110, getImage().getHeight()*110);  
    }  
Gevater_Tod4711 Gevater_Tod4711

2013/1/12

#
your method eatCrab() is a void but your are using it like a boolean because you used it in the if statement you could use a boolean variable for this or (what would be easyer) you could just delete the if statement and ad the code in line 14 to the eatCrab method.
ctgreenfoot ctgreenfoot

2013/1/12

#
i tried deleting the if statement and adding the code but now what happens is that when the lobster eats the crab, greenfoot stops and the crab goes back to its original position. here is my code:
import greenfoot.*;

public class Lobster extends Animals
{
    public Lobster()
    {
    }

    public void act() 
    {
        moveAround();
        eatCrab();

    }  

    private void moveAround()
    {
        move(5);
        if (Greenfoot.getRandomNumber (100) < 10)
        {
            turn(Greenfoot.getRandomNumber (90)-45);
        }
        if (atWorldEdge())
        {
            turn(180);
        }
    }

    public boolean atWorldEdge()
    {
        if (getX() <= 5 || 
        getX() >= getWorld() . getWidth() -5 ||
        getY() <= 5 ||
        getY() >= getWorld() . getHeight() -5)
            return true;
        return false;
    }

    private void eatCrab()
    {
        Actor crab;
        crab = (Crab)getOneObjectAtOffset (0,0,Crab.class);
        if (crab != null)
        {
            int x = Greenfoot.getRandomNumber(getWorld().getWidth());  
            int y = Greenfoot.getRandomNumber(getWorld().getHeight());
            crab.setLocation(x, y);
            getImage().scale(getImage().getWidth()*110, getImage().getHeight()*110);  
            Greenfoot.playSound ("eating.wav");

            

        }
    }
}
also a terminal error window comes up. please help!
Gevater_Tod4711 Gevater_Tod4711

2013/1/12

#
what is the exception you get in the terminal window?
ctgreenfoot ctgreenfoot

2013/1/12

#
java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41) at java.awt.image.Raster.createPackedRaster(Raster.java:458) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015) at java.awt.image.BufferedImage.<init>(BufferedImage.java:324) at apple.awt.CGraphicsConfig.createCompatibleImage(CGraphicsConfig.java:133) at greenfoot.util.GraphicsUtilities.createCompatibleTranslucentImage(GraphicsUtilities.java:179) at greenfoot.GreenfootImage.scale(GreenfootImage.java:403) at Lobster.eatCrab(Lobster.java:48) at Lobster.act(Lobster.java:12) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
Gevater_Tod4711 Gevater_Tod4711

2013/1/12

#
Ok that is probably not because of the method I posted. The problem in this case is that the heap space (heap is a part of the RAM) which is reserved by greenfoot is not enought. The bad thing is that you can't do anything against this Errors. The good thing is that greenfoot should work normaly after a restart.
MatheMagician MatheMagician

2013/1/12

#
Uh, actually Gevater_Tod4711, I think its because you misspelled your code. You wrote:
getImage().scale(getImage().getWidth()*110, getImage().getHeight()*110);    
where you meant to write:
getImage().scale((getImage().getWidth()*110/100), (getImage().getHeight()*110)/100);    
Your code was trying to increase the size of the lobster 110 times instead of just ten percent.
Gevater_Tod4711 Gevater_Tod4711

2013/1/12

#
oh yes. Sorry that was my fail. Thank you MatheMagician.
ctgreenfoot ctgreenfoot

2013/1/13

#
thank you both! it works now
You need to login to post a reply.