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

2019/6/24

How to hit the object?

Jargy Jargy

2019/6/24

#
Hello. I'm sorry for my bad English. I need help. How can i realize hit? My current code is.... public void Hit() { List <Zombie1> zombies = getObjects(Zombie1.class); List <bullet> bullets = getObjects(bullet.class); for (Zombie1 zombie : zombies) { for (bullet bullet : bullets) { if ((zombie.getY() == bullet.getY()) && (zombie.getX() == bullet.getX())) { Greenfoot.stop(); } } } } I check all objects' coordinates. if it's equal then bullet hit zombie. My code isnt work. How can i realize it?
Super_Hippo Super_Hippo

2019/6/24

#
Easiest way is to check it in one of the classes. You can use the Actor's methods for intersection to check if it intersects with an object of the other class. So for example:
//in Bullet's act method
if (isTouching(Zombie1.class))
{
    Greenfoot.stop();
}
You need to login to post a reply.