Jump to content

parabolic knock back


Recommended Posts

Hey guys,


i'm fairly new to phaser, in-fact I am still mostly just modifying the tutoral star collection demo to see what I can do.


I have successfully added a standard enemy which walks backwards and forwards on a set path, as well as a basic 3 life system, the problem I am having though is the generic parabolic knock back effect. Everything I try results in my player sprite either shooting off to the corner of the canvas or giving me some kind of weird zero gravity style knock back which looks plain odd.

Does anybody have an example of how this works in phaser that they wouldn't mind sharing? (also if anyone knows anything about how to determine which direction the knock back should trigger that would also be great.)

Thanks in advance guys :)

Link to comment
Share on other sites

Hey, I have figured out the knock back to a degree:

function knockedBackAnimation() {	player.animations.stop();	player.frame = 11;	player.body.sprite.alpha = 0.5;		var distance = 75;		if(knockedTo == 0)	{		knockedTo = (player.body.x - distance);		immortal = true;	}			player.body.velocity.x = -500;		if(player.body.x <= (knockedTo + distance/2))	{		player.body.velocity.y = 100;	}	else	{		player.body.velocity.y = -100;	}		if(player.body.x <= knockedTo)	{		player.frame = 6;		knockedTo = 0;		knockback = false;	}}

I am still having issues in how I am meant to determine which direction to knock back happens however.

Link to comment
Share on other sites

Hi, I think you need to look at the player.body.touching property. It tells you if the collision was up, down, left or right eg. The following pseudo-code might help you get on the right track.

if ( player.body.touching.left ) {   // direction to knockback is to the right}if ( player.body.touching.right ) {   // direction to knockback is to the left}// landing on an enemyif ( player.body.touching.down ) {   // kill enemy OR   // calculate direction   // player.body.x > enemy.body.x = knockback to right   // player.body.x <= enemy.body.x = knockback to left}

There is also a player.body.wasTouching property that contains the previous touching data. In case the touching directions are no longer true and you need to know what it was.

Link to comment
Share on other sites

Hey Robert,

Thanks for your reply. I have looked into the 'touching' property but unfortunately I was never able to get it to give me an accurate result, it only ever read out that touching was down (the floor) and never seemed to register the collision with the 'enemy'.

I am currently using a pseudo solution for now, as follows:

function knockedbackDirection(Obj1, Obj2) {	var direction = '';	if(Obj1.center.x < Obj2.center.x)	{		direction = 'left';	}	else	{		direction = 'right';	}		return direction;}

As it is a 2D game, I just determine which side the 'enemy' is located in relation to the 'player', the knock back is always away from the 'enemy'.


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...