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

2014/3/3

Problems With Multiple Collisions?

allyand17 allyand17

2014/3/3

#
I am trying to get my game of Frogger to detect collisions with specific objects so it can react differently but when it collides with one of the objects (specifically one) it comes up with an error for one of the other collisions. I figured it was because it was trying to do what the other collisions too, but I'm not sure how to change it so the collisions are separate.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Frog extends Actor
{
    int delay = 0;
    int halfPixelMovement = 0;
    public void act()
    {
    }   
    public void movement()
    {
        if(Greenfoot.isKeyDown("up") && delay == 0) {
            setLocation(getX(), getY()-16);
            delay = 1;
        }
        if(Greenfoot.isKeyDown("down") && delay == 0) {
            setLocation(getX(), getY()+16);
            delay = 1;
        }
        if(Greenfoot.isKeyDown("left") && delay == 0) {
            setLocation(getX()-16, getY());
            delay = 1;
        }
        if(Greenfoot.isKeyDown("right") && delay == 0) {
            setLocation(getX()+16, getY());
            delay = 1;
        }
        if(delay < 10 && delay > 0) {
            delay = delay + 1;
        }
        if(delay == 10) {
            delay = 0;
        }
    }
    public void collide() 
    {
        Actor collided;
        collided = getOneIntersectingObject(Cerz.class);
        if (collided != null) {
            getWorld().removeObject(this);
            Greenfoot.stop();
        }
    }
    public void collideLogLeft1() 
    {
        Actor collided2;
        collided2 = getOneIntersectingObject(Log2.class);
        if (collided2 != null) {
            setLocation(getX()-halfPixelMovement, getY());
            if(halfPixelMovement == 0) {
                halfPixelMovement = halfPixelMovement + 1;
            } else if(halfPixelMovement == 1) {
                halfPixelMovement = halfPixelMovement - 1;
            }
        }
    }
    public void collideLogRight() 
    {
        Actor collided3;
        collided3 = getOneIntersectingObject(Log.class);
        if (collided3 != null) {
            setLocation(getX()+halfPixelMovement, getY());
            if(halfPixelMovement == 0) {
                halfPixelMovement = halfPixelMovement + 1;
            } else if(halfPixelMovement == 1) {
                halfPixelMovement = halfPixelMovement - 1;
            }
        }
    }
    public void collideWater() 
    {
        Actor collided4;
        collided4 = getOneIntersectingObject(Water.class);
        if (collided4 != null) {
            getWorld().removeObject(this);
            Greenfoot.stop();
        }
    }
}
danpost danpost

2014/3/3

#
Two of these collision methods (particularly 'collideWater' and 'collide' which checks for Cerz objects) have the possibility of removing the frog from the world. If one does remove the frog, the other cannot run properly because collision cannot be detected if the frog is not in the world (you were probably getting an 'IllegalStateException' error). To fix this, put a condition on calling the second one: 'if (getWorld != null) ...'.
allyand17 allyand17

2014/3/3

#
Easy fix, thanks!
You need to login to post a reply.