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

2017/4/27

How to get/call method from touching object

Casper Casper

2017/4/27

#
I'm making a game, if player touches bonus block(BonusBlock.class), it should call a method for spawning random bonus from the touching object. I already have this method in the BonusBlock class. That's my code:
if(isTouching(BonusBlock.class)){ 
         //calling method from the touching block
        }
how can I get and call any method from the touching object?
danpost danpost

2017/4/27

#
Replace what you gave above with this:
BonusBlock bb = (BonusBlock)getOneIntersectingObject(BonusBlock.class);
if (bb != null) bb.methodName(); // call the method here
Casper Casper

2017/4/27

#
Thanks
You need to login to post a reply.