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

2018/11/15

Contact during the whole time if object1 via object2

Bolla13 Bolla13

2018/11/15

#
I want whoever a lobster sees a crab to only get one life ripped off. The problem is that the crab "crawls" over the lobster and has about 5 times contact = lifetime is deducted. So a query anzLeben == 0 does not lead to death only if you set anzLeben initially to approx. 10. I tried to work with a variable hit = true. Unfortunately I can't get the logic under control. Either the crab is still eaten immediately or not at all :-( Here's my code try: I have declared a variable boolean hit = true; In the method public void lookforcrab() { If (canSee (Crab.class) && hits) { Crab crab1 = (Crab) getOneObjectAtOffset (0,0,Crab.class); If (crab1 == null) { hit = false; } If (hit) { krabbe1.lifetime(-1); } If (Crab.anzLife == 0) { Eat(Crab.class); Greenfoot.playSound("au.wav") Greenfoot.stop(); } } }
danpost danpost

2018/11/15

#
Bolla13 wrote...
I want whoever a lobster sees a crab to only get one life ripped off. The problem is that the crab "crawls" over the lobster and has about 5 times contact = lifetime is deducted.
Your solution might work (with some minor adjustments) if not for two issues. The first one is that you probably have multiple lobsters in the world. A boolean field like hits would only detect (a) crab intersect any, but unknown, lobster; or (b) crab not touching any at all. That is, it does not track which lobster is intersecting. So, if a 2nd one touches while one is already intersecting, it will not do any damage (lose life points). Best to use a List object to track all touching lobsters. The second issue deals with the animation of the crab. The two images used are of different sizes. It is therefore possible that a lobster that is very close to the crab may alternate between touching and not touching, repeatedly, act step to act step. A solution to this issue is to only check collision when a specific image is set to the actor. So, you would have this (in Crab class):
/** instance List field */
java.util.List touching = new java.util.ArrayList<>();

/** in act (or collision method */
if (getImage() == image1)
{
    List updated = getIntersectingObjects(Lobster.class);
    touching.retainAll(updated); // removes lobsters no longer touching
    updated.removeAll(touching); // retains newly touching lobsters
    if (!updated.isEmpty())
    {
        adjustLives(-updated.size());
        Greenfoot.playSound("au.wav");
        touching.addAll(updated); // combines still touching with newly touching
    }
}
You need to login to post a reply.