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

2017/5/9

I know the problem, just don't know how to fix it.

ET78759 ET78759

2017/5/9

#
I get a logical error when certain cars crash into each other because I use a variable that indicates when the victory effect should play in my game, Crash Course, and when cars crash, they use a method from the World class to add the variable in other classes. I don't know how to fix it, so if someone could help, I'd be grateful. I'm posting my code in separate posts or the website will crash.
ET78759 ET78759

2017/5/9

#
MyWorld:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author Edwin, Paul and Mike
 * @version 2.0
 */
public class MyWorld extends World
{
    private GreenRacecar greenracecar;
    private RedRacecar redracecar;
    private Racecar racecar;
    private Rocket rocket;

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 850x433 cells with a cell size of 1x1 pixels.
        super(850, 433, 1); 
        prepare();
    }
    
    public void greenRaceCar()
    {
        greenracecar.addExplosionCount();
    }
    
    public void raceCar()
    {
        racecar.addExplosionCount();
    }
    
    public void redRaceCar()
    {
        redracecar.addExplosionCount();
    }
    
    public void Rocket()
    {
        rocket.addExplosionCount();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        redracecar = new RedRacecar();
        addObject(redracecar,203,72);
        racecar = new Racecar();
        addObject(racecar,191,277);
        greenracecar = new GreenRacecar();
        addObject(greenracecar,207,176);
        redracecar.setLocation(201,110);
        greenracecar.setLocation(691,107);
        greenracecar.setLocation(518,165);
        racecar.setLocation(298,166);
        Rocket rocket = new Rocket();
        addObject(rocket,510,106);
    }
}
ET78759 ET78759

2017/5/9

#
Racecar:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Racecar here.
 * 
 * @author Edwin, Paul, Mike 
 * @version 2.0
 */
public class Racecar extends Actor
{

    public int explosionCount;
    /**
     * Act - do whatever the Wizard wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        crash();
        playSound();
    }    

    public void move()
    {

        if (Greenfoot.isKeyDown("up"))
        {
            move(4);
        } 
        if (Greenfoot.isKeyDown("down"))
        {
            move(-4);
        } 
        if (Greenfoot.isKeyDown("left"))
        {
            turn(4);
        } 
        if (Greenfoot.isKeyDown("right"))
        {
            turn(-4);
        } 
    }

    public void crash()
    {
        MyWorld world = (MyWorld) getWorld();
        Actor redRacecar;
        redRacecar = getOneObjectAtOffset(0,0, RedRacecar.class);
        Actor explosion;
        explosion = getOneObjectAtOffset(0,0, Explosion.class);
        if (redRacecar != null)
        {
            Greenfoot.playSound("explosion.wav");

            world.greenRaceCar();

            world.removeObject(redRacecar);
            addExplosion(getX(), getY());
            explosionCount++;
            world.Rocket();

        }
        Actor greenRacecar;
        greenRacecar = getOneObjectAtOffset(0,0, GreenRacecar.class);

        if (greenRacecar != null)
        {
            Greenfoot.playSound("explosion.wav");

            world.removeObject(greenRacecar);
            addExplosion(getX(), getY());
            explosionCount++;
            world.redRaceCar();
            world.Rocket();

        }
        
        Actor rocket;
        rocket = getOneObjectAtOffset(0,0, Rocket.class);
        if (rocket != null)
        {
            Greenfoot.playSound("explosion.wav");
            addExplosion(getX(), getY());


            world.removeObject(rocket);
            explosionCount++;
            world.redRaceCar();
            world.greenRaceCar();

        }
    }
    public void addExplosion(int X, int Y)
    {
        World world = getWorld();
        Explosion explosion = new Explosion();
        world.addObject(explosion, X, Y);
    }

    public void playSound()
    {
        if(explosionCount == 3)
        {
            Greenfoot.playSound("blueCarWins.wav");
            explosionCount++;
        }
    }

    public void addExplosionCount()
    {
        explosionCount++;
    }
}

ET78759 ET78759

2017/5/9

#
RedRacecar:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class RedRacecar here.
 * 
 * @author Edwin, Paul, Mike 
 * @version 2.0
 */
public class RedRacecar extends Actor
{
    public int explosionCount;
    /**
     * Act - do whatever the Wizard wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        crash();
        move();
        playSound();

    }    

    public void move()
    {
        if (Greenfoot.isKeyDown("W"))
        {
            move(4);
        } 
        if (Greenfoot.isKeyDown("S"))
        {
            move(-4);
        } 
        if (Greenfoot.isKeyDown("A"))
        {
            turn(4);
        } 
        if (Greenfoot.isKeyDown("D"))
        {
            turn(-4);
        } 

    }

    public void crash()
    {
        MyWorld world = (MyWorld) getWorld();
        Actor racecar;
        racecar = getOneObjectAtOffset(0,0, Racecar.class);
        Actor explosion;
        explosion = getOneObjectAtOffset(0,0, Explosion.class);
        if (racecar != null)
        {
            Greenfoot.playSound("explosion.wav");

            addExplosion(getX(), getY());

            world.removeObject(racecar);
            explosionCount++;
            world.greenRaceCar();
            world.Rocket();
            

        }

        Actor greenRacecar;
        greenRacecar = getOneObjectAtOffset(0,0, GreenRacecar.class);
        if (greenRacecar != null)
        {
            Greenfoot.playSound("explosion.wav");
            addExplosion(getX(), getY());


            world.removeObject(greenRacecar);
            explosionCount++;
            world.raceCar();
            world.Rocket();

        }
        
        Actor rocket;
        rocket = getOneObjectAtOffset(0,0, Rocket.class);
        if (rocket != null)
        {
            Greenfoot.playSound("explosion.wav");
            addExplosion(getX(), getY());


            world.removeObject(rocket);
            explosionCount++;
            world.raceCar();
            world.greenRaceCar();

        }
    }

    public void addExplosion(int X, int Y)
    {
        World world = getWorld();
        Explosion explosion = new Explosion();
        world.addObject(explosion, X, Y);
    }

    public void playSound()
    {
        if(explosionCount == 3)
        {
            Greenfoot.playSound("redCarWins.wav");
            explosionCount++;
        }
    }

    public void addExplosionCount()
    {
        explosionCount++;
    }
}
ET78759 ET78759

2017/5/9

#
GreenRacecar:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class GreenRacecar here.
 * 
 * @author Edwin, Paul, Mike 
 * @version 2.0
 */
public class GreenRacecar extends Actor
{
    public int explosionCount;
    /**
     * Act - do whatever the Wizard wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        crash();
        playSound();
    }    

    public void move()
    {
        if (Greenfoot.isKeyDown("I"))
        {
            move(4);
        } 
        if (Greenfoot.isKeyDown("K"))
        {
            move(-4);
        } 
        if (Greenfoot.isKeyDown("J"))
        {
            turn(4);
        } 
        if (Greenfoot.isKeyDown("L"))
        {
            turn(-4);
        } 

    }

    public void crash()
    {
        MyWorld world = (MyWorld) getWorld();
        Actor redRacecar;
        redRacecar = getOneObjectAtOffset(0,0, RedRacecar.class);
        Actor explosion;
        explosion = getOneObjectAtOffset(0,0, Explosion.class);
        if (redRacecar != null)
        {
            Greenfoot.playSound("explosion.wav");

            addExplosion(getX(), getY());

            world.removeObject(redRacecar);
            explosionCount++;
            world.raceCar();
            world.Rocket();

        }

        Actor racecar;
        racecar = getOneObjectAtOffset(0,0, Racecar.class);
        if (racecar != null)
        {
            Greenfoot.playSound("explosion.wav");
            addExplosion(getX(), getY());

            world.removeObject(racecar);
            explosionCount++;
            world.redRaceCar();
            world.Rocket();

        }
        
        Actor rocket;
        rocket = getOneObjectAtOffset(0,0, Rocket.class);
        if (rocket != null)
        {
            Greenfoot.playSound("explosion.wav");
            addExplosion(getX(), getY());


            world.removeObject(rocket);
            explosionCount++;
            world.raceCar();
            world.redRaceCar();

        }
    }

    public void addExplosion(int X, int Y)
    {
        World world = getWorld();
        Explosion explosion = new Explosion();
        world.addObject(explosion, X, Y);
    }

    public void playSound()
    {
        if(explosionCount == 3)
        {
            Greenfoot.playSound("greenCarWins.wav");
            explosionCount++;
        }
    }

    public void addExplosionCount()
    {
        explosionCount++;
    }
}

ET78759 ET78759

2017/5/9

#
Rocket:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class YellowRacecar here.
 * 
 * @author Edwin, Paul, Mike 
 * @version 2.0
 */
public class Rocket extends Actor
{
    public int explosionCount;
    /**
     * Act - do whatever the Wizard wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        crash();
        playSound();
    }    

    public void move()
    {
        if (Greenfoot.isKeyDown("5"))
        {
            move(4);
        } 
        if (Greenfoot.isKeyDown("2"))
        {
            move(-4);
        } 
        if (Greenfoot.isKeyDown("1"))
        {
            turn(4);
        } 
        if (Greenfoot.isKeyDown("3"))
        {
            turn(-4);
        } 

    }

    public void crash()
    {
        MyWorld world = (MyWorld) getWorld();
        Actor redRacecar;
        redRacecar = getOneObjectAtOffset(0,0, RedRacecar.class);
        Actor explosion;
        explosion = getOneObjectAtOffset(0,0, Explosion.class);
        if (redRacecar != null)
        {
            Greenfoot.playSound("explosion.wav");

            addExplosion(getX(), getY());

            world.removeObject(redRacecar);
            explosionCount++;
            world.raceCar();
            world.greenRaceCar();

        }

        Actor racecar;
        racecar = getOneObjectAtOffset(0,0, Racecar.class);
        if (racecar != null)
        {
            Greenfoot.playSound("explosion.wav");
            addExplosion(getX(), getY());

            world.removeObject(racecar);
            explosionCount++;
            world.redRaceCar();
            world.greenRaceCar();

        }
        
        Actor greenRacecar;
        greenRacecar = getOneObjectAtOffset(0,0, GreenRacecar.class);
        if (greenRacecar != null)
        {
            Greenfoot.playSound("explosion.wav");
            addExplosion(getX(), getY());

            world.removeObject(greenRacecar);
            explosionCount++;
            world.redRaceCar();
            world.raceCar();

        }
    }

    public void addExplosion(int X, int Y)
    {
        World world = getWorld();
        Explosion explosion = new Explosion();
        world.addObject(explosion, X, Y);
    }

    public void playSound()
    {
        if(explosionCount == 3)
        {
            Greenfoot.playSound("rocketWins.wav");
            explosionCount++;
        }
    }

    public void addExplosionCount()
    {
        explosionCount++;
    }
}

danpost danpost

2017/5/9

#
In the MyWorld class, remove the first word from line 63 ('Rocket'). In the four Actor subclasses, remove the two lines dealing with an Explosion object at around line 50 (an Explosion object is created in the 'addExplosion' method). The first change may have an effect on the game, but I fear it is not the main cause of your issue. I think that in the four methods in your MyWorld class between lines 27 and 45, you should only add to the explosion count if the actor is in the world. For example:
public void redRaceCar()
{
    if (redracecar.getWorld() == this) redracecar.addExplosionCount();
}
ET78759 ET78759

2017/5/10

#
Thanks, I did everything you suggested, and everything worked perfectly. Am currently uploading the latest version.
You need to login to post a reply.