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/5

#
Now it tells me "lookForLobsterR" is an "unreachable statement"
public void act()
    {              
        move();
        World myWorld = getWorld();
        DoubleWater doublewater = (DoubleWater)getWorld();       
        lookForWolpertingerDou();
        return;
        lookForLobsterR();
}
davmac davmac

2015/10/5

#
It is unreachable, because you have an unconditional return statement on line 7. You only want to return from act() if lookForWolpertingerDou actually removed this object from the world. One easy way to check that is to see if getWorld() returns null:
public void act()
{              
    move();
    World myWorld = getWorld();
    DoubleWater doublewater = (DoubleWater)getWorld();       
    lookForWolpertingerDou();
    if (getWorld() == null) {
        return;
    }
    lookForLobsterR();
}
You could also write it as:
public void act()
{              
    move();
    World myWorld = getWorld();
    DoubleWater doublewater = (DoubleWater)getWorld();       
    lookForWolpertingerDou();
    if (getWorld() != null) {
        lookForLobsterR();
    }
}
Collin Collin

2015/10/7

#
Okey great thanks! Now I still have this error: java.lang.NullPointerException in the DOUBLEPLAYER class. Both Bullet classes are a subclass of DOUBLEPLAYER. It gives me the error in this part:
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);
    }
davmac davmac

2015/10/7

#
Which line does the exception occur on? (If it's the lines with 'getWorld()', the problem is that the actor has been removed from the world, and you solve it the same way as the previous issue - either call 'move' before the actor can get removed from the world, or check if the actor has been removed from the world and don't call 'move' in that case).
Collin Collin

2015/10/7

#
Yeah, you're right it is in the getWorld() line
Collin Collin

2015/10/7

#
Oh my god it workes!! You are my hero! Last problem, the red lobster cant't fire before the blue lobster gets eaten. When i press the space bar (for firing red lobster bullets) nothing happens. How do i fix that?
danpost danpost

2015/10/7

#
Collin wrote...
Oh my god it workes!! You are my hero! Last problem, the red lobster cant't fire before the blue lobster gets eaten. When i press the space bar (for firing red lobster bullets) nothing happens. How do i fix that?
You need to show your firing code (for both classes) if you want us to help fix it.
Collin Collin

2015/10/7

#
Red Lobster firing code:
public void fire() 
    {
        if (reloadDelayCount >= gunReloadTime) {
            BulletR bulletR = new BulletR(getMovement().copy(), getRotation());
            getWorld().addObject(bulletR, getX(), getY());
            bulletR.move(20);
            shotsFired++;
            reloadDelayCount = 0;             
        }
    }
Key press:
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);
            }
        }
Blue Lobster firing code:
public void fire() 
    {
        if (reloadDelayCount >= gunBeloadTime) {
            BulletB bulletB = new BulletB(getMovement().copy(), getRotation());
            getWorld().addObject(bulletB, getX(), getY());
            bulletB.move(20);
            shotsFired++;
            reloadDelayCount = 0; 
        }
    }
Key press:
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();
            }
            else 
            {  
                ErrorMessage errormessage = new ErrorMessage();
                myWorld.addObject(errormessage, 500, 300);
                Greenfoot.delay(50);
                doublewater.removeObject(errormessage);
            }
        }
Thanks again!!
danpost danpost

2015/10/7

#
The main problem is that 'getKey' will only return a value once. That is, the first actor that checks the return value of 'getKey' will consume that value. That is why the second actor will only accept the keystrokes after the first one is removed from the world (when it no longer calls 'getKey'). Use the Greenfoot method 'isKeyDown' to check for the key -- add a boolean field to track the state of the key and capture changes in its state:
// instance field
private boolean wDown; // to track state of key
// check trigger key
if (Greenfoot.isKeyDown("w") != wDown) // change in state of key
{
    wDown = !wDown; // update recorded state
    if (wDown) // downstroke
    {
        // etc. (triggered)
Collin Collin

2015/10/8

#
It workes!! You are sooo awesome!! Thanks a lot!!
NORTON NORTON

2015/10/9

#
HEY IT'S TELLING ME THAT ''reached end of file while parsing'' what does it mean? Can you help me out coz I dont even see my Crab when I hit the Run or ACT
danpost danpost

2015/10/9

#
@NORTON, your project must be able to compile without error for your world to show. The error, "reached end of file while parsing", means that you are missing one or more closing squiggly brackets. Make sure all your brackets are paired appropriately. (btw, the missing one may not be at the end of the file itself, but within the file somewhere). Note (for the future): please do not use all uppercase letters in your discussions. It makes it seem like you are screaming and most people will not respond to you. Also, when you get an error, as you have here, you should supply the class code in question (read the page like to the "Posting code? read this!" link below the reply box).
You need to login to post a reply.
1
2