I have implemented a simple swipe to dash mechanism but it’s really buggy. Can someone recommend a better way to do it. This is how I have done it:
update: function() {
this.physics.arcade.collide(player, platforms);
var cursors = game.vjoy.cursors;
player.body.velocity.x = 0;
if (cursors.left) {
player.body.velocity.x = -200;
} else if (cursors.right) {
player.body.velocity.x = 200;
}
ptr = game.input.activePointer;
if(ptr.justPressed() == true)
{
startx = ptr.worldX;
starty = ptr.worldY;
}
if(ptr.justReleased()==true)
{
endx = ptr.worldX;
endy = ptr.worldY;
movx = endx - startx;
movy = endy - starty;
//If swipe starts from right half of the screen and magnitude of swipe is greater than 10
if(startx >= 512 && (movx>10 || movx<-10 || movy>10 || movy<-10))
this.moveplayer(movx,movy);
}
},
moveplayer: function(velx,vely)
{
player.body.velocity = 0;
velx = velx * 10;
vely = vely * 10;
movevelocity = new Phaser.Point(velx,vely);
player.body.velocity = movevelocity;
},
I even have an onTap listener which makes the player jump.
For some reason the dash is really weird, after the player stops moving in the direction of the dash it start going up. You can try it for yourself here http://princewael100.000webhostapp.com/Splash/index.html
Just swipe in any direction on the right side of the screen.