| X Position | |
x position | |
| Category | Motion |
| Type | Reporter |
| Introduced in | 11Jun04 (0.x) |
The x position block is a reporter block and a Motion block. The block holds its sprite's x value, the sprite’s horizontal location on the stage. The block can be displayed as a Stage Monitor.
Example Uses
Some common uses for the x position block are:
- Triggering actions via screen position:
forever
wait until <(x position) > [220]>
change [Hits v] by (1)
end
Another use is setting values based on a movable slider without using Scratch's default slider, with the script setting the value to the slider's X position:
set [worms v] to (x position)
- Comparing the X position with a record to check for movement:
when green flag clicked
set [x_pos prev v] to (x position)
set drag mode [draggable v] //makes the sprite easier to move
forever
if <not <(x_pos prev) = (x position)>> then
say [My X position moved!] for (2) seconds //if the x position moved, say something
set [x_pos prev v] to (x position)
end
end
- Constantly storing a sprite's X movement so it can be re-enacted later
when green flag clicked //script only works in a sprite
delete all of [x positions v]
delete all of [y positions v]
say [Move your mouse and I will reenact the movement!] for (2) seconds
say [Go!] for (1) seconds
repeat (50)//records 50 coordinates at a rate of one for every 0.1 seconds
go to (mouse-pointer v)
add (x position) to [x positions v]
add (y position) to [y positions v]
wait (0.1) seconds
end
set [counter v] to [1]
say [I will reenact it for you now!] for (2) seconds
repeat (50)
go to x:(item (counter) of [x positions v]) y:(item (counter) of [y positions v])
wait (0.1) seconds
change [counter v] by (1)
end
- Changing a sprite's speed based on its coordinates
Workaround
- Main article: List of Block Workarounds
The block can be replicated by finding the X position with the () of () sensing block:
([x position v] of (wanted sprite v))
However, as this workaround requires the ([ v] of ( v)) block, it cannot be used to get the X position of the current sprite and must be done from another sprite, unless this workaround is used:
([x position v] of (join [wanted sprite][]))