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

2014/8/17

'n must be positive' error

Dab1001 Dab1001

2014/8/17

#
I have an actor which points towards the mouse when certain keys are down, and it is touching certain actors. The minimum rotation is meant to be -90 and the maximum is 180. The rotation is converted into a number from 10 to 280 by adding 100. This number is taken from 300 and then used in the 'Greenfoot.getRandomNumber(x)' method. However, I keep getting the 'n must be positive' error, and I have no idea how that can occur. Here is my code:
import greenfoot.*;
public class BuretteWheel extends Objects
{
    private boolean pickedUp = false;
    private int dripSpawnChance = 0;
    private boolean initialised = false;
    public void act() 
    {
        if (initialised == false)
        {
            setRotation(-90);
            initialised = true;
        }
        if (pickedUp)
        {
            if (Greenfoot.getMouseInfo() != null)
            {
                turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
                if (Greenfoot.getMouseInfo().getX() < getX() && Greenfoot.getMouseInfo().getY() > getY())
                {
                    if (getRotation() > -135)
                    {
                        setRotation(-90);
                    }
                    else
                    {
                        setRotation(180);
                    }
                }
            }
            if(Greenfoot.isKeyDown("A") == false 
            && Greenfoot.isKeyDown("W") == false 
            && Greenfoot.isKeyDown("E") == false 
            && Greenfoot.isKeyDown("R") == false 
            && Greenfoot.isKeyDown("space") == false)
            {
                pickedUp = false;
                ((GameBackdrop) getWorld()).object.objectPickedUp = false;
            }
        }
        else
        {
            if(Greenfoot.isKeyDown("A") == true && getOneIntersectingObject(Little.class) != null
            || Greenfoot.isKeyDown("W") == true && getOneIntersectingObject(Ring.class) != null 
            || Greenfoot.isKeyDown("E") == true  && getOneIntersectingObject(Middle.class) != null
            || Greenfoot.isKeyDown("R") == true  && getOneIntersectingObject(Index.class) != null
            || Greenfoot.isKeyDown("space") == true && getOneIntersectingObject(Thumb.class) != null)
            {
                pickedUp = true;
                ((GameBackdrop) getWorld()).object.objectPickedUp = true;
            }
        }
        dripSpawnChance = getRotation() + 100;
        if (Greenfoot.getRandomNumber(300 - dripSpawnChance) == 0 && getRotation() != 0 && ((GameBackdrop) getWorld()).burettefluid.volume > 0)
        {
            Droplet droplet = new Droplet("Neutralisation Alkali");
            ((GameBackdrop) getWorld()).addObject(droplet, 400, 375);
        }
    }    
}
Anybody know how this is happening? Thanks in advance!
danpost danpost

2014/8/18

#
If the rotation is greater than 199, which will occur when you think the rotation is between -160 and -1, then you will be trying to get a random number using an argument less than one; hence the error. The rotation returned using the Actor class method 'getRotation' will be a number between zero and 359, inclusive. You should reformulate your maths based on this. Maybe use something like this for line 53:
dripSpawnChance = (getRotation()+100)%360;
Between -90 and -1 (or 270 and 359), dripSpawnChance will be between 10 and 99. Between 0 and 180, dripSpawnChance will be between 100 and 280. All values will be less than 300; so you should be safe from the error.
Dab1001 Dab1001

2014/8/18

#
danpost wrote...
If the rotation is greater than 199, which will occur when you think the rotation is between -160 and -1, then you will be trying to get a random number using an argument less than one; hence the error. The rotation returned using the Actor class method 'getRotation' will be a number between zero and 359, inclusive. You should reformulate your maths based on this. Maybe use something like this for line 53:
dripSpawnChance = (getRotation()+100)%360;
Between -90 and -1 (or 270 and 359), dripSpawnChance will be between 10 and 99. Between 0 and 180, dripSpawnChance will be between 100 and 280. All values will be less than 300; so you should be safe from the error.
Oh, thanks! That's a fail on my part, as I originally assumed it'd be 0-359, but I must've coded something wrong again which lead me to believe it was -179 to 180. Thanks again! This'll be the last thing I need to finish the game :)
You need to login to post a reply.