Class Collision

edited June 2014 in How To...

Hey there,

Was working on my super mario game and I am quite confused how to go about collision detection between 2 classes. This is because when I enter the xpos of a class and the xpos of the other, one will be unrecognizable because it is in another class.

Any ideas?

Answers

  • This online example got class Bullet receiving an Asteroid object in method checkHit():

    http://studio.processingtogether.com/sp/pad/export/ro.9K-kjhuONcJDZ/latest

  • There is no confusion, as you must prefix each field with the name of the object they belong to. Or, if you are in a class, the field without prefix belongs to the current class:

    if (mario.xpos > mushroom.xpo) // ...
    

    or in the Mario class:

    if (xpos > mushroom.xpos) // ...
    
  • edited June 2014

    Thankyou! :)

    }
    if (xPos == Goomba.xpos && yPos == Goomba.ypos) {
      GAMEOVER = true;
    }
    

    I wrote this in the mario class in void move() and it says cannot make a static reference to the non-static field Goomba.xpos, any ideas?

  • Anyone? Extremely urgent D:

  • edited June 2014

    You need a variable holding the reference for a Goomba object in order to access its members!
    Take a look at checkHit() from my online example link above! 3:-O

  • Don't mix up objects (instances of classes, created with new), and classes (the declaration / blueprint of all objects).

    In Java, you can call ClassName.method() or use ClassName.field, but only for a special kind of methods and fields, called static methods / fields. And this style isn't "natural" in Processing.

Sign In or Register to comment.