You’re trying to make a really simple way of making the player step back, then return, but that’s a little too simple. I could send a screenshot of how to do this, but trying to figure it out on your own will be really beneficial in the future.
Think of what you have,
- A detection for when the enemy hits the player.
- A message telling the player to attack.
Think of what you want to happen,
- The player to move back 5 pixels when hit
- The player moves back to the starting position after attacking.
You have Message(fight) → Move Forwards. You can add onto that, → Switch(on) and a different Switch(off)
That switch will do this Animation(attack)[Done) → Switch → 240 Position(X)
That will bring the player back to the original position. After that have the switch turn itself off. and the other switch turn on
If the player collides with the enemy just move the player back 5 pixels
Collision(Enemy) → Switch → Position(X+)
a note, a Toggle Switch can be used over 2 Switches, but switches are much more flexible.
The code you have in the player and enemy I could probably make in 2 minutes, but that’s because I know what I want to make (Player move forward, attack, then move to start position. Enemy attack then move to start position). Knowing what you still need to make and what you already have is very important, it allows me to continue making things while never slowing down because I’m constantly thinking of the next thing that needs to be done.
You don’t always have to plan everything out, but it’s useful to realize what is going to be needed. If I’m making an inventory system I don’t know right as I begin how it will look, but I do know that I will need a List and a List Modify, so I can place those behaviors without knowing what they’ll do, but know they will be used. Then I just go through the steps of adding an item to the List, removing it via the List Modify, etc.