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

2015/5/12

HELLLLLLLLLP!!! (pretty please) For loop question!

GreenfootNewbie GreenfootNewbie

2015/5/12

#
Per the (below) instructions I did all this & my results were as they should be EXCEPT I am having difficulty with the "for loop" and my book kinda confuses me...well....a lot actually
To begin, there are 8 frowning faces lined up in a row at the top of the world and 8 "Me" objects arranged at the bottom, also in a row. The faces began moving in random directions. At the same time the Me's also started moving, but they moved straight ahead, turning only when the user pressed one of two keys...one key made them turn left and another key made them turn right...you may use any two keys you wish to implement this feature. When a Me object crossed the path of a Face object the Face's frown changed to a smile. In a somewhat unorthodox manner we have chosen to let the Face and Me sub-classes be Animals. The main reason for this was to expedite getting access to Actor methods and Animal methods. In real practice we would have made another class which would have replaced the Animal class with one which might be more pertinent to objects like Face and Me...maybe a Gliders class...but if we did that we would have many of the same methods in it as is in the new hypothetical Gliders class...so we are fudging the environment a little for the sake of simplicity. The same can be said for re-using the CrabWorld sub-class. In the CrabWorld constructor you will need to place the 16 objects. To get full credit you will need to use "for" loops. To equally space these objects horizontally you can call the getWidth() method and divide it by 8...this would be your starting placement...then your loop could add that same amount to arrive at the next 7 placements. To change frowny faces you have two possible options: 1.The Face looks for a Me object. Use the setImage method along with the canSee method. This is the easier approach. 2.The Me looks for the Face object. Inside Me's Act method you would declare an Actor object, say Friend, by calling the getOneObjectAtOffset method. Then you would use a statement like... friend.setImage("smiley2.png")...but this is the much harder concept to visualize and to pull off
I thought the code should look something like this based on the book
public void placeFace()
    {
        for (int i = 1; i < 8; i++)
        {
            int x = Greenfoot.getRandomNumber(getWidth()/8);
            int y = Greenfoot.getRandomNumber (getHeight()/8);
            addObject(new Face(), x, y);
        }
    }
but I cant figure outhow to place the 16 objects (8 faces straight across at the top & 8 "Me" images straight across at the bottom) using "for" loops with them being equally spaced horizontally using the getWidth() method. I am also confused where or how to declare the image int (based off the books guidance) since I already placed them in the World to make all the other functions work and my code doesnt address the placement of "Me"...would I do this in the World twice? or just put "addObject(new Me(), x,y); again for the "Me"...if so do I have to delete all the images I manually placed???
danpost danpost

2015/5/12

#
As far as deleting all the images manually placed -- no. Just re-compiling the project will remove them. As far as the code -- there was no mention in the instructions of any randomness; also, if they are to be placed horizontally across the top and across the bottom, there would be no change in the y coordinate for either the Me or the Face objects and dividing vertically by 8 has no bearing on the placement unless you want to use that value to place the rows of objects at that distance from the top and bottom edge. The 'for' loop parameters you are using will only produce 7 objects; the eighth iteration of the loop will have the condition fail. Start the value of the 'for' counter variable at zero instead of one. Remove all randomness. Use pre-determined distances within the 'for' loop. Use the value of the 'for' counter value in conjunction with those pre-determined distances in determining the placement of each of the eight objects. The instructions says to use 'for' loops (plural); so, use one loop for each row of objects. The pre-determined values can be used in both loops.
You need to login to post a reply.