getDirection

Category:Turtle

getDirection()

Category: Turtle

Returns the current direction that the turtle is facing. Zero degrees is pointing up and the direction increases clockwise.

Sometimes the user controls the turtle and your app might need to know what direction the turtle is facing. The direction is a number between 0 and 359 degrees.

Examples


turnRight(randomNumber(359));
console.log(getDirection());

Turning on your Command Turn the turtle in a random direction, and display the direction, on every user click of a button.

// Turn the turtle in a random direction, and display the direction, on every user click of a button.
textLabel("direction", "direction: " + getDirection());
button("random-direction", "Random Direction");
onEvent("random-direction", "click", function() {
  turnRight(randomNumber(359));
  setText("direction", "direction: " + getDirection());
});

Syntax

getDirection()

Parameters

getDirection() does not take any parameters.

Returns

Returns an integer between 0 and 359 representing the direction the turtle is facing. [/returns]

Tips

  • The values of the compass directions are: North:0, East:90, South:180, West:270.

Found a bug in the documentation? Let us know at documentation@code.org

Found a bug in the documentation? Let us know at documentation@code.org