Hey there, im looking for help trying to create a recoil or pushback from enemy hits or encounters, where; if the player gets damage they are pushed back. I feel like I once saw a guideline for this but now that im looking for it I cant find it. Thanks!
What have you tried so far? Can you share any code or relevant events?
- To move the player you want to use
goto
- To work out the new coordinates to move the player to you need to start with their current coordinates
event.px
andevent.py
- You'll also probably need the coordinates of the enemy. If an event is in the enemy tile script you can use
event.x
andevent.y
to get the coordinates of the enemy tile
Once you have the simplest version of knockback working then you can start improving it to deal with situations like the player being knocked back into a solid tile.
Hope that helps!
Thank you for the reply, been swamped a bit so I haven't been able to give the a shot, but when I get a chance I will let you know how it goes.
hmm well, I'm not sure what you're doing, but let's say you have some enemy located at enemy_x and enemy_y then, you could do something like this:
on knockBackPlayer do
x = event.px
y = event.py
if enemy_x>x then
x--
end
if enemy_x<x then
x++
end
if enemy_y>y then
y--
end
if enemy_y<y then
y++
end
if x>=0 then
if x<25 then
if y>=0 then
if y<15 then
is_wall = solid x,y
if is_wall==0 then
goto x,y
else
// you could put in some special code for being pushed into solid things here
end
end
end
end
end
end
this will only work if only adjacent enemies can push back (it isn't looking for diagonal pushes etc)
so it checks which side the enemy is on and changes the player x/y accordingly.
it also checks if they would be pushed into a solid object or into a wall before it actually does the forced movement (the goto
statement)
im curious, because reading this over it looks like it should work, but when i run it nothing happens. so this script should go in the player script and then on the move update (the same time damage player is called), it should call the knockback player? right? not sure why this isnt working.
actually kind of got this running, except my enemies do run diagonally so i may have to kind of tweak that. but the missing sauce was
during the damage player section of the enemy tile adding the wait between calling damage and calling pushback seemed to fix the issue.
if newEnemyX==event.px then
if newEnemyY==event.py then
damage = 1
tell event.player to
call "damagePlayer"
sound "push"
wait 0.25 then
call "knockBackPlayer"
end
end
end
end
end
Yeah lots of little things need the game to run the next frame to work; you'll get used to when you need it or not. You can actually make it much less than 0.25 actually, I usually use 0.1 but in theory, 0.05 should work as that's the fastest a frame should come out. You may want to put ignore
before the wait and listen
at the end so that the player can't actually do anything during the wait, unless you like that idea.