Hi there
Have you tried to put the ph_setgravity to the begining of the function (first ph-statement)?
you also could try to set up a wait(1); at the end of the function (if the function is not called within a while).

Dont know if it helps but I hope so

cheers