Help with Wild Horses!


#1

I really need help with the level Wild Horses. :cry: Here is my code:

loop {
    // How do you find the nearest friendly unit?
    // horse = ?
    var horse = this.findByType("horse");
    if (horse) {
        var x1 = this.pos.x - 7;
        var x2 = this.pos.x + 7;
        var y = this.pos.y;
        // var y1 = this.pos.y - 1;
        if (x1 >= 7) {
            // Move to the horse's y position but use x1 for the x position.
            this.moveXY(x1, horse.y);
            // x1 += 7;
            // this.moveXY(x1, y);
            
            
        } 
        else if (x2 <= 79) {
            // Move to the horse's y position but use x2 for the x position.
            this.moveXY(x2, y);
            // x2 -= 7;
            // this.moveXY(x2, y);
        }
        var distance = this.distanceTo(horse);
        if (distance <= 15) {
            this.say("Whoa");
            // Move to the red x to return the horse to the farm.
            this.moveXY(27, 54);
            
            // Move back out into the pasture to begin looking for the next horse.
            this.moveXY(28, 18);
        }
    }
}

#2

First off you didn’t put the prefix var before horse and since the only entities in the field are horses you can simply find the nearest friend to locate a horse. Next this.pos.x isn’t the same as horse.pos.x when the level originally loads you will find this.pos.x. The x1 and x2 variables are meant to you keep you from walking into a wall forever if a horse is just above a wall and you try to move in 7 units below it or vice versa. Also you need to write horse.pos.y, not horse.y.


#3

Thank you! :slight_smile: