There are two ways I can think of.
You can compare the X of the enemy every time the enemy leaves the layout, and then depending where it is you change it's direction.
Or, I don't know if this is the best way to do it, but this is how I do it, you can create invisible sprites that doesn't interfere with the game, and then you set the enemy to change directions every time it collides with the invisible object.