Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In my class, I extend Sprite class. I override one of the method collideWith(). But my problem was that when current sprite collide with other sprite it does not call the call back method collideWith(). Here is my implementation.

@Override
public boolean collidesWith(IShape pOtherShape) {
    System.out.println("collide with called");
    return true;
}

Any member please help me.

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

This is not a callback, you have to call the method yourself to find out whether the Sprites collide. By overriding it you broke its functionality. What you want to do (I think) is override the onUpdate method of the Sprite and put there an if statement calling the collidesWith(otherSprite) method.

share|improve this answer
    
So what it the advantage to override this method in out user define class. –  Siddharth Oct 2 '12 at 8:20
    
There is no reason to override the method collidesWith unless you want to handle the collision checking all by yourself. Overriding onUpdate is just the way of achieving what you want, it will run user-defined code on every step and you're free to do whatever you want with it. Just make the body of onUpdate slim, because this code will run very often and could slow down you application. –  JohnEye Oct 2 '12 at 8:26
    
If you override a method, you redefine his behavior. By overriding, you redefine (i suppose) the collision detection process. In java, you can use the "super" operator and do something like super.collideWidth(...) –  nathan Oct 2 '12 at 8:36
add comment

Just to clarify a bit.

First read about about callbacks.

If you have to call it yourself, it's probably not a callback.

In java, you can override a method (maybe you should read about OO concepts) like you did:

@Override
public void foo() {
}

The method foo is override in a custom class that probably extends another class (or implements an interface but in this case you have to override all the methods of the interface since they are abstract. Read more about interfaces here.) that contains a foo method itself.

class ParentClass {

    public void foo() {
        //do something
    }

Since overriding a method mean "redefining" his behavior, if you call the foo method from your custom class extending ParentClass it will (in this case) not do anything. If you want to reach a method from a parent class, use super.

@Override
public void foo() {
    super.foo(); //will call the foo method of the parent class
    //add custom code here
}

Also you should read this great tutorial covering inherence in Java: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.