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

2013/5/3

Need help on collision detection.

Laurynas281 Laurynas281

2013/5/3

#
SO I've created a game where my shooter(cat) fires ammo at a dog, but i'm having trouble making that once the ammo hits the dog he would be removed and respawn. Can anyone help with the code?
Gevater_Tod4711 Gevater_Tod4711

2013/5/3

#
If you click on the menu bar in your scenario, click on import class and import the class animal you will find some methods that will help you with this problem. (found and eat) If you want your dog to respawn at a random position you have to add this code in your world.
public void act() {
    if (getObjects(Dog.class).isEmpty()) {//If your class isn't called Dog you have to use the right classname;
        addObject(new Dog(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight());
    }
}
Laurynas281 Laurynas281

2013/5/3

#
I dont really want to make it spawn, i firstly want the ammo i have the cat shooting to detect the dog and make him disappear.
Laurynas281 Laurynas281

2013/5/3

#
public void act() { if (Greenfoot.isKeyDown("left")) { setRotation (getRotation() - 5); } if (Greenfoot.isKeyDown("right")) { setRotation (getRotation() + 5); } if ("space".equals(Greenfoot.getKey())) { fire(); } } private void fire() { CatAmmo catammo = new CatAmmo(); getWorld().addObject(catammo, getX(), getY()); catammo.setRotation(getRotation()); catammo.move(40.0); } } The Ammo is already working and shooting perfectly i just want it to detect the dog when it hits him
Gevater_Tod4711 Gevater_Tod4711

2013/5/3

#
Therefore your need the methods from the class animal like a already said.
cellostar47 cellostar47

2013/5/3

#
public dog(){ public void act() { collision(); } public void collision() { CatAmmo c = (CatAmmo) getOneIntersectingObject(CatAmmo.class); if(c!=null){ setLocation(x,y); //fill in x and y with the respawn point! } } } You can also add stuff to happen after the if(c!=null) part (under setLocation(x,y).
Laurynas281 Laurynas281

2013/5/3

#
public void act() << Illegal start of expression .. :/
Gevater_Tod4711 Gevater_Tod4711

2013/5/4

#
Can you show us the code you used?
cellostar47 cellostar47

2013/5/7

#
Make sure that you have your () correct. If you don't have enough before the act() method, it will give you that error.
davmac davmac

2013/5/7

#
public dog(){ public void act()
this is a method inside a method, which is what is causing the error. I think you meant:
public class Dog {
    public void act()
cellostar47 cellostar47

2013/5/7

#
I know why: public void act() is only recognized if public class Dog extends Actor do this:
public class Dog extends Actor{


public void act()
{

}

}
You need to login to post a reply.