### Author Topic: How to bounce enemy back when hit? [SOLVED!]  (Read 1662 times)

#### Brice

• Member
• Posts: 9
• Karma: +0/-0
##### How to bounce enemy back when hit? [SOLVED!]
« on: Wed, Feb 9, 2011 »
My first intermediate question on the Flixel forums!

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)

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; }`
« Last Edit: Wed, Feb 16, 2011 by Brice »

#### Daikath

• Member
• Posts: 23
• Karma: +0/-0
##### Re: How to bounce enemy back when hit?
« Reply #1 on: Thu, Feb 10, 2011 »
Did you try setting their acceleration instead of their velocity?

#### tkshredder

• Member
• Posts: 20
• Karma: +0/-0
• Hit the ground running, with a plan
##### Re: How to bounce enemy back when hit?
« Reply #2 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:

#### wonkytonk

• Member
• Posts: 73
• Karma: +0/-0
• don't panic
##### Re: How to bounce enemy back when hit?
« Reply #3 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

#### xhunterko

• Contributor
• Posts: 449
• Karma: +0/-0
##### Re: How to bounce enemy back when hit?
« Reply #4 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.

#### Brice

• Member
• Posts: 9
• Karma: +0/-0
##### Re: How to bounce enemy back when hit?
« Reply #5 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!

#### OmegaStorm

• Member
• Posts: 36
• Karma: +0/-0
##### Re: How to bounce enemy back when hit?
« Reply #6 on: Tue, Feb 15, 2011 »
You could always use a timer and override the velocity for a certain duration.

#### Brice

• Member
• Posts: 9
• Karma: +0/-0
##### Re: How to bounce enemy back when hit?
« Reply #7 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!