If you always set speed to 10 and only set speed to -10 when the object is over the border, the object will not return, but keep staying at the border! Try this:
If obj.xPos<0 Then obj.SetSpeedX(10)
If obj.xPos>450 Then obj.SetSpeedX(-10)
This needs a definition of SpeedX when creating the object.
Method New()
...
Self.SpeedX=10
And it works only if there is anywhere a line with...
obj.xPos=obj.xPos+obj.SpeedX
By the way... I would encapsle all this things into the Object-Class
OnRender()
obj.Draw()
....
End
Class Object
Field SpeedX%=10, xPos%, yPos%
Method Draw()
DrawRect xPos,yPos,5,5
xPos=xPos+SpeedX
CheckBorders
End
Method CheckBorders()
If xPos<0 Then SpeedX=10
If xPos>450 Then SpeedX=-10
End
|