Telash Posted February 5, 2014 Share Posted February 5, 2014 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 More sharing options...
Telash Posted February 5, 2014 Author Share Posted February 5, 2014 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 More sharing options...
Robert O'Rourke Posted February 7, 2014 Share Posted February 7, 2014 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 More sharing options...
Telash Posted February 10, 2014 Author Share Posted February 10, 2014 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 More sharing options...
Robert O'Rourke Posted February 10, 2014 Share Posted February 10, 2014 No problem, maybe I misunderstood how the touching property was supposed to work. To be honest I'm new to phaser.js as well! Link to comment Share on other sites More sharing options...
Recommended Posts