JoC #27: Object interaction – a second look

      Daring ideas are like chessmen moved forward; they may be beaten,
      but they may start a winning game.
            — Goethe

In this and the next episode, we’re looking a bit more into how to set up object structures, and how to get them to interact. This is one of the most fundamental concepts of object-oriented programming, so you need to get your head around this.

We discuss this here with the example of integrating our counter object into a scenario.

Download video

Concepts discussedobject interaction, object references, score counting, constructors, fields

Download:  (the scenario at the end of this episode)

9 thoughts on “JoC #27: Object interaction – a second look

  1. Pingback: JOC#26: Working with Text -

  2. Thanks for a great tutorial. One question, when I followed the JOC #27 video and tried to add the counter, my counter immediately started counting even before I released the ball. After trying to find the answer, with no luck, I downloaded your file So that I could compare the code line by line.

    In the video at the 13.48 mark you explain to add the “counter.addScore()” to the paddle class.

    However when I look in the code that I downloaded from here, this line is missing and when I remove the line from my code my counter suddenly works.
    Am I missing something here?

    Thanks again for a really god tutorial.

  3. I think there is a misunderstanding here somewhere.
    The “counter.addScore()” line is added (in the video) to the Ball class (in the checkPaddle method), not to the Paddle class. And I just downloaded the “” scenario (from this page, above) and the line is indeed there as it should. Without this line, no counting would happen.

    My guess is that you added this line at the wrong place. Maybe in a wrong class, or if it was in the checkPaddle method in Ball, then maybe a line too low: outside the if-statement.


  4. Hello Michael,
    first: Thank you for your great videos!
    I have a little question: Is it possible to get a reference to an object by using getOneIntersectingObject()? I’m trying to call an object’s method and I can’t figure out why this doesn’t work. Maybe you can explain how it works.

  5. @George Yes, it is. In fact, getOneIntersectingObject() is one of the most common methods to get a reference if you don’t already have one. It should give you a reference for an object you’re touching.

    I’ll do a video about this soon. Until then: The problem is most likely casting. You need to cast to the actor type you are expecting. E.g.

    Cake myCake = (Cake) getOneIntersectingObject(Cake.class);

    Casting (that’s the bit in the parantheses in the line above) is one of the most tricky aspects of Java. I’ll do a special video about that as well. (It might be called something like “polymorphism” or “subclassing”, since that’s all connected.)

  6. Hello Mik,
    thank your for your quick answer, it works pretty well!
    Grüße aus Deutschland 🙂

  7. Whenever I try compiling something with the object interaction it gives me the syntax error that I can’t reference it from a static context. Does it have to do with calling the method from a private or public method? Respond soon.

Leave a Reply

Your email address will not be published. Required fields are marked *