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

2015/10/3

Game keeps pausing

1
2
Collin Collin

2015/10/3

#
Hi again, I am working on a two player mode, where 2 lobsters can eat fish and shoot alligators. but whenever an alligator gets shot, the game pauses, and you have to click the play button to resume. how do i get rid of this?
danpost danpost

2015/10/3

#
Maybe we should have a look at the alligator an shot classes.
danpost danpost

2015/10/3

#
danpost wrote...
Maybe we should have a look at the alligator an shot classes.
Correction: Maybe we should have a look at the alligator and shot classes.
Collin Collin

2015/10/4

#
Okay, so i have a blue and a red Lobster. Both Lobsters can fire bullets, so there are 2 classes, one for each Lobster. Here it goes: Code for Alligator:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)


/**
 * A lobster. Lobsters live on the beach. They like to eat crabs. (Well, in our game
 * they do...)
 * 
 * Version: 1
 * 
 * In this version, the lobster walks around randomly. If it runs into a crab it eats
 * it.
 */

public class WolpertingerDou extends DOUBLEPLAYER
{
    boolean touchingLobsterR = false;
    boolean touchingLobsterB = false;
    /**
     * Do whatever lobsters do.
     */
    
    public void act()
    {
        move(3);
        randomTurn();
        turnAtEdge();
        lookForLobsterR(); 
        lookForLobsterB();
    }

    /**
     * Check whether we are at the edge of the world. If we are, turn a bit.
     * If not, do nothing.
     */
    public boolean isAtEdge()
    {
        if (getX()>= getWorld().getWidth()-1 || getX() <=0 || 
        getY()>= getWorld().getHeight()-1 ||   getY()<=0)
        {
            return true;
        }
        else {
            return false;
        }
    }
    public void checkKeypress()
    {
         if (Greenfoot.isKeyDown("w")) 
        {
            move(5);
        }
        if (Greenfoot.isKeyDown("s")) 
        {
            turn(-5);
        }
        if (Greenfoot.isKeyDown("d")) 
        {
            turn(5);
        }
    }
    public void turnAtEdge() {
        if(isAtEdge()) {
            turn(180);    
        }
    }
    

    
    /**
     * Randomly decide to turn from the current direction, or not. If we turn
     * turn a bit left or right by a random degree.
     */
    public void randomTurn()
    {
        if (Greenfoot.getRandomNumber(100) > 90) 
        {
            turn(Greenfoot.getRandomNumber(90)-45);
            move(8);
        }
    }

    /**
     * Try to pinch a crab. That is: check whether we have stumbled upon a crab.
     * If we have, remove the crab from the game, and stop the program running.
     */
    public void lookForLobsterR()
    {
        if ( isTouching(LobsterR.class) ) 
        {
            World myWorld = getWorld();
            removeTouching(LobsterR.class);   
            getWorld().addObject(new LobsterR(), Greenfoot.getRandomNumber(getWorld().getWidth()),         Greenfoot.getRandomNumber(getWorld().getHeight()));
            Greenfoot.playSound("au.wav");
            Greenfoot.delay(50);
            DoubleWater doublewater = (DoubleWater)myWorld;
            HealthBarR healthbarR = doublewater.getHealthBarR();
            if(touchingLobsterR == false)
            {
                healthbarR.loseHealth();
                touchingLobsterR = true;
                if(healthbarR.health <=0)
                {
                   healthbarR.loseHealth();
                   VictoryB victoryB = new VictoryB();
                   myWorld.addObject(victoryB, myWorld.getWidth()/2, myWorld.getHeight()/2);
                   Greenfoot.delay(150);
                   Greenfoot.stop();
                   Greenfoot.setWorld(new Water());
                }
            }
            
           
        } else {
            touchingLobsterR = false;
        }
    }
    
    public void lookForLobsterB()
    {
        if ( isTouching(LobsterB.class) ) 
        {
            World myWorld = getWorld();
            removeTouching(LobsterB.class);
            Greenfoot.delay(50);
            getWorld().addObject(new LobsterB(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            Greenfoot.playSound("au.wav");
            
            DoubleWater doublewater = (DoubleWater)myWorld;
            HealthBarB healthbarB = doublewater.getHealthBarB();
            if(touchingLobsterB == false)
            {
                healthbarB.loseHealth();
                touchingLobsterB = true;
                if(healthbarB.health <=0)
                {
                   healthbarB.loseHealth();
                   VictoryB victoryB = new VictoryB();
                   myWorld.addObject(victoryB, myWorld.getWidth()/2, myWorld.getHeight()/2);
                   Greenfoot.delay(150);
                   Greenfoot.stop();
                   Greenfoot.setWorld(new DoubleWater());
                }
            }
            
           
        } else {
            touchingLobsterB = false;
        }
    }
    
    

}                       
 
Code for Red Lobster Bullet:
import greenfoot.*;

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BulletDouR extends DOUBLEPLAYER
{
    boolean touchingWolpertingerDou = false;
    boolean touchingLobsterB = false;
    private int life = 30;
    
    /** The damage this bullet will deal */
    private int damage = 1;
    
    public BulletDouR()
    {
    }
    
    public BulletDouR(Vector speed, int rotation)
    {
        super(speed);
        setRotation(rotation);
        increaseSpeed(new Vector(rotation, 15));
        Greenfoot.playSound("GunShot.mp3");
    }
   
    /**
     * The bullet will damage Alligators if it hits them.
     */
    public void act()
    {   
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
        if(getWorld() == null) return;
        move();
        World myWorld = getWorld();
        DoubleWater doublewater = (DoubleWater)getWorld();       
        lookForWolpertingerDou(); 
        lookForLobsterB();
        if(getWorld() == null) return;
    } 
    
    public boolean isAtEdge()
    {
        World myWorld = getWorld();
        DoubleWater doublewater = (DoubleWater)getWorld();
        if (getX()>= getWorld().getWidth()-1 || getX() <=0 || 
        getY()>= getWorld().getHeight()-1 ||   getY()<=0)
        {
            return true;
        }
        else {
            return false;
        }
    }
    
    public void lookForWolpertingerDou()
    {
        if(isTouching(WolpertingerDou.class))
        {
            World myWorld = getWorld();
            removeTouching(WolpertingerDou.class);
            Greenfoot.delay(10);
            getWorld().addObject(new WolpertingerDou(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            if(getWorld() == null) return;
            DoubleWater doublewater = (DoubleWater)myWorld;
            if(touchingWolpertingerDou == false)
            {
                
                touchingWolpertingerDou = true;
                getWorld().removeObject(this);
                        
            }
        }
        else {
            touchingWolpertingerDou = false;            
        }
        
    }   
    
    
    public void lookForLobsterB()
    {
        if ( isTouching(LobsterB.class) ) 
        {
            World myWorld = getWorld();
            removeTouching(LobsterB.class);
            getWorld().addObject(new LobsterB(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            if(getWorld() == null) return;
            Greenfoot.playSound("au.wav");
            Greenfoot.delay(50);
            DoubleWater doublewater = (DoubleWater)myWorld;
            HealthBarB healthbarB = doublewater.getHealthBarB();
            if(touchingLobsterB == false)
            {
                healthbarB.loseHealth();
                touchingLobsterB = true;
                if(getWorld() == null) return;
                if(healthbarB.health <=0)
                {
                   healthbarB.loseHealth();
                   VictoryB victoryB = new VictoryB();
                   myWorld.addObject(victoryB, myWorld.getWidth()/2, myWorld.getHeight()/2);
                   Greenfoot.delay(150);
                   Greenfoot.stop();
                   Greenfoot.setWorld(new DoubleWater());
                }
            }           
        } 
         else 
        {
            touchingLobsterB = false;
        }
       } 
}

Code for blue Lobster Bullet:
import greenfoot.*;

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BulletDouB extends DOUBLEPLAYER
{
    boolean touchingWolpertingerDou = false;
    boolean touchingLobsterR = false;
    private int life = 30;
    
    /** The damage this bullet will deal */
    private int damage = 1;
    
    public BulletDouB()
    {
    }
    
    public BulletDouB(Vector speed, int rotation)
    {
        super(speed);
        setRotation(rotation);
        increaseSpeed(new Vector(rotation, 15));
        Greenfoot.playSound("GunShot.mp3");
    }
   
    /**
     * The bullet will damage Alligators if it hits them.
     */
    public void act()
    {   
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
        if(getWorld() == null) return;
        move();
        World myWorld = getWorld();
        DoubleWater doublewater = (DoubleWater)getWorld();       
        lookForWolpertingerDou(); 
        lookForLobsterR();
        if(getWorld() == null) return;
    } 
    
    public boolean isAtEdge()
    {
        World myWorld = getWorld();
        DoubleWater doublewater = (DoubleWater)getWorld();
        if (getX()>= getWorld().getWidth()-1 || getX() <=0 || 
        getY()>= getWorld().getHeight()-1 ||   getY()<=0)
        {
            return true;
        }
        else {
            return false;
        }
    }
    
    public void lookForWolpertingerDou()
    {
        if(isTouching(WolpertingerDou.class))
        {
            World myWorld = getWorld();
            removeTouching(WolpertingerDou.class);        
            Greenfoot.delay(10);
            getWorld().addObject(new WolpertingerDou(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            DoubleWater doublewater = (DoubleWater)myWorld;
            if(getWorld() == null) return;
            if(touchingWolpertingerDou == false)
            {
                
                touchingWolpertingerDou = true;
                getWorld().removeObject(this);
                         
            }
        }
        else {
            touchingWolpertingerDou = false;            
        }
        
    }   
    
    public void lookForLobsterR()
    {
        if (isTouching(LobsterR.class)) 
        {
            World myWorld = getWorld();
            removeTouching(LobsterR.class);
            getWorld().addObject(new LobsterR(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            Greenfoot.playSound("au.wav");
            Greenfoot.delay(50);
            DoubleWater doublewater = (DoubleWater)myWorld;
            HealthBarR healthbarR = doublewater.getHealthBarR();
            if(touchingLobsterR == false)
            {
                healthbarR.loseHealth();
                touchingLobsterR = true;
                if(healthbarR.health <=0)
                {
                   healthbarR.loseHealth();
                   VictoryB victoryB = new VictoryB();
                   myWorld.addObject(victoryB, myWorld.getWidth()/2, myWorld.getHeight()/2);
                   Greenfoot.delay(150);
                   Greenfoot.stop();
                   Greenfoot.setWorld(new DoubleWater());
                }
            }
            
           
        } else {
            touchingLobsterR = false;
        }
        
    }
}


Thanks in advance!
danpost danpost

2015/10/4

#
Lines 107 and 140 of the WolpertingerDou class cause the scenario to stop. Remove them -- and if you want to continue in the same world, remove the 'setWorld' lines that follow them. You also have that same 'stop' line in both the lobster classes.
Collin Collin

2015/10/4

#
That didn't work, the game still pauses when an Alligator gets hit
danpost danpost

2015/10/4

#
Collin wrote...
That didn't work, the game still pauses when an Alligator gets hit
Maybe you should publish (share) the scenario on this site checking the 'Publish source code' checkbox so that we can see what is going on.
Collin Collin

2015/10/4

#
Okay so here is everything you need. World Class, Blue Lobster class, Red Lobster class, Blue Lobster Bullet class and Red Lobster Bullet class. That's a lot of code, dude. I really admire you for being able to look through it all and immediately being able to tell what the problem is!
import greenfoot.*;

/**
 * Write a description of class Water here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DoubleWater extends World
{
    ScoreBoardR scoreboardR = new ScoreBoardR();
    ScoreBoardB scoreboardB = new ScoreBoardB();
    ScoreBoardWR scoreboardWR = new ScoreBoardWR();
    ScoreBoardWB scoreboardWB = new ScoreBoardWB();
    HealthBarR healthbarR = new HealthBarR();
    HealthBarB healthbarB = new HealthBarB();
    SpeedBarR speedbarR = new SpeedBarR();
    SpeedBarB speedbarB = new SpeedBarB();
    LobsterR lobsterR = new LobsterR();
    LobsterB lobsterB = new LobsterB();
    HealthLobsterDou healthDou = new HealthLobsterDou();
    BoostLobsterDou boostLDou = new BoostLobsterDou();
    AmmoLobsterDou ammoDou = new AmmoLobsterDou();
    HealthLobsterDou2 healthDou2 = new HealthLobsterDou2();
    BoostLobsterDou2 boostLDou2 = new BoostLobsterDou2();
    AmmoLobsterDou2 ammoDou2 = new AmmoLobsterDou2();
    AmmoBarR ammobarR = new AmmoBarR();
    AmmoBarB ammobarB = new AmmoBarB();
    Fish fish = new Fish();
    Fish fish2 = new Fish();
    Fish fish3 = new Fish();
    WolpertingerDou wolpertingerDou = new WolpertingerDou();
    WolpertingerDou wolpertingerDou2 = new WolpertingerDou();
    /**
     * Constructor for objects of class DoubleWater.
     * 
     */
    public DoubleWater()
    {    
        super(1000, 600, 1); 
        prepare();     
    }

    public ScoreBoardR getScoreBoardR()
    {
        return scoreboardR;
    } 
    
    public ScoreBoardB getScoreBoardB()
    {
        return scoreboardB;
    } 
    
    public ScoreBoardWB getScoreBoardWB()
    {
        return scoreboardWB;
    } 
    
    public ScoreBoardWR getScoreBoardWR()
    {
        return scoreboardWR;
    } 
    
    public HealthBarR getHealthBarR()
    {
        return healthbarR;
    }
    
    public HealthBarB getHealthBarB()
    {
        return healthbarB;
    }
    
    public SpeedBarR getSpeedBarR()
    {
        return speedbarR;
    }
    public SpeedBarB getSpeedBarB()
    {
        return speedbarB;
    }
    
    public AmmoBarR getAmmoBarR()
    {
        return ammobarR;
    }
    
    public AmmoBarB getAmmoBarB()
    {
        return ammobarB;
    }
    
    public void prepare()
    {       
       addObject(scoreboardR, 400, 500); 
       addObject(scoreboardB, 600, 500);
       addObject(speedbarR, 300, 40);
       addObject(speedbarB, 700, 40);
       addObject(boostLDou, 700, 40);
       addObject(boostLDou2, 300, 40);
       addObject(healthbarB, 900, 40);
       addObject(healthbarR, 100, 40);
       addObject(healthDou, 100, 40);
       addObject(healthDou2, 900, 40);
       addObject(ammobarB, 700, 80);
       addObject(ammobarR, 300, 80);
       addObject(ammoDou, 700, 80);
       addObject(ammoDou2, 300, 80);
       addObject(lobsterB, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
       addObject(lobsterR, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
       addObject(wolpertingerDou, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
       addObject(wolpertingerDou2, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
       addObject(fish, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
       addObject(fish2, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));             
       addObject(fish3, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }
}

import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
/**
 * A lobster. Lobsters live on the beach. They like to eat crabs. (Well, in our game
 * they do...)
 * 
 * Version: 1
 * 
 * In this version, the lobster walks around randomly. If it runs into a crab it eats
 * it.
 */

public class LobsterB extends DOUBLEPLAYER
{
    private int gunBeloadTime; 
    private int fishDelayCount;
    private int reloadDelayCount;           
    private Vector acceleration;          
    private int shotsFired;
    Vector vector = new Vector();
    public LobsterB()
    {
        World myWorld= getWorld();
        DoubleWater doublewater = (DoubleWater)myWorld;
        
        gunBeloadTime = 2;
        reloadDelayCount = 0;
        acceleration = new Vector(0, 0.3);    
        shotsFired = 0;
    }
    
    public void act()
    {    
        checkKeypress();
        move(5);
        lookForFish();      
        reloadDelayCount++;
    }
    public void setGunBeloadTime(int reloadTime)
    {
        gunBeloadTime = reloadTime;
    }
    public int getShotsFired()
    {
        return shotsFired;
    }
    
    private void fire() 
    {
        if (reloadDelayCount >= gunBeloadTime) {
            BulletDouB bDouB = new BulletDouB(getMovement().copy(), getRotation());
            getWorld().addObject(bDouB, getX(), getY());
            bDouB.move(20);
            shotsFired++;
            reloadDelayCount = 0; 
            if(getWorld() == null) return;
        }
    }

    /**
     * Check whether we are at the edge of the world. If we are, turn a bit.
     * If not, do nothing.
     */
    public  boolean  isAtEdge()
    {
        if (getX()>= getWorld().getWidth()-1 || getX() <=0 || 
        getY()>= getWorld().getHeight()-1 ||   getY()<=0)
        {
            return true;
        }
        else {
            return false;
        }
    }
 
        
    /**
     * Bandomly decide to turn from the current direction, or not. If we turn
     * turn a bit left or right by a random degree.
     */
    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("q")) 
        {
                World myWorld= getWorld();
                DoubleWater doublewater = (DoubleWater)myWorld;
                SpeedBarB speedbarB = doublewater.getSpeedBarB();              
                if(speedbarB.speed >=0)
                {
                    move(3);
                    speedbarB.loseSpeed(); 
                }
                
                if(speedbarB.speed <=0)
                {
                    move(0);
                }
        }
         if (Greenfoot.isKeyDown("a")) 
        {
            turn(-5);
        }
        if (Greenfoot.isKeyDown("s")) 
        {
            turn(5);
        }
        String key = Greenfoot.getKey();
        if ("w".equals(key))
        {
            World myWorld= getWorld();
            DoubleWater doublewater = (DoubleWater)myWorld;
            AmmoBarB ammobarB = doublewater.getAmmoBarB();
            if(ammobarB.ammo > 0)
            {
                fire();
                ammobarB.loseAmmo();
                if(getWorld() == null) return;
            }
            else 
            {  
                ErrorMessage errormessage = new ErrorMessage();
                myWorld.addObject(errormessage, 500, 300);
                Greenfoot.delay(50);
                doublewater.removeObject(errormessage);
            }
        }
        if (Greenfoot.isKeyDown("n")) 
        {
            Greenfoot.setWorld(new Water());
        }
        if(Greenfoot.isKeyDown("p"))
        {
            Greenfoot.setWorld(new SingleWater());
        }
         if (Greenfoot.isKeyDown("down")) 
        {
            move(0);
        }
    }
    
    /**
     * Try to pinch a fish. That is: check whether we have stumbled upon a fish.
     * If we have, remove the fish from the game, and stop the program running.
     */
    public void lookForFish()
    {
        
        if ( isTouching(Fish.class) ) 
        {
            World myWorld= getWorld();
            removeTouching(Fish.class);
            DoubleWater doublewater = (DoubleWater)myWorld;
            SpeedBarB speedbarB = doublewater.getSpeedBarB();
            speedbarB.addSpeed();
            ScoreBoardB scoreboardB = doublewater.getScoreBoardB();
            AmmoBarB ammobarB = doublewater.getAmmoBarB();
            scoreboardB.addScore();
            ammobarB.addAmmo();
            Greenfoot.playSound("slurp.wav");
            getWorld().addObject(new Fish(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
/**
 * A lobster. Lobsters live on the beach. They like to eat crabs. (Well, in our game
 * they do...)
 * 
 * Version: 1
 * 
 * In this version, the lobster walks around randomly. If it runs into a crab it eats
 * it.
 */

public class LobsterR extends DOUBLEPLAYER
{
    private int gunReloadTime; 
    private int fishDelayCount;
    private int reloadDelayCount;           
    private Vector acceleration;          
    private int shotsFired;
    Vector vector = new Vector();
    public LobsterR()
    {
        World myWorld= getWorld();
        DoubleWater doublewater = (DoubleWater)myWorld;
        
        gunReloadTime = 2;
        reloadDelayCount = 0;
        acceleration = new Vector(0, 0.3);    
        shotsFired = 0;
    }
    
    public void act()
    {    
        checkKeypress();
        move(5);
        lookForFish();      
        reloadDelayCount++;
    }
    public void setGunReloadTime(int reloadTime)
    {
        gunReloadTime = reloadTime;
    }
    public int getShotsFired()
    {
        return shotsFired;
    }
    
    private void fire() 
    {
        if (reloadDelayCount >= gunReloadTime) {
            BulletDouR bDouR = new BulletDouR(getMovement().copy(), getRotation());
            getWorld().addObject(bDouR, getX(), getY());
            if(getWorld() == null) return;
            bDouR.move(20);
            shotsFired++;
            reloadDelayCount = 0; 
            
        }
    }

    /**
     * Check whether we are at the edge of the world. If we are, turn a bit.
     * If not, do nothing.
     */
    public  boolean  isAtEdge()
    {
        if (getX()>= getWorld().getWidth()-1 || getX() <=0 || 
        getY()>= getWorld().getHeight()-1 ||   getY()<=0)
        {
            return true;
        }
        else {
            return false;
        }
    }
 
        
    /**
     * Randomly decide to turn from the current direction, or not. If we turn
     * turn a bit left or right by a random degree.
     */
    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("up")) 
        {
                World myWorld= getWorld();
                DoubleWater doublewater = (DoubleWater)myWorld;
                SpeedBarR speedbarR = doublewater.getSpeedBarR();              
                if(speedbarR.speed >=0)
                {
                    move(3);
                    speedbarR.loseSpeed(); 
                }
                
                if(speedbarR.speed <=0)
                {
                    move(0);
                }
        }
         if (Greenfoot.isKeyDown("left")) 
        {
            turn(-5);
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            turn(5);
        }
        String key = Greenfoot.getKey();
        if ("space".equals(key))
        {
            World myWorld= getWorld();
            DoubleWater doublewater = (DoubleWater)myWorld;
            AmmoBarR ammobarR = doublewater.getAmmoBarR();
            if(ammobarR.ammo > 0)
            {
                fire();
                ammobarR.loseAmmo();
            }
            else 
            {  
                ErrorMessage errormessage = new ErrorMessage();
                myWorld.addObject(errormessage, 500, 300);
                Greenfoot.delay(50);
                doublewater.removeObject(errormessage);
            }
        }
        if (Greenfoot.isKeyDown("n")) 
        {
            
            Greenfoot.setWorld(new Water());
        }
        if(Greenfoot.isKeyDown("q"))
        {
            
            Greenfoot.setWorld(new SingleWater());
        }
         if (Greenfoot.isKeyDown("down")) 
        {
            move(0);
        }
    }
    
    /**
     * Try to pinch a fish. That is: check whether we have stumbled upon a fish.
     * If we have, remove the fish from the game, and stop the program running.
     */
    public void lookForFish()
    {
        
        if ( isTouching(Fish.class) ) 
        {
            World myWorld= getWorld();
            removeTouching(Fish.class);
            DoubleWater doublewater = (DoubleWater)myWorld;
            SpeedBarR speedbarR = doublewater.getSpeedBarR();
            speedbarR.addSpeed();
            ScoreBoardR scoreboardR = doublewater.getScoreBoardR();
            AmmoBarR ammobarR = doublewater.getAmmoBarR();
            scoreboardR.addScore();
            ammobarR.addAmmo();
            Greenfoot.playSound("slurp.wav");
            getWorld().addObject(new Fish(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
        }
    }
}
import greenfoot.*;

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BulletDouB extends DOUBLEPLAYER
{
    boolean touchingWolpertingerDou = false;
    boolean touchingLobsterR = false;
    private int life = 30;
    
    /** The damage this bullet will deal */
    private int damage = 1;
    
    public BulletDouB()
    {
    }
    
    public BulletDouB(Vector speed, int rotation)
    {
        super(speed);
        setRotation(rotation);
        increaseSpeed(new Vector(rotation, 15));
        Greenfoot.playSound("GunShot.mp3");
    }
   
    /**
     * The bullet will damage Alligators if it hits them.
     */
    public void act()
    {   
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
        if(getWorld() == null) return;
        move();
        World myWorld = getWorld();
        DoubleWater doublewater = (DoubleWater)getWorld();       
        lookForWolpertingerDou(); 
        lookForLobsterR();
        if(getWorld() == null) return;
    } 
    
    public boolean isAtEdge()
    {
        World myWorld = getWorld();
        DoubleWater doublewater = (DoubleWater)getWorld();
        if (getX()>= getWorld().getWidth()-1 || getX() <=0 || 
        getY()>= getWorld().getHeight()-1 ||   getY()<=0)
        {
            return true;
        }
        else {
            return false;
        }
    }
    
    public void lookForWolpertingerDou()
    {
        if(isTouching(WolpertingerDou.class))
        {
            World myWorld = getWorld();
            removeTouching(WolpertingerDou.class);        
            Greenfoot.delay(10);
            getWorld().addObject(new WolpertingerDou(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            DoubleWater doublewater = (DoubleWater)myWorld;
            if(getWorld() == null) return;
            if(touchingWolpertingerDou == false)
            {
                
                touchingWolpertingerDou = true;
                getWorld().removeObject(this);
                         
            }
        }
        else {
            touchingWolpertingerDou = false;            
        }
        
    }   
    
    public void lookForLobsterR()
    {
        if (isTouching(LobsterR.class)) 
        {
            World myWorld = getWorld();
            removeTouching(LobsterR.class);
            getWorld().addObject(new LobsterR(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            Greenfoot.playSound("au.wav");
            Greenfoot.delay(50);
            DoubleWater doublewater = (DoubleWater)myWorld;
            HealthBarR healthbarR = doublewater.getHealthBarR();
            if(touchingLobsterR == false)
            {
                healthbarR.loseHealth();
                touchingLobsterR = true;
                if(healthbarR.health <=0)
                {
                   healthbarR.loseHealth();
                   VictoryB victoryB = new VictoryB();
                   myWorld.addObject(victoryB, myWorld.getWidth()/2, myWorld.getHeight()/2);
                   Greenfoot.delay(150);               
                   Greenfoot.setWorld(new DoubleWater());
                }
            }
            
           
        } else {
            touchingLobsterR = false;
        }
        
    }
}

import greenfoot.*;

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BulletDouR extends DOUBLEPLAYER
{
    boolean touchingWolpertingerDou = false;
    boolean touchingLobsterB = false;
    private int life = 30;
    
    /** The damage this bullet will deal */
    private int damage = 1;
    
    public BulletDouR()
    {
    }
    
    public BulletDouR(Vector speed, int rotation)
    {
        super(speed);
        setRotation(rotation);
        increaseSpeed(new Vector(rotation, 15));
        Greenfoot.playSound("GunShot.mp3");
    }
   
    /**
     * The bullet will damage Alligators if it hits them.
     */
    public void act()
    {   
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
        if(getWorld() == null) return;
        move();
        World myWorld = getWorld();
        DoubleWater doublewater = (DoubleWater)getWorld();       
        lookForWolpertingerDou(); 
        lookForLobsterB();
        if(getWorld() == null) return;
    } 
    
    public boolean isAtEdge()
    {
        World myWorld = getWorld();
        DoubleWater doublewater = (DoubleWater)getWorld();
        if (getX()>= getWorld().getWidth()-1 || getX() <=0 || 
        getY()>= getWorld().getHeight()-1 ||   getY()<=0)
        {
            return true;
        }
        else {
            return false;
        }
    }
    
    public void lookForWolpertingerDou()
    {
        if(isTouching(WolpertingerDou.class))
        {
            World myWorld = getWorld();
            removeTouching(WolpertingerDou.class);
            Greenfoot.delay(10);
            getWorld().addObject(new WolpertingerDou(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            if(getWorld() == null) return;
            DoubleWater doublewater = (DoubleWater)myWorld;
            if(touchingWolpertingerDou == false)
            {
                
                touchingWolpertingerDou = true;
                getWorld().removeObject(this);
                        
            }
        }
        else {
            touchingWolpertingerDou = false;            
        }
        
    }   
    
    
    public void lookForLobsterB()
    {
        if ( isTouching(LobsterB.class) ) 
        {
            World myWorld = getWorld();
            removeTouching(LobsterB.class);
            getWorld().addObject(new LobsterB(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            Greenfoot.playSound("au.wav");
            Greenfoot.delay(50);
            DoubleWater doublewater = (DoubleWater)myWorld;
            HealthBarB healthbarB = doublewater.getHealthBarB();
            if(touchingLobsterB == false)
            {
                healthbarB.loseHealth();
                touchingLobsterB = true;
                if(getWorld() == null) return;
                if(healthbarB.health <=0)
                {
                   healthbarB.loseHealth();
                   VictoryB victoryB = new VictoryB();
                   myWorld.addObject(victoryB, myWorld.getWidth()/2, myWorld.getHeight()/2);
                   Greenfoot.delay(150);                 
                   Greenfoot.setWorld(new DoubleWater());
                }
            }           
        } 
         else 
        {
            touchingLobsterB = false;
        }
       } 
}

Thanks again!
danpost danpost

2015/10/4

#
Your DOUBLEPLAYER class should also be included. However, I did not suggest that you copy/paste all your classes into the discussion thread. You can upload the entire scenario via the 'Share' feature. Just make sure the 'Publish source code' checkbox is checked.
Collin Collin

2015/10/4

#
Sorry i cant do that, if my teacher sees that, he'll fail me. Here's the DOUBLEPLAYER and Vector class:
import greenfoot.*;

/**
 * Write a description of class OverClass here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public abstract class DOUBLEPLAYER extends Actor
{
    private Vector movement = new Vector();
    
    private double x = 0;
    private double y = 0;
    
    public DOUBLEPLAYER()
    {
    }
    
    /**
     * Create new mover initialised with given speed vector.
     */
    public DOUBLEPLAYER(Vector speed)
    {
        movement = speed;
    }
    
    /**
     * Move forward one step. Direction and speed are dtermined by the 
     * internal movement vector. Movement wraps around at the world edges.
     */
    public void move() 
    {
        x = x + movement.getX();
        y = y + movement.getY();
        if(x >= getWorld().getWidth()) {
            x = 0;
        }
        if(x < 0) {
            x = getWorld().getWidth() - 1;
        }
        if(y >= getWorld().getHeight()) {
            y = 0;
        }
        if(y < 0) {
            y = getWorld().getHeight() - 1;
        }
        setLocation(x, y);
    }
    
    public void setLocation(double x, double y) 
    {
        this.x = x;
        this.y = y;
        super.setLocation((int) x, (int) y);
    }
    
    public void setLocation(int x, int y) 
    {
        setLocation((double) x, (double) y);
    }

    /**
     * Increase the current speed with the given vector.
     */
    public void increaseSpeed(Vector s) 
    {
        movement.add(s);
    }
    
    /**
     * Return the current movement.
     */
    public Vector getMovement() 
    {
        return movement;
    }
}
Vector:
import greenfoot.*;

/**
 * Write a description of class Vector here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Vector
{
    double dx = 0;
    double dy = 0;
    int direction = 0;
    double length;
    
    /**
     * Create a default vector initialised to zero.
     */
    public Vector()
    {
    }

    /**
     * Create a vector with given direction and length.
     */
    public Vector(int direction, double length)
    {
       this.length = length;
       this.direction = direction;
       dx = length * Math.cos(Math.toRadians(direction));
       dy = length * Math.sin(Math.toRadians(direction));    
    }

    /**
     * Set the direction of this vector.
     */
    public void setDirection(int direction) 
    {
        this.direction = direction;
        dx = length * Math.cos(Math.toRadians(direction));
        dy = length * Math.sin(Math.toRadians(direction));   
    }
   
    /**
     * Add another vector to this vector.
     */
    public void add(Vector other) 
    {
        dx += other.dx;
        dy += other.dy;    
        this.direction = (int) Math.toDegrees(Math.atan2(dy, dx));
        this.length = Math.sqrt(dx*dx+dy*dy);
    }   
    
    /**
     * Return the x offset of this vector.
     */
    public double getX() 
    {
        return dx;
    }
     
    /**
     * Return the y offset of this vector.
     */
    public double getY() 
    {
        return  dy;
    }
    
    /**
     * Return the current direction (in degrees).
     */
    public int getDirection() 
    {
        return direction;
    }
    
    /**
     * Return the current length of the vector.
     */
    public double getLength() 
    {
        return length;
    }
    
    /**
     * Create a copy of this vector.
     */
    public Vector copy() 
    {
        Vector copy = new Vector();
        copy.dx = dx;
        copy.dy = dy;
        copy.direction = direction;
        copy.length = length;
        return copy;
    }    
}

danpost danpost

2015/10/5

#
I far as I can tell, it is the 'stop' and/or 'setWorld' commands in your WolpertingerDou class. Please post its revised code.
Collin Collin

2015/10/5

#
Okay so apparently the game has a problem with both bullet classes. It gives me this error with both classes: "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed." Here's my WolpertingerDou code:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)


/**
 * A lobster. Lobsters live on the beach. They like to eat crabs. (Well, in our game
 * they do...)
 * 
 * Version: 1
 * 
 * In this version, the lobster walks around randomly. If it runs into a crab it eats
 * it.
 */

public class WolpertingerDou extends DOUBLEPLAYER
{
    boolean touchingLobsterR = false;
    boolean touchingLobsterB = false;
    /**
     * Do whatever lobsters do.
     */
    
    public void act()
    {
        move(3);
        randomTurn();
        turnAtEdge();
        lookForLobsterR(); 
        lookForLobsterB();
    }

    /**
     * Check whether we are at the edge of the world. If we are, turn a bit.
     * If not, do nothing.
     */
    public boolean isAtEdge()
    {
        if (getX()>= getWorld().getWidth()-1 || getX() <=0 || 
        getY()>= getWorld().getHeight()-1 ||   getY()<=0)
        {
            return true;
        }
        else {
            return false;
        }
    }
    public void checkKeypress()
    {
         if (Greenfoot.isKeyDown("w")) 
        {
            move(5);
        }
        if (Greenfoot.isKeyDown("s")) 
        {
            turn(-5);
        }
        if (Greenfoot.isKeyDown("d")) 
        {
            turn(5);
        }
    }
    public void turnAtEdge() {
        if(isAtEdge()) {
            turn(180);    
        }
    }
    

    
    /**
     * Randomly decide to turn from the current direction, or not. If we turn
     * turn a bit left or right by a random degree.
     */
    public void randomTurn()
    {
        if (Greenfoot.getRandomNumber(100) > 90) 
        {
            turn(Greenfoot.getRandomNumber(90)-45);
            move(8);
        }
    }

    /**
     * Try to pinch a crab. That is: check whether we have stumbled upon a crab.
     * If we have, remove the crab from the game, and stop the program running.
     */
    public void lookForLobsterR()
    {
        if ( isTouching(LobsterR.class) ) 
        {
            World myWorld = getWorld();
            removeTouching(LobsterR.class);
            getWorld().addObject(new LobsterR(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            Greenfoot.playSound("au.wav");
            Greenfoot.delay(50);
            DoubleWater doublewater = (DoubleWater)myWorld;
            HealthBarR healthbarR = doublewater.getHealthBarR();
            if(touchingLobsterR == false)
            {
                healthbarR.loseHealth();
                touchingLobsterR = true;
                if(healthbarR.health <=0)
                {
                   healthbarR.loseHealth();
                   VictoryB victoryB = new VictoryB();
                   myWorld.addObject(victoryB, myWorld.getWidth()/2, myWorld.getHeight()/2);
                   Greenfoot.delay(150);                  
                   Greenfoot.setWorld(new DoubleWater());
                }
            }           
        } else {
            touchingLobsterR = false;
        }
    }
    
    public void lookForLobsterB()
    {
        if ( isTouching(LobsterB.class) ) 
        {
            World myWorld = getWorld();
            removeTouching(LobsterB.class);
            Greenfoot.delay(50);
            getWorld().addObject(new LobsterB(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            Greenfoot.playSound("au.wav");
            
            DoubleWater doublewater = (DoubleWater)myWorld;
            HealthBarB healthbarB = doublewater.getHealthBarB();
            if(touchingLobsterB == false)
            {
                healthbarB.loseHealth();
                touchingLobsterB = true;
                if(healthbarB.health <=0)
                {
                   healthbarB.loseHealth();
                   VictoryR victoryR = new VictoryR();
                   myWorld.addObject(victoryR, myWorld.getWidth()/2, myWorld.getHeight()/2);
                   Greenfoot.delay(150);                   
                   Greenfoot.setWorld(new DoubleWater());
                }
            }
            
           
        } else {
            touchingLobsterB = false;
        }
    }
    
    

}                        
danpost danpost

2015/10/5

#
Collin wrote...
Okay so apparently the game has a problem with both bullet classes. It gives me this error with both classes: "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed."
Switch the last two lines in the act methods of your bullet classes (it, for one thing, does not make sense to place a conditional return at a location where a return will take place anyway). If the 'lookForWolpertingerDou' method removes the actor, you need to exit the act method before calling the 'lookForLobster*' method.
Collin Collin

2015/10/5

#
How would I exit the act method? I assume I would somehow have to get back in the act method too, to continue.
danpost danpost

2015/10/5

#
Collin wrote...
How would I exit the act method? I assume I would somehow have to get back in the act method too, to continue.
If some condition requires you to leave the act method before the last statement it contains, then the 'return' statement can be used. Otherwise, the end of the method forces an automatic return. While your project is running, the greenfoot framework will automatically call the act method of the active world and the act method for all Actor objects within that world repeatedly.
There are more replies on the next page.
1
2