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

2015/1/28

Remove Object, in two different layers

Tyllier Tyllier

2015/1/28

#
I have no compiling problem, but as i play the game, the goldenNutSmall won't dissappear, eventhough my Squirrelis in the correct layer. The Squirrel can move forward and backwards, the z value. If it is in the back layer, the layer value is over 1, and therefore my goldenNutSmall should remove itself as it touches the Squirrel, but it doesn't. Can anyone help me with this? World (Sworld)
    public Sworld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(660, 495, 1); 
        GreenfootImage background = getBackground();
        theSquirrel= new Squirrel();
    }
    public Squirrel getSquirrel()
    {
        return theSquirrel;
    }
}
Squirrel
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private int layer = 1;
    public void act() 
    {
        move();
        checkKeys();
        giveZ();
    }
    public int giveZ()
    {
        int Z = layer;
            return Z;
    }
    public Squirrel()
    {
        image1 = new GreenfootImage("egern1.png");
        image2 = new GreenfootImage("egern2.png");
        image3 = new GreenfootImage("egern3.png");
        setImage(image1);
    }  
    public void checkKeys()
    {
        movement.setLength(0.0);
        movement.setDirection(0);
        if (Greenfoot.isKeyDown("left"))
        {
            movement.setLength(10.0);
            movement.setDirection(180);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            movement.setLength(10.0);
            movement.setDirection(0);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setImage(image2);
            Greenfoot.delay(2);
            setImage(image3);
            layer = layer + 1;
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setImage(image2);
            Greenfoot.delay(2);
            setImage(image1);
            layer = layer - 1;
        }
    }

}
and goldenNutSmall
    public void act() 
    {
        move();
        fall();
        checkCollision();
    }    
    public void fall()
    {
           addForce(new Vector(90, 0.8));
    }
    public void checkCollision() 
    {
        Actor a = getOneIntersectingObject(Squirrel.class);
        if (a != null) 
        {
            Sworld sworldy = (Sworld) getWorld();
            Squirrel zsquirrel = sworldy.getSquirrel();
            int Z = zsquirrel.giveZ();
            if (Z > 1)
            {
                sworldy.removeObject(this);
            }
        }
    }
}
i copied only the important parts of Sworld and GoldenNutSmall, but the entirity of Squirrel. Thanks
danpost danpost

2015/1/28

#
Please supply the lines of code related to adding the squirrel into the world.
Tyllier Tyllier

2015/1/29

#
public class Sworld extends World
{
    /**
     * Constructor for objects of class Gameworld.
     * 
     */
    private Squirrel theSquirrel;
    public Sworld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(660, 495, 1); 
        GreenfootImage background = getBackground();
        theSquirrel= new Squirrel();
    }
    public Squirrel getSquirrel()
    {
        return theSquirrel;
    }
}
This is all i have in the world. The squirrel is being created manually, is this a mistake, and how do i fix it? I'm quite new to this Greenfoot-programming
danpost danpost

2015/1/29

#
The field 'theSquirrel' will not hold the instance you created manually; it will hold the one created programmatically in your Sworld class which is never added to the world and whose 'layer' value never changes. Line 13 in your GoldenNutSmall class already gets a reference to the squirrel in the world (if an intersecting one is found; however, it is only typed as an Actor. You can change the 'checkCollision' method to this:
public void checkCollision() 
{
    Squirrel a = (Squirrel) getOneIntersectingObject(Squirrel.class);
    if (a != null && a.giveZ() > 1) getWorld().removeObject(this);
}
which is much more direct.
Tyllier Tyllier

2015/1/29

#
I have solved it myself. A very simple mistake. The corrections are:
public class Sworld extends World
{
    /**
     * Constructor for objects of class Gameworld.
     * 
     */
    private Squirrel theSquirrel;
    public Sworld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(660, 495, 1); 
        GreenfootImage background = getBackground();
        theSquirrel= new Squirrel();
        addObject(theSquirrel, 330, 415);
    }
    public Squirrel getSquirrel()
    {
        return theSquirrel;
    }
}
I simply forgot to add theSquirrel to the world. Sorry for the inconvenience.
Tyllier Tyllier

2015/1/29

#
I thank you for you help @danpost. Your solution is working brilliantly too.
You need to login to post a reply.