I am Having Trouble With Wild Horses

I am having a problem with the level Wild Horses and I keep getting an error that says: cannot read property β€˜x’ of undefined.
Here is my code:

while True:
    # How do you find the nearest friendly unit?
    # horse = ?
    horse = hero.findFriends()
    if horse:
        x1 = horse.pos.x - 7
        x2 = horse.pos.x + 7
        if x1 >= 1:
            # Move to the horse's y position but use x1 for the x position.
            hero.moveXY(horse.pos.y, x1)
        elif x2 <= 79:
            # Move to the horse's y position but use x2 for the x position.
            hero.moveXY(horse.pos.y, x2)
        distance = hero.distanceTo(horse)
        if distance <= 10:
            # Move to the red x to return the horse to the farm.
            hero.moveXY(27, 54)
            # Move back out into the pasture to begin looking for the next horse.
            hero.moveXY(45, 37)

Here is the formatting you should use for moving to an (x, y) location:

hero.moveXY(x, y) # (x coordinate, y coordinate)

You are putting the y-coordinate first in lines 12 and 15. Try switching those around.

I still get the error

and if this helps it says it’s on line 7

Just looked at your code again and noticed that you are checking the x-property of an array of friends. Try using a for-loop. Or use horse = hero.findNearest(hero.findFriends()) so it only returns the nearest one, and keep the if-statement.

1 Like