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

2017/4/18

Help with spawning in an object a set location infinite times

1
2
Asiantree Asiantree

2017/4/25

#
Okay, so I am trying to read in all the integers from the file into an array and then store them into the array called "Balls." The currentSize integer keeps track of the currentSize of the array. Now, I am having trouble placing it. Where would I put this code so that the integers from the file go into the array and then I can use the integers in the array to spawn Balls?
1
2
3
4
5
6
7
this.Balls = new int[10000];
        while (s.hasNext())
        {
            if (currentSize < Balls.length)
            {
                Balls[currentSize++] = s.nextInt();
            }
danpost danpost

2017/4/25

#
Spawning is usually done in the world class (unless there is a specific actor that is considered the source of the new object being spawned).
Asiantree Asiantree

2017/4/25

#
Yes, but how can I access each element of the array to spawn in a new ball?
danpost danpost

2017/4/25

#
Asiantree wrote...
Yes, but how can I access each element of the array to spawn in a new ball?
You can use a for loop to spawn all of them at once:
1
for (int i=0; i<currentSize; i+=2) addObject(new Ball(), Balls[i], Balls[i+1]);
or you can use a counter to spawn them individually (but in order):
1
2
3
4
5
6
7
8
9
// instance field
private int ballCounter;
 
// in act
if (<< condition to spawn >> == true)
{
    addObject(new Ball(), Balls[ballCounter], Balls[ballCounter+1]);
    ballCounter = (ballCounter+2)%currentSize;
}
You could also spawn them in random order, both by getting every location before repeating or by allowing repeated locations before the list is exhausted.
Asiantree Asiantree

2017/4/25

#
Okay, so the parameter for a Ball object requires the a reference to BallInfo, I believe I have posted previously the Ball class to show you how it was set up. A new Ball object should look something like this when creating a new Ball:
1
new Ball(*BallInfo bi*)
Where "BallInfo bi" is a reference to the BallInfo class. Everything else compiles, but I need help with accessing the BallInfo class when spawning a new Ball object, and what to put into the Ball's parameters.
danpost danpost

2017/4/25

#
Asiantree wrote...
Okay, so the parameter for a Ball object requires the a reference to BallInfo, I believe I have posted previously the Ball class to show you how it was set up.
Yes, I now recall that. Then:
1
2
3
4
5
6
7
// in act
if (<< condition to spawn >> == true)
{
    BallInfo info = infos.get(Greenfoot.getRandomNumber(infos.size()));
    addObject(new Ball(info), Balls[ballCounter], Balls[ballCounter+1]);
    ballCounter = (ballCounter+2)%currentSize;
}
Asiantree Asiantree

2017/4/25

#
Okay, now I have compiled everything into a "spawnBalls" method that is placed inside the World's act method. Now, I am getting a "NoSuchElementException for the line of code commented below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public void spawnBalls()
    {
        if (Greenfoot.isKeyDown("l"))
        {
            FileDialog fd = null;
            fd = new FileDialog(fd, "Pick a file" , FileDialog.LOAD);
            fd.setVisible(true);
         
            String fname = fd.getDirectory() + fd.getFile();
         
            File file = new File(fname);
         
            Scanner fReader = null;
        try
        {
            fReader = new Scanner(file);
        }
        catch(FileNotFoundException fne)
        {
            System.out.println(fne);
            return;
        }
        while(fReader.hasNextLine())
        {
            if (currentSize < Balls.length)
            {
                Balls[currentSize++] = fReader.nextInt(); //here is where I am getting the NoSuchElementException
            }
            if (!infos.isEmpty())
            {
                BallInfo info = infos.get(Greenfoot.getRandomNumber(infos.size()));
                addObject(new Ball(info), Balls[ballCounter], Balls[ballCounter+1]);
                ballCounter = (ballCounter+2)%currentSize;
            }
        }
       }
    }
And just a reminder, the file that I am using the spawn the Balls in looks like this: 6 6 6 12 12 12 18 18 18 24 24 24 30 30 30 36 36 36 42 42 42 ... ... ... Where three individual numbers represent the Ball's red, green, and blue value respectively, and is used in the BallInfo class to represent what Ball is being placed into the world so that a graph will appear in the world keeping track of how many Balls have touched the slider at the bottom.
danpost danpost

2017/4/25

#
Lines 29 through 34 do not belong where you put them. Where you placed those lines is where you need to fill the two List objects (the one with Color objects and the other with BallInfo objects). In fact, I do not see where you are clearing those List objects when a new "load" is processed. Also, you are using 'hasNextLine' and then 'nextInt' (which probably should be 'nextLine'). Then the line would need split into its individual int values. Maybee all the lines need read in first (as Strings); then, you iterate through them, splitting them into the three int values, to create the Color and BallInfo objects to be placed in their respective Lists objects.
Asiantree Asiantree

2017/4/25

#
Do ArrayLists operate the same as normal Arrays or are they of the same class?
danpost danpost

2017/4/25

#
Asiantree wrote...
Do ArrayLists operate the same as normal Arrays or are they of the same class?
They are both collections and you have methods to convert from one to the other; but they do have their distinctions. The size of an array is immutable; but the size of an ArrayList is variable -- for one thing. That, in itself, tells you they are not of the same class. There is an ArrayList class and there is an Arrays class. The Arrays class is what backs up arrays (like String or int) -- much like the way the Integer class backs up an int type field. You cannot call any methods on an array; where there are an abundance of methods you can call on an ArrayList object. Just like you cannot call any methods on an int; but there are multiple methods you can call on an Integer object. Hope that answered your question to your satisfaction.
Asiantree Asiantree

2017/4/25

#
Yes, that answered my question. Though, I have not learned about ArrayLists in my class currently, so could you point me in right direction for solving this issue with just Arrays.
danpost danpost

2017/4/25

#
Asiantree wrote...
Yes, that answered my question. Though, I have not learned about ArrayLists in my class currently, so could you point me in right direction for solving this issue with just Arrays.
As a precursor, let me make note that regardless of which you use, ArrayList objects or normal arrays, how you will treat and use them will not be much different. It would be easier to work with ArrayList objects for the Color and BallInfo objects because their size is not fixed. If you used normal arrays, you will either be juggling different arrays or have empty elements within them (as you "load" or "merge" a file). Hopefully, the use of a simple int field will be enough to make it manageable. Some more background info first. Tell me if I am correct -- you have exactly six files, each with exactly seven lines of three int values for the color parts of different colors. It would also be nice to know how these files are named. Each of the seven lines in each file is a String object with space seperators between the values. Let us start with that.
danpost danpost

2017/4/25

#
Something just came up. Will be out for a while -- maybe 8 hrs or so. Until then.
Asiantree Asiantree

2017/4/25

#
I have three files that containing 30 lines of code that I may choose from. I just showed you one file's first seven lines of integers to show you the idea that I need to read in three integers as a set of values to spawn in ONE ball. The name of the first file, and the values I showed above, is "42ShadesOfGray.col" I believe I am to use normal Arrays since my knowledge from the course is limiting me to that required topic to accomplish this task. I will work on creating an array with a line split that can read in the three values for one Ball and display it later this evening when you get back.
Asiantree Asiantree

2017/4/25

#
Okay, so I got the file to read in the BallInfo integers and managed to spawn in all of them (I believe). Using this code, about 35 BallInfo objects are spawned into the world:
1
2
3
4
5
6
while(fReader.hasNextLine())
        {
            String line = fReader.nextLine();
            String[] balls = line.split(" ");
            addObject(new BallInfo(Integer.valueOf(balls[0]), Integer.valueOf(balls[1]), Integer.valueOf(balls[2])), Greenfoot.getRandomNumber(450), Greenfoot.getRandomNumber(600));
        }
Essentially, I took the code you gave me from my previous project (the line and line split part) and applied it here. The array looks at the first integer and reads in it all the way to the third integer and then moves to the next line to spawn in the rest of the BallInfo values that are in the file. I used the getRandomNumber method to try and separate the BallInfo objects out to see if all of them actually spawned into the world. I wish to have the BallInfo objects spawn along the Y axis on a set X location. So basically making one large graph that runs down the left side of the screen. Do I use the getRandomNumber for the Y value only for that? Also, I went ahead and made a spawnBalls method and here is the code, although I am getting an error for the following line of code:
1
2
3
4
5
6
7
8
9
public void spawnBalls()
    {
        if (!infos.isEmpty())
            {
                BallInfo info = infos.get(Greenfoot.getRandomNumber(infos.size()));
                addObject(new Ball(info), balls[ballCounter], balls[ballCounter+1]); // I get a  java.lang.String cannot be converted to int error for this line of code. I have tried to parseInt it but that didn't work
                ballCounter = (ballCounter+2)%currentSize;
            }
    }
You need to login to post a reply.
1
2