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

2019/11/16

How do you create an array for spawning actors in a random order

KylesGallery KylesGallery

2019/11/16

#
this is my code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class ArraySelection here. * * @author (your name) * @version (a version number or a date) */ public class ArraySelection extends Actor { /** * Act - do whatever the ArraySelection wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ static int Color=0; public ArraySelection() { /* int array = new int; // array for the choices to be made randomly // add random values and shuffle for (int i=0; i<array.length; i++, array = 9) // fill array { java.util.Collections.shuffle(java.util.Arrays.asList(array)); if(array==0) { Objective objective = new Objective(); getWorld().addObject(objective,1164,114); } if(array==1) { Objective objective2 = new Objective(); getWorld().addObject(objective2,296,432); } if(array==2) { Objective objective1 = new Objective(); getWorld().addObject(objective1,282,774); } if(array==3) { Objective objective3 = new Objective(); getWorld().addObject(objective3,805,52); } if(array==4) { Objective objective4 = new Objective(); getWorld().addObject(objective4,130,89); } if(array==5) { Objective objective5 = new Objective(); getWorld().addObject(objective5,233,230);} if(array==6) { Objective objective7 = new Objective(); getWorld().addObject(objective7,489,606);} if(array==7) { Objective objective8 = new Objective(); getWorld().addObject(objective8,751,672);} if(array==8) { Objective objective9 = new Objective(); getWorld().addObject(objective9,760,295);} if(array==9) { Objective objective10 = new Objective(); getWorld().addObject(objective10,1119,458); objective10.setLocation(1066,362); } } } }
danpost danpost

2019/11/16

#
KylesGallery wrote...
// add random values and shuffle
for (int i=0; i<array.length; i++, array[i] = 9)
I do not see random values being added to the array. Also, all following codes will be executed 9 times as it is all within the for loop. That means you are shuffling 9 times, not just once. You say "// fill array" at a place where the following codes do nothing to add values to the array. You need two separate for loops. The first to add random values to the array and the latter to act upon those values. The shuffling should be done between the two loops. My main question here is how does it make any difference what order they are spawned in? Also, major problem is that you cannot spawn actors into a world by an actor that is not yet in a world and has no reference to any world. That is, the getWorld method within this block of code will return a null value and you cannot add actors to a non-existent world.
KylesGallery KylesGallery

2019/11/16

#
Can you show an example? I just started coding in greenfoot 2 months ago
danpost danpost

2019/11/16

#
KylesGallery wrote...
Can you show an example? I just started coding in greenfoot 2 months ago
// fill array
for (int i=0; i<array.length; i++) array[i] = i;
// shuffle
java.util.Collections.shuffle(java.util.Arrays.asList(array));
The above will put the values 0 through 8 in the array in order (line 2) and then shuffle them (line 4). Now you would loop again to spawn your actors in the new randomized (shuffled) order:
for (int i=0; i<array.length; i++) ...
KylesGallery KylesGallery

2019/11/16

#
You asked above why the difference matters, i currently have 10 types of objectives, each with different color/value with each and instead of always getting the same one or 2 with the same i decided to do this and spawn them in at different timings public Objective() { GreenfootImage image = getImage(); if(ArraySelection.Color==0) { setImage("Objective Red.png"); } if(ArraySelection.Color==1) { setImage("Objective Blue.png"); } if(ArraySelection.Color==2) { setImage("Objective Green.png"); } if(ArraySelection.Color==3) { setImage("Objective White.png"); } if(ArraySelection.Color==4) { setImage("Objective Pink.png"); } if(ArraySelection.Color==5) { setImage("Objective Yellow.png"); } if(ArraySelection.Color==6) { setImage("Objective Gray.png"); } if(ArraySelection.Color==7) { setImage("Objective Purple.png"); } if(ArraySelection.Color==8) { setImage("Objective Black.png"); } if(ArraySelection.Color==9) { setImage("Objective Brown.png"); } ArraySelection.Color++; image.scale(15,15); setImage(image); }
KylesGallery KylesGallery

2019/11/16

#
The "Player" can only extract one type of "objective" at a time, then going back to the beginning to store it and give you a new color. Also did i do it right? because I'm getting an error New code for "ArraySelection" static int Color=0; public ArraySelection() { int array = new int; // array for the choices to be made randomly java.util.Collections.shuffle(java.util.Arrays.asList(array)); // add random values and shuffle for (int i=0; i<array.length; i++, array = 9) // fill array { for ( i=0; i<array.length; i++) array = i; // shuffle //java.util.Collections.shuffle(java.util.Arrays.asList(array)); if(array==0)//line 30 { Objective objective = new Objective(); getWorld().addObject(objective,1164,114); } if(array==1) { Objective objective2 = new Objective(); getWorld().addObject(objective2,296,432); } if(array==2) { Objective objective1 = new Objective(); getWorld().addObject(objective1,282,774); } if(array==3) { Objective objective3 = new Objective(); getWorld().addObject(objective3,805,52); } if(array==4) { Objective objective4 = new Objective(); getWorld().addObject(objective4,130,89); } if(array==5) { Objective objective5 = new Objective(); getWorld().addObject(objective5,233,230); } if(array==6) { Objective objective7 = new Objective(); getWorld().addObject(objective7,489,606);} if(array==7) { Objective objective8 = new Objective(); getWorld().addObject(objective8,751,672);} if(array==8) { Objective objective9 = new Objective(); getWorld().addObject(objective9,760,295);} if(array==9) { Objective objective10 = new Objective(); getWorld().addObject(objective10,1119,458); objective10.setLocation(1066,362); } } } Also this is the error java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 at ArraySelection.<init>(ArraySelection.java:30)<<Line 30 is labeled in this comment near the top at MyWorld.prepare(MyWorld.java:684)<<The code to spawn "ArraySelection" at MyWorld.<init>(MyWorld.java:20)<<<Prepare code at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) at greenfoot.core.Simulation.newInstance(Simulation.java:580) at greenfoot.platforms.ide.WorldHandlerDelegateIDE.lambda$instantiateNewWorld$0(WorldHandlerDelegateIDE.java:143) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:470) at greenfoot.core.Simulation.maybePause(Simulation.java:299) at greenfoot.core.Simulation.runContent(Simulation.java:190) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2019/11/16

#
This is the start of your codes (for reference):
static int Color=0;
public ArraySelection() 
{
    
    int[] array = new int[10]; // array for the choices to be made randomly
    java.util.Collections.shuffle(java.util.Arrays.asList(array));
    // add random values and shuffle
    for (int i=0; i<array.length; i++, array[i] = 9)
    // fill array
    
    {
        
        for ( i=0; i<array.length; i++) array[i] = i;
        // shuffle
        //java.util.Collections.shuffle(java.util.Arrays.asList(array));
        
        if(array[i]==0)[b]//line 30[/b]
        {
            Objective objective = new Objective();
            getWorld().addObject(objective,1164,114);
        }
        // etc.
danpost danpost

2019/11/16

#
Line 13, which should be my unmodified line 2 above, was to be placed before line 6. Line 8 should not contain the comma nor the expression following it. So, the starting codes should look like this:
static int Color=0;
int array = new int[10]; // create array

public ArraySelection() 
{
    for (int i=0; i<array.length; i++) array[i] = i; // fill array
    java.util.Collections.shuffle(java.util.Arrays.asList(array)); // shuffle array
}

// somewhere else (after this actor is placed into the world)
    for (int i=0; i<array.length; i++)
    {
       if(array[i]==0)
        {
            Objective objective = new Objective();
            getWorld().addObject(objective,1164,114);
        }
        // etc.
KylesGallery KylesGallery

2019/11/18

#
Heres a link to my errors(BTW its still happening https://snipboard.io/HdTtns.jpg And heres the raw code static int Color=0; int array = new int; // create array public ArraySelection() { for (int i=0; i<array.length; i++) array = i; // fill array java.util.Collections.shuffle(java.util.Arrays.asList(array)); // shuffle array Spawning(); } // somewhere else (after this actor is placed into the world) public void Spawning() { for (i=0; i<array.length; i++) { if(array==0) { Objective objective = new Objective(); getWorld().addObject(objective,1164,114); } if(array==1) { Objective objective2 = new Objective(); getWorld().addObject(objective2,296,432); } if(array==2) { Objective objective1 = new Objective(); getWorld().addObject(objective1,282,774); } if(array==3) { Objective objective3 = new Objective(); getWorld().addObject(objective3,805,52); } if(array==4) { Objective objective4 = new Objective(); getWorld().addObject(objective4,130,89); } if(array==5) { Objective objective5 = new Objective(); getWorld().addObject(objective5,233,230); } if(array==6) { Objective objective7 = new Objective(); getWorld().addObject(objective7,489,606);} if(array==7) { Objective objective8 = new Objective(); getWorld().addObject(objective8,751,672);} if(array==8) { Objective objective9 = new Objective(); getWorld().addObject(objective9,760,295);} if(array==9) { Objective objective10 = new Objective(); getWorld().addObject(objective10,1119,458); objective10.setLocation(1066,362); } } }
danpost danpost

2019/11/18

#
You are still trying to execute the spawning code prior to the ArraySelection actor being put in the world. Make use of the Actor class method called addedToWorld.
You need to login to post a reply.