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

2015/5/17

Very confused Please assist

ipeefreely ipeefreely

2015/5/17

#
I'm supposed space 5 items evenly across the top of the world and 5 accross the bottom of the world. I am very confused about the for loop in general let alone how to place items using it. After reading the book that confused me this is the code I put in the animal class thinking it would set the world up but I keep getting errors. I tried to put the spacing int at 560 but I was redirected by my instructor to use getWidth instead. Even still I am still confused and please ask for your help
public void setup()
    {
        int numberOfActors = 5;
        int spacing = getWidth() / numberOfActors;
        addObject(getWidth());
        for (int i = 0; i < numberOfActors; i++)
        {
            addObject(new turtle, spacing / 30 + i * spacing, 280);
            addObject(new bug, spacing/30+ * spacing 280));
        }
    }
danpost danpost

2015/5/17

#
First, remove line 5. You cannot add an integer into a world and there is no 'addObject(int)' method that can be used. What is that line supposed to be doing?? The 'for' loop construct is fine. It is lines 8 and 9 that have a bunch of errors in them. For one thing, you need round braces after the name of the class when instantiating a class. Line 8 has only one comma in the parameter list as well as the fact that you have '+ *' in it, which makes no sense. After you remove those errors, you can then work on the placement of the object in the world (no matter what you do to remove those errors -- like, you can change the '+ *' to '+ 9827 *' or just '+' or '*' for now; at least that part should pass compilation, then). You are showing '280' at the end of both 'addObject' parameter lists in lines 8 and 9. Once you place the needed comma before the '280' on line 9, you should have both sets of objects (turtles and bugs) appearing at the same height in the world. Use 'getHeight' in a simple formula in place of the '280' in both lines to change where each row is placed vertically..
ipeefreely ipeefreely

2015/5/18

#
I tried to understand but I still dont. I've tried several codes & keep getting errors like "method addObject in class Animal cannot be applied to given types; required int; found: Face, int, int; reason: actual and formal argument lists differ in length I used this code (putting it in the Animal class) & got no syntax errors BUT it didnt add the objects to the world for me.

What code will work?...please

  public void addObject(int x)
    {
        int w;
        int numberOfActors = 5;
        int spacing = getWorld().getWidth() / numberOfActors;
        
        Face newFace;
        newFace = new Turtle();
        
        Me newMe;
        newMe = new Bug();
        
        World world;
        world=getWorld();
        
        world.addObject(newTurtle, 30, 1 );
        world.addObject(newBug, 30, 1 );
        for (int i = 0; i < numberOfActors; i++)
        {
            world.addObject(new Turtle(), spacing / 30 + spacing * i, 280);
            world.addObject(new Bug(), spacing / 30 + spacing * i, 280);
        }
    }
danpost danpost

2015/5/18

#
There are other problems as well -- but, the main one is where you are putting the code. It should not be in the Animal class, but in your World subclass and called from the constructor of that class. Also, a better name for the method might be 'addObjects' (plural), since it adds multiple actors into the world:
public void addObjects(int x)
{
    if (x < 1) return; // x must be positive
    int spacing = getWidth()/x;
    for (int n=0; n<x; n++)
    {
        addObject(new Turtle(), spacing/30+spacing*n, 280);
        addObject(new Bug(), spacing/30+spacing*n, 280);
    }
}
The placement of the actors within each row as well as where the rows are placed still need work. As is, the turtles will appear behind the bugs and the rows will be shifted a bit to the left.
You need to login to post a reply.