JoC #15: Object interaction (first encounter)

      The pure and simple truth is rarely pure and never simple.
            — Oscar Wilde

Today a long, and quite full episode. We encounter various very important concepts for the first time: local variables, class types and object interaction.

If you have questions left after watching this – don’t worry. We will look at all of this again in more detail in future episodes. And you can use the comments below to ask your questions (or make any other comments).

By the way, I just noticed I made a mistake. (Yes, that does happen!) There is a bug in the program that I’m writing in this episode. Can you spot it?

Download video

Concepts discussedvariable, field, local variable, assignment, class type, object interaction, external method call

16 thoughts on “JoC #15: Object interaction (first encounter)

  1. Michael, you should test if the points are equal or more than 15. Otherwise the program could miss the goal.
    Example: Two bugs and a lettuce are 11 points together. If you eat another bug it will be 16 and never trigger the action.

    if (points >= 15) {

  2. @Andreas – You’re right – well spotted! That’s a bug. I only noticed that after the video was done (and I was too lazy to remake it).

    You can actually see this happening late in the video, where the game should stop and it doesn’t…

  3. how would i delete an object if its at the edge?

    right now i’m trying to write the method within the actual object class. I have an atWorldEdge() method and i reference the world with the getWorld() function. The problem happens when I try to use the removeObject() method. From my understanding, I would need an object reference to the object running the method – is there any way to do that?

  4. Pingback: Joy Of Code #15 – Object Interaction -

  5. Hi Mik,

    I was working through this episode tonight and couldn’t find the bug you used so selected an Ant instead. Interestingly they have a strong tendency towards the edges. Why would this happen?

    Thanks again for the amazing tutorials – hope you don’t mind that I’ve embedded them into my site and added notes for future reference.



  6. Hi Mik,

    Technically speaking, would it be better to take the moving code from the ‘Snake’ class and put it into ‘Animal’. Then make ‘Bug’ extend animal?

    Just wanted to ask. Cheers

  7. When I create a new Bug how would you suggest I could have it face a random direction?

    Right now I have “turn(Greenfoot.getRandomNumber(360));” when it’s created but that turns the Turtle (which is a nice twist to the game), and I’m not sure how to go about changing the code to make it so the new Bug is turned.

  8. @Aidan Just put the same statement into the Bug class.

    For this, you need to write a constructor in the Bug class, and then put your statement into it. That should do it.

  9. hey mik

    I hope you wil read this

    in the video i saw you can eat a bug and the bug wil come immidally on anouther place on the screem but is it possible that if you eat the bug, it wil cost maby 10 seconds and than the bug wil come on the sreem

    (sorry for my bat english)


  10. @martijn Thanks for the question – I’ll cover the answer in a soon-to-be-released video. Keep your eyes open for this!


  11. Hello, I am making a game where I shoot a projectile and it hits a fly. I used this method so every time I shoot the fly one more should appear but the problem is, right when I press the shoot button multiple flies appear at once even before I kill the fly that I placed in the world. I checked and i have the createNewFly(); in the same method when the fly gets removed from the world.

    public void shoot()

    Actor fly;
    fly = getOneObjectAtOffset(0, 0, fly.class);
    if (fly != null)
    World world;
    world = getWorld();

  12. Brad, your code looks fine. It’s hard to say where the problem is without seeing the rest of it. Perhaps you are calling the createNewFly() method from elsewhere too? Ot perhaps that method creates more than one fly?

  13. Hi Mik,

    I’m just wondering in a design-philosophical sense whether a turtle object should be deciding whether or not to and then adding bug objects to the world. That seems like something the World object should do. The turtle could just tell the world that it ate a bug and then the world can decide whether or not to add a new bug and then do so.


  14. Dear Michael,

    Thanks a lot for these videos. I had to watch it many times AND take notes. But in the end I understood.

    I’m a visual learner and all the text books are text and code examples, whereas Greenfoot shows a class diagram and the game action. It’s just a very very good way to learn for visual learners.

    Thanks again.

Leave a Reply

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