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

2015/1/9

Help needed Damage when touching another

Thanatos Thanatos

2015/1/9

#
I want my car to get damage when touching Actor A or B, but this don't work. It will only get damage when hitting actor A, but no damage with actor B. What am I doing wrong? public void carDamage () { Actor A = getOneIntersectingObject(A.class); Actor B = getOneIntersectingObject(B.class); if(isTouching(A.class)|| isTouching(B.class)) { healthbar.loseHealth(); } if(healthbar.health <=0) { GameOverScreen(); } }
danpost danpost

2015/1/9

#
Please bookend your code with code tags. Click on the link that says 'Posting code? read this!' for an explanation. The third and fourth lines (the one beginning with 'Actor' can be removed. The variables declared there are not being used in the method. The second 'if' block should be inside and at the end of the first 'if' block. You only need to check the value only after it decreases, not every act cycle. Other than that, is should still work as you had it, except that the health will drop in bunches. Each contact will probably last more than one act cycle and the health will decrease once per act cycle that the same intersection occurs. Will need further information (Actor class structure for one) and more testing (ensure actor you think is of the B class is really a B class object (right-click on object and select 'Inspect' from the pop-up menu; the title should show it as being a B type object). There may be other reason that health does not drop due to intersecting B objects (showing the B class code might be helpful). These should eliminate the most obvious reasons.
You need to login to post a reply.