So when should the enemy follow the player and when not?
(what are the conditions?)
When should the enemy attack and when not?
Being close could be checked with distance(enemy.x,enemy.y,player.x,player.y) < Whatever amount of pixels you like..
If you only want to check in the horizontally abs(enemy.x-player.x) should do the trick..
Fleeing when hurt could be done by giving the enemy a boolean variable "Hurt" and setting it to true when hurt..
Adding the condition enemy is not hurt to your attack and follow events and adding an event what to do when enemy is hurt.
It's all about telling the computer when to execute certain actions (when all conditions are met)