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

2015/5/8

Help with Asteroids

NickConnors NickConnors

2015/5/8

#
Part of my grade in my computer programming class is adding my own touch to the scenarios. Right now I am trying to add 3 lives in the asteroid scenario and have the Rocket completely stop after respawning. I have this working somewhat but am running into a few issues. The Rocket does spawn 3 times, and the game ends after the 3rd. Also, the Rocket does come to a complete stop, but ONLY when hit going directly up/down or left/right into an asteroid. If the rocket crashes into the asteroid sideways, it respawns at an uncontrollable speed. I'm not sure why it does this only when the rocket is sideways when intersecting with the asteroid. My Rocket class is almost exactly the same as the pre-made one in Asteroids-3 with the following additions: -added a private int variable called lives -added a private int called thrustDelay (prevents you from respawning at a very fast speed) -set the lives to 3 in the constructor -set the thrustDelay to 1000000000 in the constructor -changed the checkCollision to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void checkCollision()
    {
       Actor a = getOneIntersectingObject(Asteroid.class);
        if (a != null)
        {
            lives--;
            Space space = (Space) getWorld();
            space.addObject(new Explosion(), getX(), getY());
            setLocation(space.getWidth()/2, space.getHeight()/2);
            addForce(new Vector(getRotation(),-getSpeed()));
            thrustDelay=0;
        
            if (lives<=0)
            {
            space.removeObject(this);
            space.gameOver();
            }
        }
    }
I have no idea what could possibly be masking the rocket not come to a stop when intersecting with an asteroid sideways. To make sure it is not a mistake on part, those variables and the checkCollision are the only thing different from the pre-made Asteroid-3. If anyone could provide any insight it would be greatly appreciated. Thanks
Super_Hippo Super_Hippo

2015/5/8

#
As much as I can see, the rocket is never "respawned". If it should just stop, you could do something like 'speed=0;'. Maybe you should show how the moving is working. (I don't have the Asteriods scenario here and I have no idea what the Vector class is doing.)
NickConnors NickConnors

2015/5/8

#
Super_Hippo wrote...
As much as I can see, the rocket is never "respawned". If it should just stop, you could do something like 'speed=0;'. Maybe you should show how the moving is working. (I don't have the Asteriods scenario here and I have no idea what the Vector class is doing.)
Yeah, the rocket technically isn't "respawned." I'm just setting its location back to the middle. I'll quote the vector class soon for anyone curious.
You need to login to post a reply.