We use the expressions:
CanvasToLayerY("layer-name",0,0) <-- Top of the screen
CanvasToLayerY("layer-name",WindowWidth,WindowHeight) <-- Bottom
CanvasToLayerX("layer-name",0,0) <-- Left
CanvasToLayerX("layer-name",WindowWidth,WindowHeight) <-- Right
This will reliably give you the edge coordinates of the screen in any "fullscreen mode." Just keep awareness of scaling of the layer involved. I typically use a 0,0 parallax layer such as "UI."
Then just do what LS says above to keep it on screen.
Why not use the viewport expressions?
They do exactly the same..
Using the (0) for layer zero (1) for layer 1, you can reference the correct layer the sprite is on so it will consider the scaling and paralax..