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

2015/4/10

How can I add actors while the world is running?

1
2
decadence18 decadence18

2015/4/10

#
I need to be able to add actors while the world is running. I'm attempting to do this by taking a combination of key presses to trigger a method, and then taking user info on an item ID, and how many of the items that the user wants, and then adding that number of that actor into the world. Ex. -
public void act() {
    if (Greenfoot.isKeyDown("shift") && Greenfoot.isKeyDown("w")) {
        addMiscActor();
    }
}

public String actorID;
public String howManyActors;
public int numberOfActors;
public void addMiscActor() {
    actorID = JOptionPane.showInputDialog("Actor ID");
    howManyActors = JOptionPane.showInputDialog("How many actors?");
    numberOfActors = Integer.parseInt(howManyActors);
    if (actorID == "M1") {
        for (int i=0; i<numberOfActors; i++) {
            addObject(new ActorOne(), 35, 35);
        }
    }
}
However, this still only adds a single actor. What am I doing wrong/missing?
danpost danpost

2015/4/10

#
You are probably adding the number entered; but they are one on top of another. After adding a number of actors, pause the scenario and drag the one actor that you see was added. You will probably find more under it. You are adding them all at the same location coordianates.
decadence18 decadence18

2015/4/10

#
danpost wrote...
You are probably adding the number entered; but they are one on top of another. After adding a number of actors, pause the scenario and drag the one actor that you see was added. You will probably find more under it. You are adding them all at the same location coordianates.
No, I have code in that allows me to drag th actors while the scenario is running, and there is only one. That's the problem.
Super_Hippo Super_Hippo

2015/4/10

#
Did you actually do what danpost suggested? If not, it could still be that you drag all instead of one if you coded it like that.
decadence18 decadence18

2015/4/10

#
Before posting this I tried what danpost suggested, and what I said, and several other ways including having it stagger their spawn position. It is only creating one.
Super_Hippo Super_Hippo

2015/4/10

#
What is happening if you write that at the end of the method?
System.out.println(numberOfActors);
System.out.println(getObjects(ActorOne.class).size());
Maybe have the second line at the beginning of the method too if there are already objects of ActorOne in the world before the method is called. Are ActorOne objects remove other intersecting ActorOne objects?
decadence18 decadence18

2015/4/10

#
If I write that at the end of the method, it always returns the "numberOfActors" variable correctly, and it always returns the second "getObjects" function as 0.
danpost danpost

2015/4/10

#
Are you entering "M1" as the actor id? It is the only thing that restricts the spawning of the number of actors at this point.
decadence18 decadence18

2015/4/10

#
private String actorID;
    private String howManyActors;
    private int numberOfActors;
    private Actor thisActor;

public void act()
    {
        if (Greenfoot.isKeyDown("shift") && Greenfoot.isKeyDown("a") && Greenfoot.isKeyDown("m"))
        {
            addMiscActor();
        }
    }

    public void addMiscActor()
    {
        actorID = JOptionPane.showInputDialog("Actor ID");
        howManyActors = JOptionPane.showInputDialog("How many actors?");
        numberOfActors = Integer.parseInt(howManyActors);
        if ((actorID == "M1") || (actorID == "m1"))
        {
            thisActor = new MBarrier();
            for (int i=0; i<numberOfActors; i++)
            {
                addObject(thisActor, 35, 35);
            }
        }
    }
This is the most recent applicable code I have. I have tried both M1 and m1 and neither spawn anything now.
danpost danpost

2015/4/10

#
Change line 19 to this:
if ("M1".equals(actorID.toUpperCase())) {
You need to compare for equality the contents of the String objects, not the objects themselves. Remove line 21 and put 'new MBarrier()' within the parameter of the 'addObject' call. Add an offset to one of the placement coordinates '+i*30' for example; or to both (for testing purposes).
decadence18 decadence18

2015/4/10

#
danpost wrote...
Change line 19 to this:
if ("M1".equals(actorID.toUpperCase())) {
You need to compare for equality the contents of the String objects, not the objects themselves.
Now I get the original one actor back. When using the "println" it still returns the right number for numberOfActors; now it just returns one for the second "println" statement, no matter what numberOfActors is.
danpost danpost

2015/4/10

#
Re-read my last post (I had edited it).
decadence18 decadence18

2015/4/10

#
danpost wrote...
Re-read my last post (I had edited it).
Now it works, both with staggering the X coordinates and without. I have no idea what combination of things worked (I'm assuming the combination of the content comparison and the "new MBarrier()"), but it seems to work now. Any idea what was wrong before? Just for future reference.
danpost danpost

2015/4/10

#
The 'new MBarrier' line outside the 'for' loop caused one (the same) object to be placed repeatedly within the 'for' loop. With it inside the 'for' loop, you are placing a new (different) one each time through the loop. The use of the 'equals' method allowed the 'for' loop to execute at all, as by using '==', the result of the condition was ALWAYS false.
decadence18 decadence18

2015/4/10

#
danpost wrote...
The 'new MBarrier' line outside the 'for' loop caused one (the same) object to be placed repeatedly within the 'for' loop. With it inside the 'for' loop, you are placing a new (different) one each time through the loop. The use of the 'equals' method allowed the 'for' loop to execute at all, as by using '==', the condition was ALWAYS false.
I see. I'll try to avoid that next time. Up until this point in our code, we were mostly comparing integer values so I would just convert them with parseInt and then compare them to make sure the value isn't null or empty.
There are more replies on the next page.
1
2