i suggest to work with finite state machine. http://compilgames.net/wiki/doku.php/gdevelop/tutorials/howtofinitestatemachine