In this activity, we will use variables to track and display a sprite's position on the Stage in real time. As the sprite moves around, the variables will update automatically to show its current X and Y coordinates.
Every point on the Stage has an X coordinate (horizontal position) and a Y coordinate (vertical position). The center of the Stage is at x: 0, y: 0.
Setting Up
- Open PictoBlox and create a New File.
- Select Block Coding as the coding environment.
- Choose any sprite you like — we will use Tobi for this activity.
Creating the Variables
- Go to the Variables palette and click Make a Variable.
- Create a variable called X Position.
- Create another variable called Y Position.
- Make sure both variables are set to For all sprites.
- Enable Show variable for both so they are visible on the Stage.

Building the Script
Now let's build the script that continuously updates the variables to reflect the sprite's current position. You will find the blocks in the mentioned palettes:
- When flag clicked — Events
- Forever — Control
- Set (X Position) to () — Variables
- Set (Y Position) to () — Variables
- x position — Motion (used inside the Set block)
- y position — Motion (used inside the Set block)

Making the Sprite Move
To see the variables change in real time, let's make the sprite move using the arrow keys. Add a second script to control the sprite's movement:
- When right arrow key pressed — change x by 10
- When left arrow key pressed — change x by -10
- When up arrow key pressed — change y by 10
- When down arrow key pressed — change y by -10

Running the Script
- Click the green flag to start the program.
- Use the arrow keys to move the sprite around the Stage.
- Watch the X Position and Y Position variables update in real time as the sprite moves!
- Notice how X increases when moving right and decreases when moving left, while Y increases when moving up and decreases when moving down.

Understanding the Stage Coordinates
Save your file with the name Tracking Position before moving on to the next activity!

.webp&w=3840&q=75)
.webp&w=3840&q=75)
.webp&w=3840&q=75)
.webp&w=3840&q=75)
.webp&w=3840&q=75)