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

2011/10/27

Creating Random Objects

GeneralBrae GeneralBrae

2011/10/27

#
Hello again. I'm afraid I have another question. I have this code to create a random actor in a certain position. The code below is only OK for one actor with the name B2 but I'm going to expand it if I can get the logic working. I have an array called "classes" which contains the names of all my actors. When I compile I'm getting the message "Incompatible Types" and the line createClasses = newB2 is highlighted. Any suggestions? Thanks in advance for any help. I'll try not to bug you all too much after this!
for (int i = 0; i < classes.length - 1; i++)
        {
            int randomNumber = Greenfoot.getRandomNumber(classes.length - 1);
            
            B2 newB2 = new B2();
            createClasses[i] = newB2;
            
            addObject (createClasses[i], 200, 140);
            
        }
nccb nccb

2011/10/27

#
The error is related to what type the createClasses array has. It needs to have an array type that is either an array of B2, or an array of a super-class of B2 (such as Actor). I can't see what type it is at the moment, but I suspect that's the problem.
GeneralBrae GeneralBrae

2011/10/27

#
OK. Do you have any idea how I could solve it? Do I have to declare the array differently or something?
nccb nccb

2011/10/27

#
Post the declaration of createClasses and then I can suggest a specific solution.
GeneralBrae GeneralBrae

2011/10/27

#
Not much to it. Just declared a basic array with no values...I think!
public String[] createClasses = {}; 
danpost danpost

2011/10/27

#
I think the simplest way would be to write a method getRandomActor() with a switch:
    private Actor getRandomActor()
    {
        switch (Greenfoot.getRandomNumber(3)) // 3 can be change to the number of different actors it could be
        {
            case 0:  return new Ball();
            case 1:  return new Balloon();
            case 2:  return new Barrel();  //  Whatever actors they could be
            ...
        }
        return null; // this statement should never be reached, but system will not compile without it
    }
Then, this should work:
        Actor randomActor = getRandomActor();
        int myX = Greenfoot.getRandomNumber(getWidth() - 100) + 50;
        int myY = Greenfoot.getRandomNumber(getHeight() - 100) + 50;
        addObject(randomActor, myX, myY);
This avoids dealing with the array totally.
nccb nccb

2011/10/27

#
danpost's suggestion is a good one. If you want to fix your code as-is, you need to change the type in the array from String (B2 is not a String, that's where the error comes from) to Actor:
public Actor createClasses = {};
That will fix the compilation error, but will get you a run-time error: that array is empty, but your loop will try to access all sorts of elements inside it. I'd suggest adding this line before your original loop (the one shown in the first post in this thread):
createClasses = new Actor[classes.length];
This will make sure your createClasses array is the same length as your classes array before the loop, which should then avoid any problems.
GeneralBrae GeneralBrae

2011/10/28

#
Thanks that solves it. Is there a way to empty data at a specific index from an array? Thanks again. This is hugely helpful.
You need to login to post a reply.