Scratch doesn't allow sprites to leave the stage. We can abuse this to find the width and height of a sprite. If a sprite is found outside of a border, it is clamped to the border. The border is something like 15 pixels smaller than the stage. Using this, we try to move a sprite outside of the border, then do some basic math with x position. See inside, it makes sense there. It does lose accuracy when the sprite has an offset, so make sure to center your sprites properly.