X and Y Positions
CreatiCode last edited by info-creaticode
When we add a sprite, we often need to tell the computer where to place it. If we just say “place Cody on the top right corner”, that could mean many different locations:
To be more exact, we can use “X position” to tell the computer how far left or right is the sprite. X position is a number between -240 and 240:
- If x position is 240, the sprite’s center will be at the right edge of the stage;
- If x position is -240, the sprite’s center will be at the left edge of the stage;
- If x position is 0, the sprite’s center will be in the middle of the stage;
You can try different values for the x position using the input box below the stage. Note that the red dot is the center of the sprite.
Similarly, we can use “Y position” to specify how high or low is the sprite’s position, which can be any number between -180 and 180:
- If y position is 180, the sprite’s center will be at the top edge of the stage;
- If y position is -180, the sprite’s center will be at the bottom edge of the stage;
- If y position is 0, the sprite’s center will be in the vertical middle of the stage;
You can also try different values for the y position using the input box below the stage.
To help you find the correct position values, you can use a backdrop called “Xy-grid”:
This grid contains 2 lines that intersect at the center of the stage, where x is 0 and y is 0. The orange line is the “X-axis”, and the blue line is the “Y-axis”.
To move a sprite to an X position, you can use the “set x to” block in the Motion category. The x position below the stage also changes to reflect the change.
Similarly, you can move a sprite to any Y position using the “set y to” block. Note that when you change the Y position of a sprite, its X position will not be affected.
You can also set both the X and Y positions at the same time, using the “go to x y” block. It has the same result as running “set x to” then “set y to” blocks.
You can also create an animation when you move a sprite, using the “glide to x y” block. You can specify an additional input for how long it takes the sprite to move there.
If you need to find out the current X and Y positions of the sprite, you can use these 2 reporter blocks: