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

2013/7/8

Pausing

I am making my entry for the Greenfoot dare and if I fire a shot, when said shot gets to the end of the world and removes itself, as it should do, the game pauses as though the main actor has died. Does anyone know why/ Here is the code
    
     move(+3);
                Actor Car1;
Car1 = getOneObjectAtOffset(0, 0, Car1.class);
if (Car1 != null)
{
World world;
world = getWorld();
world.removeObject(Car1);
}
          Actor Car2;
Car2 = getOneObjectAtOffset(0, 0, Car2.class);
if (Car2 != null)
{
World world;
world = getWorld();
world.removeObject(Car2);
}
          Actor Car3;
Car3 = getOneObjectAtOffset(0, 0, Car3.class);
if (Car3 != null)
{
World world;
world = getWorld();
world.removeObject(Car3);
}
          Actor Ambulance;
Ambulance = getOneObjectAtOffset(0, 0, Ambulance.class);
if (Ambulance != null)
{
World world;
world = getWorld();
world.removeObject(Ambulance);
}
int gapX = 10;
int gapY =8;
if (getX() >= getWorld().getWidth()-gapX || getX() <= gapX || getY() >= getWorld().getHeight()-gapY ||getY() <= gapY)
{
getWorld().removeObject(this);
}
               Actor Car;
Car = getOneObjectAtOffset(0, 0, Car.class);
if (Car != null)
{
World world;
world = getWorld();
world.removeObject(Car);
}
Can anyone help? The DoctorProfessor is out!
My guess is that you have the actor that shoots the bullet get hurt if a bullet hits it. But what if you have a bullet that hurts it, spawn right on it? The actor that shot it will get hurt. You'll have to specify both to the bullet and to the actor who shot it, and who should get hurt.
okay, the actor fires a bullet which hits the enemy. the enemy should then disappear and the actor should not be harmed in anyway. The DoctorProfessor is out!
What's the main actor? Car, Car1? And could you post the code of creating a bullet in the world?
no, none of the actors in the above code are the main actor. And the code for creating a bullet is this:
if ("space".equals(Greenfoot.getKey()))
{
    for (int i=0; i<1; i++)
    {
        PoisonNeedle2 poison = new PoisonNeedle2();
        getWorld().addObject(poison, getX(), getY());
        poison.move(3);
    }
}
The DoctorProfessor is out!
danpost danpost

2013/7/8

#
Move lines 41 through 48 to before line 35. You cannot use collision checking methods after the object is removed (see line 39).
There you go, like i said before, you're telling a poison dart that hurts ANY of the cars to spawn right in the center of one of them. So it destroys the car that created it. You'll have to put something in your bullet class that tells the bullet not to hurt the car that made it (I recommend putting a Car object in it's constructor). Also, unless you want it like this, you should make the bullet disappear after touching a car. And another smart idea: If you haven't already, you should make all the cars a subclass of one Car class, then you can have only one if statement for the destruction of cars:
Actor car = getOneIntersectingObject(Car.class); //Returns the same thing as getOneObjectAtOffset(0,0,Car.class) and it will return any car, not just the superclass Car
if (car != null)  
{  
World world = getWorld();  
world.removeObject(Car);  
world.removeObject(this);
} 
Also, with your shoot code, i recommend using the Greenfoot.isKeyDown() method, because getKey() often returns null, causing several problems if you don't know how to use it. And why do you have a for-loop that only iterates once?
danpost danpost

2013/7/8

#
@FlyingRabidUnicornPig, getKey is better in this case and is being used correctly (as far as I can tell with what is provided). Using isKeyDown will end up continuously producing poison objects as long as the key is held down. You are, however, correct about the use of 'for' to iterate once. The code will automatically execute one time without the use of the loop.
I have the for-loop because I got the code when trying to shoot four different objects in different directions. since I didn't know the basic code, I improvised. The DoctorProfessor is out!
Well, it's not necessary the way you're doing this so you can just get rid of the for-loop.
Well it works anyway.
Yah it works, but it's bad code writing and not necessary.
Well i didn't realise you could remove it without the code going funny. The DoctorPrrofessor is out!
You need to login to post a reply.