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

2021/4/28

How to create a “border”

BlueHand BlueHand

2021/4/28

#
Hello. I have an obstacle class which has three images, one being grass.png. This object will be used to make wall at the top and bottom of the world. A for() loop will be used. How do I make the for() loop?
RcCookie RcCookie

2021/4/28

#
A for loop follows this system:
for(<counter variable init>; <condition to continue>; <counting>) {
    // loop code
}
Here’s a normal for loop printing the numbers 0-9. Usually the counting variable is called i (short for index, iterator,…):
for(int i=0; i<10; i++) { // i++ is the same as i = i + 1;
    System.out.println(i);
}
In your case, you may want to use a slightly different loop:
// Inside of your world class
for(int i = <grassWidth> / 2; i < getWidth(); i += <grassWidth>) {
    addObject(new Grass(), i, getHeight() - <grassHeight> / 2);
    Grass topGrass = new Grass();
    topGrass.turn(180);
    addObject(topGrass, i, <grassHeight> / 2);
}
Replace <grassWidth> and <grassHeight> with the width and height of your grass actor’s image.
You need to login to post a reply.