How to create 'bounce' effect after getting hit by spike?

so i’m trying to create a proper ‘freeze in mid-air’ then do a ‘bouncing fall’ off the screen like in sonic the hedgehog HERE :

instead of using “sprite.setY ( )” for creating a “get hit by spike” effect, how can I create a ‘bouncing’ effect based on arcade physics? i need control of how high or low the player goes flying & WHICH direction the player goes flying. i tried setVelocity , that didn’t work.

Any help as always is GREATLY appreciated!

anyone can help?

It’s just setVelocity(…). But you have to disable input and collisions after to let him fall off the screen.

@samme:: how would I do that???

this.input.enabled = false;
sprite.body.checkCollision.none = true;

@samme : Why is it when I’m holding down a movement key or jumping key, switch tabs then go back, OR I disable key input completely, is my character still moving / jumping automatically even though they should not be able to move at all? How do I fix this?

This is my 'playerHit ( )' function :

/**
  * playerHit resets the player's state when it dies from colliding with a spike
  * @param {*} objData - any Data we may need
*/

playerHit : function ( __objData ) {

	this.__objData = __objData;

	this.__input = this.__objData.input;
	this.__map = this.__objData.map;
	this.__layerName = this.__objData.layerName;
	this.__player = this.__objData.player;
	this.__hitObject = this.__objData.collidedWithObject;
	this.__set_x = this.__objData.setx;
	this.__set_y = this.__objData.sety;
	this.__xvel = this.__objData.xvel;
	this.__yvel = this.__objData.yvel;
	this.__tweens = this.__objData.tweens;

	if ( typeof ( this.__objData ) !== 'object' ) { return console.error ( 'ERROR :: { Please ensure you are using an `object` for `objectData` & try again } !' ); }

	// Set velocity back to 0

	this.__player.setVelocity ( 0, 0 );

	// Disable input & Collision

	this.__input.keyboard.enabled = false;
	this.__player.body.checkCollision.none = true;

	// Stop Player Animation{s}

	StopAnimation ( this.__player );

	// Set `Player` Animation to `idle`

	PlayAnimation ({
		sprite : this.__player, 
		animName : 'idle', 
		loopAnim : true, 
	});

	// Set Velocity

	if ( this.__set_x === true ) { this.__player.setVelocity ( this.__xvel, 0.0 ); }
	if ( this.__set_y === true ) { this.__player.setVelocity ( 0.0, this.__yvel ); }

	// OR set BOTH Velocities

	if ( this.__set_x === true && this.__set_y === true )

	{

		this.__player.setVelocity ( this.__xvel, this.__yvel );

	}

	// Set the visibility to 0 i.e. hide the player

	this.__player.setAlpha ( 0 );

	// Add a tween that 'blinks' until the player 
	// is gradually visible

	let tw = this.__tweens.add ( {
		targets : this.__player, alpha : 1, duration : 100, 
		ease : 'Linear', repeat : 3, 
	} );

}, 

This is how I call it :

this.__physics.add.overlap ( this.__entity.sprite, this.__spikes, function ( objectA, objectB ) {

	this.__gameData.playerHit ({
		input : this.__input, 
		map : this.__map, 
		layerName : 'objects', 
		player : objectA, 
		collidedWithObject : objectB, 
		setx : false, 
		sety : true, 
		xvel : 0.0, 
		yvel : -1000, 
		tweens : this.__tweens, 
	});

	StopAnimation ( this.__entity.sprite );

}, null, this );

Because when input is disabled it’s not processing key up events either.

Set a flag like playerDead instead and use that to ignore player input.

@samme : how?

However you’re moving the player for keyboard input, stop doing it when playerDead is true.

@samme : ok, so I disabled collisions on the player’s body with :

this.__player.body.checkCollision.none = true;

which works fine, but the problem is it doesn’t disable collision for my ground…

this.__make = this.make;
this.__input = this.input;

// Create our Tilemap for Level { 1 }
this.__tileMap = this.__make.tilemap ({ key : 'map-1-1' });

// Add Tileset to Tilemap for Level { 1 }
this.__tiles =  this.__tileMap.addTilesetImage ( 'platformPack_tilesheet', 'lvl-1-1' );

// Create `Dynamic` `ground` Layer for Level{s} { 1 }
this.__groundLayer = this.__tileMap.createDynamicLayer ( 'ground', this.__tiles );

I guess that’s a bug. You can do

this.__player.body.checkCollision.left = false;
this.__player.body.checkCollision.right = false;
this.__player.body.checkCollision.up = false;
this.__player.body.checkCollision.down = false;

@samme :

However you’re moving the player for keyboard input, stop doing it when playerDead is true.

How?

if (!playerDead) {
  updatePlayerMovement();
}

@samme : just did this & it’s still not stopping the x-velocity from firing. i need it to stop the character in mid air, activate the bounce effect ( y-velocity ), then fall down through the level.

@samme : setting a gameOver variable works WONDERS. But I have a small issue. For some reason even though I do :

// Set `velocity` back to `0`
this.__player.setVelocity ( 0, 0 );
this.__player.setVelocityY ( -600 );

inside the loop ( which is where the 'this.__player.setVelocity ( 0, 0 );' needs to be ), I get the player doing the fall correctly, but the y-velocity keeps on getting faster & faster. The problem is, I NEED the velocity set to 0 in order to keep the player from not moving to the sides AND in the air when running into a spike.

Any help is as usual, GREATLY appreciated!!! Thanks so much!

@samme : For the life of me, I can’t figure out why it keeps speeding up. It should stay at one terminal velocity.

Gravity?

@samme : ? it doesn’t speed up in your demo.

I guess I don’t understand the problem you’re describing. Visually, what happens now when your player hits a spike and what do you want to happen instead?