Basically, there are 3 different range limited types in graphics programming:
1. It won't go beyond a certain limit but stay at the nearest point of the limit
If Position < LowestLimit then Position := LowestLimit;
If Position > HighestLimit then Position := HighestLimit;
2. When it goes beyond a certain limit, it appears at the opposite side
If Position < LowestLimit then Position := HighestLimit;
If Position > HigestLimit then Position := LowestLimit;
3. When reaching a certain limit, it switches to the opposite direction
If (Position <= LowestLimit) or (Position >= HighestLimit) then
Direction := -Direction;
Position := Position + Direction;
Which one do you want? Perhaps you can show us your whole source code. Why my code not working questions, usually can be solved quickly if we can inspect the whole source code.