vendredi 26 juin 2015

Java - Class.isInstance() always returns false

In my GameObject class I have the following method to check if the GameObject would be colliding with another object if it moved to the specified position:

public boolean collisionAt(Vector2d position, Class<? extends GameObject>... exclusions) {
    if (getBounds() == null)
        return false;
    Rectangle newBounds = getBounds().clone();
    newBounds.setPosition(position);
    // Check collisions
    for (GameObject object : new ArrayList<>(gameObjects)) {
        if (object.getBounds() != null && newBounds.intersects(object.getBounds()) && object != this) {
            boolean b = true;
            for (Class<? extends GameObject> exclusion : exclusions) {
                if (object.getClass().isInstance(exclusion))
                    b = false;
            }
            if (b)
                return true;
        }
    }
    return false;
}

I want to allow the program to define exclusions, for example if I don't want this method to return true if it collides with a Spell for example. But for some reason the Class.isInstance() line always returns false. I even tried this:

System.out.println(Spell.class.isInstance(Spell.class));

and the console outputs false! What's going on here?





Aucun commentaire:

Enregistrer un commentaire