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

2021/4/29

x value

BlueHand BlueHand

2021/4/29

#
What is wrong with my code?
for (x = 60; x <= world.getwidth() - grass.getwidth(); x = x + 40)
        {
            Obstacle border = new Obstacle();
            border.setImage("grass.png");
            addObject(border,x,60);
        }
It's a for loop that is meant to put obstacle actors (that have the image grass.png) on the top of the world. "x" is the x coordinate. I compiled the code and it says that it can't find the variables x, world, and grass. Sorry if it is confusing.
danpost danpost

2021/4/29

#
First, you need to declare the variable "x". Next, with code being in your world class, if anything "world" should be "this". Finally, "grass" needs to refer to a Grass object. However, you seem to already know its width.
for (int x=60; x<=getWidth(); x+=40) // "x<=this.getWidth()" would also work
{
    Actor border = new Obstacle();
    border.setImage("grass.png");
    addObject(border, x, 60); // "60"?  maybe you meant 20?
}
You need to login to post a reply.