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

2013/3/7

Calling methods from another actor

tehe321 tehe321

2013/3/7

#
I want to have a bullet hit an enemy and then call a method in the enemy's class to deal damage to it equal to its power. Anyone have any idea how to do this? Thanks
davmac davmac

2013/3/7

#
Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
if (enemy != null) {
    enemy.dealDamage(10);
}
tehe321 tehe321

2013/3/7

#
I tried that but it says it can't find the method.
davmac davmac

2013/3/7

#
Then you did it wrong :) Post your code.
Make sure you're Enemy class has a dealDamage(int) method in it.
tehe321 tehe321

2013/3/8

#
Ok I was using actor enemy = getOneIntersectingObject(); instead so I switched it and it works thanks :)
You need to login to post a reply.