getX
Category:Turtle
The x coordinate is the distance from the turtle to the left of the screen.
Examples
Basic Example
var xLocation = getX();
console.log(xLocation);
moveTo(100, 100);
console.log(getX());
To the Right
Move the turtle 50 pixels to the right.
// Move the turtle 50 pixels to the right.
var newX = getX() + 50;
moveTo(newX, 100);
Am I off the screen?
Check whether the turtle has moved off the right side of the screen.
// Check whether the turtle has moved off the right side of the screen.
function isOffRight(){
if (getX() > 320) {
return true
} else{
return false
}
}
turnRight(90);
for(var i=0; i<10; i++){
moveForward(50);
console.log("Am I off the screen? "+ isOffRight());
}
Syntax
getX();
Returns
Returns a number representing the current x coordinate in pixels of the turtle within the app display.
Tips
- The screen default size is 320 pixels wide and 450 pixels high, but you can move the turtle off the screen by exceeding those dimensions.
- The turtle can be moved off the screen so getX() can return a negative number if the turtle is off the screen to the left and getX() can return a number greater than 320 if the turtle is off the screen to the right.
Found a bug in the documentation? Let us know at support@code.org.