This is an interesting challenge because the spikes could move either left or right from their initial position. I can think of many ways to go about it. A primitive solution would be to have 2 scripts: one for a left-aiming spike, one for a right-aiming one. Solarus 1.6 introduces custom entity properties, letting you configure whether a spike aims left or right by double clicking it and adding custom fields to each one.
But I think it makes more sense to assume that all spikes have a starting position, and they can move left *and* right depending on whether the hero triggers them in that direction. I don't think a Zelda game ever has a spike that can be triggered from both directions since they always start in a corner, but maybe this is how they actually are.
I'd consider storing the initial position of the enemy in the enemy itself, so you can come back to it. Something like this:
function enemy:on_created()
local x, y = self:get_position()
self._initial_x = x
self._initial_y = y
end
Then I'd probably use
enemy:on_update() (this gets called every frame) to constantly check the current position of the hero against the current position of the enemy, and if their Y values are within a certain range, activate the movement. Set the movement's angle based on difference between the X positions of the hero and the enemy.
There are also a few ways to determine when the enemy has reached the end and must reverse. I'd consider trying the
entity:on_obstacle_reached() event (walls are obstacles). Also, it's possible that
movement:on_finished() might be called for a straight movement if the entity hits a wall? I'm not sure, but if so that's easier.
Keep in mind that
movement:start() also has a second optional callback parameter, which would be functionally the same as movement:on_finished() in this case.
To reverse back home, simply use a
target movement to the enemy's initial x/y
It's a good idea to use print() a lot when writing a script like this so you can take things one step at a time. For example, make it print "hero crossed paths!" when the hero enters a zone that should trigger the enemy, before you even make the enemy actually move.
Since you said you were new to programming I tried to be thorough. Please feel free to write more questions, or say so if you can't figure it out and I can write out more parts of the script.