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

2015/1/16

Error: n must be positive

Pe1per Pe1per

2015/1/16

#
I`ve a problem. I`m programming a flappy bird game but every time the pipe at the bottom is placed there is an error "n must be positive" and I don`t understand why! Here is the code (need help)
  public void act ()
    {
      Pipecounter++;            
      if (Pipecounter % 100 == 0) {
          // setze Pipe Object 
          place = Greenfoot.getRandomNumber((350) - 250);
          
          Pipe pipe = new Pipe();
          Pipe2 pipe2 = new Pipe2();
          addObject(new Pipe(), getWidth(), 0 - place ); 
          addObject(new Pipe2(), getWidth(), 800 + place );
          
    }
Super_Hippo Super_Hippo

2015/1/16

#
0-place is less than 0, so negative. To avoid this problem, change your super(x,y,cellsize) when creating the world to super(x,y,cellsize,false), then objects can be outside the visible world.
Pe1per Pe1per

2015/1/16

#
The Error is still there...
danpost danpost

2015/1/16

#
With the code given, it would seem that the gap varies in size, not in location. The operator for 'place' in the addObject statements (lines 10 and 11) should be the same (either both plus or both minus) to vary the location and not the size of the gap. For the error, first show the entire error message.
Super_Hippo Super_Hippo

2015/1/17

#
By the way, place is between 0 and 99. I don't know I you wanted to do it like that. Lines 8+9 aren't used. As danpost said, it should be something like that:
int place = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new Pipe(), getWidth(), 0 + place); 
addObject(new Pipe2(), getWidth(), getHeight()/2 + place);
davmac davmac

2015/1/17

#
The error is on line 6:
        place = Greenfoot.getRandomNumber((350) - 250);
... this is equivalent to:
        place = Greenfoot.getRandomNumber(-100);
That's why you get an error; Greenfoot.getRandomNumber() requires a positive parameter. I think what you really wanted was:
        place = Greenfoot.getRandomNumber(350) - 250;
It would have been much easier to diagnose this if you had made it clear which line the error actually occurred on.
Super_Hippo Super_Hippo

2015/1/17

#
How is 350-250= -100?
davmac davmac

2015/1/17

#
Super_Hippo wrote...
How is 350-250= -100?
Hmm, you're right. I mis-read that code (as 250 - 350). However, the 'n must be positive' error nearly always comes from mis-use of the Greenfoot.getRandomNumber method, in my experience (and it certainly doesn't come from addObject). Pe1per, perhaps you can point out which line actually causes the error rather than forcing us to guess?
You need to login to post a reply.