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

2015/2/28

Need help with getrandomnumber (Problem in line 17 to 20)

xobookx xobookx

2015/2/28

#
 public void act() 
    {
        int groesse = 0;
        int s = 0;
        int l = 0;
   
        Actor a;
        a = getOneIntersectingObject(Krümel.class);
        if(a != null)
        {
            getWorld().removeObject(a);
            groesse = groesse + 10;
            System.out.println(groesse);
            GreenfootImage image = getImage();
            image.scale(image.getWidth() + groesse, image.getHeight() + groesse);
            setImage(image);
            Greenfoot.getRandomNumber(s);
            Greenfoot.getRandomNumber(l);
            getWorld().addObject(new Krümel(), s, l);
        }
xobookx xobookx

2015/2/28

#

I want the new Object to be added with its X Position on the Random number "s" and its Y Position on the Random number "l", but i cant say that "s" may only be smaller than 600 and "l" may only be smaller than 400 but both should be taller than 0

danpost danpost

2015/2/28

#
First, the value of the parameter of 'getRandomNumber' must be a positive number. I do not see where 's' nor 'l' are set to any value other than zero. Second, only the values of 's' and 'l' are taken when used as part or all of a parameter expression. Finally, and I think this goes along with the previous statement, using 'getRandomNumber' returns a value that must be used either in an expression or assigned to a variable to be used later; otherwise, the value is immediately lost. In other words, lines 17 and 18 do absolutely nothing above, even if the value of 's' or 'l' was a positive number. You said you wanted X Position and Y Position, where you will place the new object within an area that is sized (600, 400).
int xPos = Greenfoot.getRandomNumber(600);
would set the new int variable 'xPos' to some random value between 0 and 599, inclusive. Maybe you can figure out the rest from there, doing similar for 'yPos' and using them in the 'addObject' statement.
You need to login to post a reply.