If you forgive the pun it smacks of a poor connection.
Striking with the hammer makes the connection good again.
Vibration of the engine sometimes makes it bad again.
This is only my guess...
Perhaps next time it happens you could wriggle wires in an attempt to find where the bad connection is. A failure in the motor itself is more than likely to be permanent and not correctable by bashing. Another guess...
Peccavi
February 2009