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

2012/12/17

Help With Information Passing Between Classes

marzukr marzukr

2012/12/17

#
I have a problem where I have three classes, Odesseus, flower, and circe, I have circe as an enemy where Odesseus has to get the flower so he can defeat circe or else the game is over, I need to know how I can do this, I don't know how I can get information that Odesseus has got the flower to circe so she knows not to end the game when in contact.
danpost danpost

2012/12/17

#
If you put an instance boolean field in the Odesseus class, called something like 'flowerTaken', and set it to 'true' when he contacts the flower, and add a method like the following
public boolean hasFlower()
{
    return flowerTaken;
}
Then in your circe class, when in contact with odesseus, you can use:
Odesseus odesseus=(Odesseus) getWorld().getObjects(Odesseus.class).get(0);
if (!odesseus.hasFlower())
{
    // code ending game
}
marzukr marzukr

2012/12/17

#
I need the game to be over if Odesseus doesn't have the flower.
marzukr marzukr

2012/12/17

#
Sorry, I am not too good with Greenfoot, how would I make the boolean instance?
marzukr marzukr

2012/12/17

#
marzukr wrote...
I need the game to be over if Odesseus doesn't have the flower.
I need this and I tried you previous code but when in contact with Circe, Odesseus didn't do anything (yes I replaced the comment you wrote with code)
danpost danpost

2012/12/17

#
Adding the instance boolean with the code above:
private boolean flowerTaken;

public boolean hasFlower()
{
    return flowerTaken;
}
You might want to show the method where you added the second code set above.
marzukr marzukr

2012/12/17

#
This is what I have in Odesseus:
public void help()
    {
        if(canSee(flower.class))
        {
            eat(flower.class);
            flowerTaken = true;
        }
    }
    private boolean flowerTaken = false;
    public boolean hasFlower()  
    {  
        return flowerTaken;  
    }  
And In circe class:
public void hi()
    {
        if(canSee(Odesseus.class))
        {
            Odesseus odesseus=(Odesseus) getWorld().getObjects(Odesseus.class).get(0);  
            if (!odesseus.hasFlower())  
            {  
                Greenfoot.setWorld(new gameover());
            }  
        }
    }
With this code it doesn't do anything, Oh and here is the canSee and eat code which is inherited from the special class that both Odesseus and Circe are under:
public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 20, clss);
        return actor != null;        
    }
    
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 20, clss);
        if(actor != null) 
        {
            getWorld().removeObject(actor);
        }
    }
Why is this not working, is there anything I am doing wrong?
danpost danpost

2012/12/17

#
Please post the act method in the circe class.
marzukr marzukr

2012/12/17

#
the act method in circe is hi(); thats all.
marzukr marzukr

2012/12/17

#
Okay, so it worked but how do I make it so that if I have the flower then when in contact with circe then she disappears?
danpost danpost

2012/12/17

#
I do not know if this would have anything to do with the issue, but the 'canSee' method (and the 'eat' method) checks for objects 20 cells below the center of the actor in question (which I am not sure if that is what you wanted). The 'hi' method checks for any intersecting object, which may or may not catch what is caught by the two aforementioned methods. If the image of the object in question is less than 40 pixels high in a world with pixel-sized cells, or if your world has cells that are larger than one pixel wide and high, one or the other method may trigger, but not both in the same circumstance. EDIT: ignore this, if you did fix it.
danpost danpost

2012/12/17

#
If you are going to a new gameover world, then there is no need to remove circe from the old world.
marzukr marzukr

2012/12/17

#
No, I want the game over world to appear if you don't have the flower and if you do circe disappear and it says in the current world.
danpost danpost

2012/12/17

#
Just tell it to remove itself if Odesseus has the flower.
public void hi()
{
    if(canSee(Odesseus.class))
    {
        Odesseus odesseus=(Odesseus) getWorld().getObjects(Odesseus.class).get(0);
        if (!odesseus.hasFlower())
        {
            Greenfoot.setWorld(new gameover());
        }
        else
        {
            getWorld().removeObject(this);
        }
    }
}
marzukr marzukr

2012/12/17

#
Thanks, this worked! (Sorry for replying 3 hours late)
You need to login to post a reply.