# Flixel Forums

## development => help => Topic started by: Brice on Wed, Feb 9, 2011

Title: How to bounce enemy back when hit? [SOLVED!]
Post by: Brice on Wed, Feb 9, 2011
My first intermediate question on the Flixel forums!   ;D

I'm trying to write a reusable function where when someone gets hit they are bounced backwards.  I'd like this to happen for enemies as well as the player.

Here's an example of the exact behavior I want from Legend of Zelda: Link to the Past (skip ahead to 6:15) (http://www.youtube.com/watch?v=UOoXNVYI5u4)

I've written a function that takes in what I call a ball and bat object (both FlxSprites), and the velocity.  It calculates which direction to throw them in and the applies the velocity to the ball object.

I thought that using velocity to do this would be the simplest way, but it gives me two problems:
1) If an enemy is moving around the stage by the AI setting their velocity, then they don't bounce at all, because their update() just resets their velocity to what their AI is telling them to do.
2) If the player gets hit by an enemy while he's up against a wall, crazy collision things happen (cam goes off screen, player and enemy get stuck together, etc)

Does anyone know of a better way to implement the actual throwing once I know what direction to throw them in?  (Or better ways to implement enemy movement AI than manipulating velocity).

Thanks so much!  Here is my code if it would help to see:

Code: [Select]
` public function bounceBack(batObject:FlxSprite, ballObject:FlxSprite, hitVelocity:int = 500):void { var xThrow:int; var yThrow:int; if (ballObject.x < (batObject.x - batObject.width/2)) //Calculate which direction to throw them in xThrow = -hitVelocity; else if (ballObject.x > (batObject.x + batObject.width/2)) xThrow = hitVelocity; else xThrow = 0; if (ballObject.y < (batObject.y - batObject.height/2)) // Same for y yThrow = -hitVelocity; else if (ballObject.y > (batObject.y + batObject.height/2)) yThrow = hitVelocity; else yThrow = 0; ballObject.velocity.x = xThrow; // Smash them! ballObject.velocity.y = yThrow; }`
Title: Re: How to bounce enemy back when hit?
Post by: Daikath on Thu, Feb 10, 2011
Did you try setting their acceleration instead of their velocity?
Title: Re: How to bounce enemy back when hit?
Post by: tkshredder on Thu, Feb 10, 2011
I second the notion to set the acceleration in the x and y directions to modify these properties. Any luck?

You can add time codes to clips by appending #t=MmSs, where M = minute and S = seconds, like:

Title: Re: How to bounce enemy back when hit?
Post by: wonkytonk on Thu, Feb 10, 2011
you could also try setting the acceleration to 0 and then resetting the velocity, unless that's exactly what the two posts above are suggesting  :-*
Title: Re: How to bounce enemy back when hit?
Post by: xhunterko on Thu, Feb 10, 2011
You could always try this:

http://users.wpi.edu/~bmoriarty/imgd1001/tutorials/tut4.html

The example is for a platformer, but could be used for top down I'm sure. The enemies, and yourself, are knocked over when hit by a grenade blast. Should be some help.
Title: Re: How to bounce enemy back when hit?
Post by: Brice on Mon, Feb 14, 2011
No luck with the acceleration, it had the exact same effect as before...?

I'll try out the example posted by xhunterko tomorrow.  Thanks!
Title: Re: How to bounce enemy back when hit?
Post by: OmegaStorm on Tue, Feb 15, 2011
You could always use a timer and override the velocity for a certain duration.
Title: Re: How to bounce enemy back when hit?
Post by: Brice on Wed, Feb 16, 2011
I fixed it!  By adding a simple flicker and invulnerability period, the problem is solved, and the player has time to move.  If they don't they just sit there.

Thanks everyone for the help!