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

2012/12/16

Help with collisions

Hawx_ Hawx_

2012/12/16

#
I am creating a game where if the "Rocket" touches the "Mine" the game ends . Here's what I've got already,
    public void act() 
    {
        if (Greenfoot.isKeyDown("up"))
            move(3);

        if (Greenfoot.isKeyDown("down"))
            move(-3);

        if (Greenfoot.isKeyDown("right"))
            turn(3);

        if (Greenfoot.isKeyDown("left"))
            turn(-3);
    }    

But I need a code when on collision with "Mine" class , the Rocket dissapears and the game stops or ends. Please help. Thanks
Hawx_ Hawx_

2012/12/16

#
This is only in my "Rocket" class though.
Hawx_ Hawx_

2012/12/16

#
I've just tried this:
Actor rocket = getOneIntersectingObject(Rocket.class);
if(rocket != null) {
getWorld().removeObject(rocket);
But on line 3 it says "illegal start of type". I'm new to this so I don't know what it means.
danpost danpost

2012/12/16

#
Usually that error message will indicate that your bracketing is off (not that your code is wrong; which it does not appear to be -- other than missing a close bracket for the 'if' block).
Hawx_ Hawx_

2012/12/16

#
danpost wrote...
Usually that error message will indicate that your bracketing is off (not that your code is wrong; which it does not appear to be -- other than missing a close bracket for the 'if' block).
Thanks , just fixed it . But I need the game to end/reset when touching another class
Hawx_ Hawx_

2012/12/16

#
Any ideas?
danpost danpost

2012/12/16

#
To end the game, add the following (at the end of the same 'if' block):
Greenfoot.stop();
return;
To reset the game, the following should work
Greenfoot.setWorld(new WorldName());
return;
where you replace 'WorldName' with the name of your world sub-class. Of course, going this second route will not give the user (player) a chance to see their final score; as the new world will be instantiated immediately).
Hawx_ Hawx_

2012/12/16

#
Thanks, this was just what I was looking for:) Hawx_
Hawx_ Hawx_

2012/12/16

#
I now have this:
public void stopped()
    {
        Actor Rocket = getOneIntersectingObject(Rocket.class);
        if (Rocket != null) 
            Greenfoot.stop();
        return;

    }
But when my Rocket hits a mine , nothing happens the class name of the mine is "Mine", I dont see why it isn't working, Please help.
Hawx_ Hawx_

2012/12/16

#
don't worry , I put the code into the Mine class and that did the trick!:) hawx
Hawx_ Hawx_

2012/12/16

#
The 1st version of the game is in my scenarios section of my profile, updates are coming soon.:) Hawx
You need to login to post a reply.