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

2016/5/7

Actor not taking damage properly

redteam55 redteam55

2016/5/7

#
Basically, I have a method in the class FriendlyProjectile called getDamaged() that returns the damage which is set in a subclass with a method in FriendlyProjectile. Then, in my GoalMover class, there is a line of code that goes like if(isTouching(FriendlyProjectile.class)) { health = health-friendlyprojectile.getDamage(); } However, at the same time, in my FriendlyProjectile class, i have another method that goes like this if(isTouching(GoalMover.class) || hitEdge()) { getWorld().addObject(projectile,getX(),getY()); getWorld().removeObject(this); } which basically just removes the projectile if it hits a GoalMover or the edge of the world. However, right now, the damage that is being returned from getDamage() is always coming to be 0 because I THINK, when the FriendlyProjectile hits a Goalmover, it is removed before the GoalMover can call upon the getDamage() method, can someone help me and tell me what i can do here?
danpost danpost

2016/5/7

#
Actually, you should only check for interaction between two objects once. Having both check for touching each other only causes problems. You can add the following after reducing health:
removeTouching(FriendlyProjectile.class);
and change the 'if' line in the FriendlyProjectile class to:
if (hitEdge())
Now, I cannot tell how your fields are assigned values -- particularly 'health', 'friendlyProjectile' and 'projectile', and I cannot say why you are adding a 'projectile' into the world when touching the edge; so, if you are still having difficulties with this, please include this extra information.
redteam55 redteam55

2016/5/7

#
OHHHH thank you, for the fast reply and the help!
You need to login to post a reply.