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

2018/12/18

Returning values

Rod53 Rod53

2018/12/18

#
Hi, I require some assistance please. I am attempting to return a RNG x and y value for a set of coordinates. The x and y i am returning is within a set area minus an area inside of the set area. I am adding an object within the set area in my world. I am getting issues with the add object part and when i try to retrieve the returned x and y from coordinates() I have attempted multiple variations of the code but must be missing something, im not too adept at Java yet. I appreciate any help and open to any scrutiny. Its all a learning experience :) Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public int coordinates()
    {
        int xInside = Greenfoot.getRandomNumber(600)+130;
        int yInside = Greenfoot.getRandomNumber(605)+115;
        int xOutside = Greenfoot.getRandomNumber(430)+225;
        int yOutside = Greenfoot.getRandomNumber(400)+210;
         
        if (((xInside >= 130 && xInside <= 600 ) && ( yInside >= 115 && yInside <= 605 ))
                && ((xOutside >= 225 && xOutside <= 430) && (yOutside >= 210 && yOutside <= 400)))
            {
                return xInside;
                return yInside;
            }
}
 
    public void zoneRNG()
    {
        barrels1[] numBarrels = new barrels1[3];
        for(int i=0; i<numBarrels.length;i++)
        {
            numBarrels[i] = new barrels1();
            addObject(numBarrels[i], coordinates("xInside", "yInside" ));
        }                 
    }
danpost danpost

2018/12/18

#
You can only return one thing back to the calling method. If you require more than one int value to be returned, it must be wrapped in another object. For 2 int values, you could return a Point object that holds two ints:
1
2
3
4
5
6
7
8
9
10
11
public class Point
{
    int x;
    int y;
     
    public Point(int ex, int wye)
    {
        x = ex;
        y = wye;
    }
}
or more simply an array:
1
int[] coords = new int[] { x, y };
Just change the return type of the method from 'int' to 'Point" or 'int' followed by empty square brackets as needed at line 1.
You need to login to post a reply.