JoC #28: Return of the object interaction

      If life gives you lemons – keep them. Because, hey. Free lemons.
            — Anonymous

In this episode, we’re continuing from the last episode’s theme and investigate and compare an alternative structure for making our Ball and Counter objects cooperate. We also have a first brief look at casting.

Recognising alternatives is a good skill, and it slowly eases us into discussions about code quality, which we will get to in more detail later.

Download video

The download today (below) is the scenario as it is at the end of this episode. However, try to add a counter into one of your own scenarios to make sure you are fully taking in what’s going on here.

Concepts discussedobject interaction, casting, score counting

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

7 thoughts on “JoC #28: Return of the object interaction

  1. Thanks for the excellent video. I am trying to toggle the background image of an instance of World by clicking on an Actor button. This method seems like it may hold the answer, but I’m having problems with it. Am I on the right track?

  2. Pingback: El júbilo de programar – Videotutoriales | CyberHades

  3. also, if you could help me……… did what you did in the video, but i just need to transfer it to the first actor so i skipped the part about the second actor and then continued with the rest and i got a error about the person not knowing the variable “counter”… was there something in the part i skipped that was neccessary? my game is on the greenfoot website if you want to see it and i can give you the source code. the game is titled “first game” and my U-Name is CrazyGamer1122.

  4. Pingback: Servicios Web Gratis » El júbilo de programar – Videotutoriales

  5. JoC #28 return of the object interaction
    Tanks for a high quality teaching material 18km!
    In episode JoC #28 is used a casting method to get access to the score of the Board via the method getWorld() in order to loosen the overall coupling of references.
    The call score() consists of the following two lines:
    Board myBoard = (Board) getWorld(); // the (Board) is casting the type World to the type Board
    In order to understand the differences between the methods used in episode JoC #27 and #28 I have tried to make a mix of the two ideas.
    First I tried to create an instance variable in the Ball class to contain the reference to the Board.
    It did not work placing the call myBoard = (Board) getWorld(); in the constructor of the Ball. I received a runtime error when using the call
    later on in the method checkPaddle().
    My first idea, why this didn’t worked, was the order in which the objects are created, so perhaps the Board wasn’t created when the constructor of the Ball was run. But the further investigations showed me, that a reference to the World (Board) is null, whenever placed in the constructor of the Ball:
    My second idea was to use the myBoard.score() method in different methods in the Ball class, making it a local instance variable of the Ball, and only updating this variable the first time, it’s used.
    Now it worked.
    Further investigations showed me, that – (Board) getWorld(); – is returning null, whenever placed in the constructor of the Ball class.
    Last I checked if the return value of the getWorld(); depends on if the call is at runtime (while playing) or it is the first time called from the constructor of myBoard – as the last “act” of pressing the compile-button ;
    But – (Board) getWorld(); – always returns null when placed in the constructor of the Ball independent of the time it’s asked.
    Why is (Board) getWorld(); returning null when placed in the constructor of an actor?
    Niels Henrik Würtz
    Langkaer Gymnasium, DK

  6. Hi Niels,

    Good question!

    Actors can exist without being in the world. You can put them into the world, and take them out again.

    When you make a new actor, it is created first, and then (possibly) inserted into the world. While you are in the constructor, you are still in the middle of creating the actor. It hasn’t even been completely created yet, and certainly not been inserted into a world yet.

    While an actor is not in a world, ‘getWorld()’ will return null. (This indicates that it is not in any world. That’s not an error, it is a normal possible state.)

    There is help for what you want to do (one-time initialisation after the actor has been added to the world): There is a callback method called “addedToWorld(World world)”. This method will be called when the actor is added to a world.

    You can place the call to getWorld here (although you don’t need to – the world you’re being added to is also passed as a parameter). Put your initialisation here.

    You can find the method in the javadoc for Actor .


Leave a Reply

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