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

2013/10/15

Need help with surface

Greenstudent Greenstudent

2013/10/15

#
Hello. Im remaking the game fishy for a school project. But I need help. The most important aspect of the game is that the bigger fish eats the smaller fish. But i cant get it to work so im asking you guys for help. I have a code where I check if my playerfish is intersecting with a computerfish. Then I want my playerfish to check whos bigger. Im trying to do this to see who has the biggest surface(width*heigth). But if i do: int opp=computervis.getWidth()computervis.getHeight(); he will give me the surface of all the objects in the world. How can i get the code to make it that it will only give me the surface of the intersecting object with my playerfish. I hope i explained it well enough in my bad english and that somebody can help me. p.s computervis is the name of the greenfootImage which I scale in a different method.
If you want to get the width of the actor, you need to get the image it's currently using, and the width and height of the image. You can do it like this:
int width = myFish.getImage().getWidth();
int height = myFish.getImage().getWitdh();
So then you aren't using you're reference fish image, you're using the image of the actors to get they're size.
RUMMAKER RUMMAKER

2013/10/16

#
i think you need to import java.util.List; you need the list because you can collide with more then 1 fish at a time. List<Fish> list = getIntersectingObjects(Fish.class); int size = list.size(); for(int n = 0; n < size; n++){ Fish f = list.get(n); //get f height and width and compair to ur fish's height and width if(//my fish is bigger then f) { getWorld().removeObject(f); } else { getWorld().removeObject(this); } }
danpost danpost

2013/10/16

#
@RUMMAKER, I do not think that is a problem (running into more than one fish) as if a collision with one finds it to be smaller, it will be removed and the next one will be detected on the next act cycle; if at any time one is found to be larger, then the game will end. So, either way, 'getOneIntersectingObject' should work.
private void checkForFish()
{
    Actor fish = getOneIntersectingObject(Fish.class);
    if (fish.getImage().getWidth()*fish.getImage().getHeight() <getImage().getWidth()*getImage().getHeight())
    {
        // increase my size
        getWorld().removeObject(fish);
    }
    else
    {
        // game over
        Greenfoot.stop();
    }
}
You need to login to post a reply.