This site requires JavaScript, please enable it in your browser!
Greenfoot back
rick_kik
rick_kik wrote ...

2017/3/31

How to call for a method in an intersecting object?

rick_kik rick_kik

2017/3/31

#
Hi everyone, I want to make a game where you have an object called Ball and an object called Brick, and when the ball hits a brick, a variable called life needs to be lowered by one. This is the code I have right now:
1
2
3
4
Actor blok = getOneIntersectingObject(Brick.class);
if (blok != null) {
blok.leven--;
           }
Nosson1459 Nosson1459

2017/3/31

#
That creates an actor object which doesn't have the life variable in it. There are TWO things you should do:
  • Change the first line to:
    1
    Brick blok = (Brick) getOneIntersectingObject(Brick.class);
  • Say what your problem first so that I don't have to figure it out.
rick_kik rick_kik

2017/4/1

#
My problem was that it gave me an error at the blok.leven--; part. it said it couldnt find the variable leven. But I changed the first line and it works perfectly now! Thanks a lot for your help!
You need to login to post a reply.