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

2015/2/16

I'm inserting an array, but where do I put it?

1
2
Kendall Kendall

2015/2/16

#
I'm making an array of actors that are all part of the subclass "Falling Object". I'm making an array to hold the actors, so I can then make a loop that pulls a random index location. I'm very new at coding, and I was wondering, where exactly do I put the array?
Frogfrote Frogfrote

2015/2/16

#
Add the new array at the beginning of your code. It should go something like this
1
private final String[] NAME = {"1", "2"};
if this was a string array. Good luck!
danpost danpost

2015/2/16

#
There is no need to even have an array for that. You can, at any time, use 'getWorld().getObjects(FallingObject.class)' from an Actor subclass or just 'getObjects(FallingObject.class)' from a World subclass. This will return a List object containing all the FallingObject actors in the world. You can, if you really want to (but is not necessary), convert the List object to an Array object using something like the following (coded as within a World subclass):
1
2
3
FallingObject[] fallingObjects = ((java.util.List<FallingObject>)getObjects(FallingObject.class)).toArray();
// or
FallingObject[] fallingObjects = getObjects(FallingObject.class).toArray(new FallingObject[0]);
This would be used before your loop. Within the loop the random number generator would then use either the List object size or the Array object length, depending on what you are using, to get a random index. If it is possible that no object of that type are in the world, you will need to ensure the List object is not empty or the Array object length is not zero before getting a random index.
Kendall Kendall

2015/2/17

#
Thank you. If I wanted to make a loop that followed this pseudo code, what might I code?
1
2
3
4
5
6
7
8
9
10
11
public void createFallingObjects(){
    List<FallingObject> fallingObjectList = getObjects(FallingObject.class);
    //ArrayList<FallingObject> fallingObjects = newArrayList(5);
    if (counter >= 10){
        add new object random i value between 0 and 4 at (x,y)
         
         
        counter = 0;
    }
     
}
Kendall Kendall

2015/2/17

#
I know that I will probably have to use Greenfoot.getRandomNumber(4) to get a random i value.
davmac davmac

2015/2/17

#
Kendall wrote...
new object random i value
What do you mean by this? What type (class) of object do you want to add, and what is the significance of the random number to it?
joandvgv joandvgv

2015/2/17

#
davmac wrote...
Kendall wrote...
new object random i value
What do you mean by this? What type (class) of object do you want to add, and what is the significance of the random number to it?
I think he has in mind 5 types of objects and the random i says which one is going next. Just my guess, don't really know that for sure.
danpost danpost

2015/2/17

#
Kendall wrote...
I know that I will probably have to use Greenfoot.getRandomNumber(4) to get a random i value.
Be specific, completing the following sentence (as a continuation of the thought presented in the quote): But, how do I use that random i value to ...
Kendall Kendall

2015/2/19

#
Because my list is a list of all objects, my goal was to write a loop that would select one of these objects at random to be added to the world. I thought to do this, I would need to select a random i value. However, I am not sure the loop could work like that. In response to danpost, how do I use that random i value to add the object at that index location to the world at a specific coordinate? Thank you guys for all of your help!
danpost danpost

2015/2/19

#
If you use 'getObects' to create the list, then all the objects in the list will have already been placed into the world -- you can only change their location if you want to move them to specific coordinates. So, what do you intend to do, then?
Kendall Kendall

2015/2/20

#
I didn't know they would already be added to the world. Do you know how I could accomplish adding the objects, one at a time at random, to the world at one specified coordinate? All of the objects are a subclass of a class called FallingObject.
danpost danpost

2015/2/20

#
Just to clarify, 'getObjects' returns a list of all objects that are currently in the world (that have previously been added by use of the 'addObject' method). Maybe the best way is to create a list of the actors to add into the world; and one at a time, remove one (at random) from the list and add it into the world. The List class method 'remove(int)', where the int is the index of the element to be removed, returns the object. So, you can use something like this (if 'listOfActors' is the name you gave the List object):
1
if ( ! listOfActors.isEmpty() ) addObject(listOfActors.remove(Greenfoot.getRandomNumber(listOfActors.size())), /** location coordinates */);
davmac davmac

2015/2/20

#
Because my list is a list of all objects, my goal was to write a loop that would select one of these objects at random to be added to the world.
So wait, how are these objects different from each other? (If they're all the same, then selecting a "random" one is the same as selecting the first one). To me this sounds a bit like the XY problem. You want a list, but it's not clear why.
Kendall Kendall

2015/2/23

#
I will try that danpost, I think that will work. Thank you! And davmac, essentially the objects all behave the same, but consist of a different image. They are all subclasses of FallingObject, but they each have a different image-- money, fish, vase, anvil, and beans. Thank you guys!
danpost danpost

2015/2/23

#
I think davmac was correct in this being an XY problem. Now that clarification has been given to the issue, it seems that the initial approach to the solution was not a very good one. A better way to handle an object type with different images is to have the object itself randomly choose an image for itself. All objects created will be FallingObject objects (not of any subclasses of it):
1
2
3
4
5
6
7
8
9
10
11
12
// class constants in FallingObject class
private static final String[] OBJS = { "money", "fish", "vase", "anvil", "beans" };
// instance field in FallingObject class
private String obj;
// in constructor of FallingObject class
obj = OBJS[Greenfoot.getRandomNumber(OBJS.length)];
setImage(obj+".png");
// extra method
public String getObjectName()
{
    return obj;
}
The method at the end can be used to determine which of the objects was chosen for that particular actor.
There are more replies on the next page.
1
2