Every tick set position to
X : clamp( sprite.X, sprite.width / 2 , ViewportRight(0) - sprite.width/2)
Y : clamp( sprite.y, sprite.height / 2 , ViewportBottom(0) - sprite.height/2)
To be more accurate:
If the sprite has his origin at the center:
set position to
X : clamp( Self.X, ViewportLeft(Self.LayerName)+Self.Width/2 , ViewportRight(Self.LayerName) - Self.Width/2)
Y : clamp( Self.Y, ViewportTop(Self.LayerName)+Self.Height/2 , ViewportBottom(Self.LayerName) - Self.Height/2)