I want to move the player by pressing down on the mouse and moving around by dragging. The player won't move to the position of the mouse. Instead it should be relative to the players current position.
https://youtu.be/oGSpX6GEYa0?t=5m55s
The way this works, your finger can be anywhere on the screen, and when you move down, the player moves down. This is what I have so far, but it's not as elegant as I want it to be. For example, if your mouse is not moving, it still applies acceleration.
public static inline var EASING:Float = 0.1;
private var mouseX:Float;
private var mouseY:Float;
private var vx:Float;
private var vy:Float;
private var distance:Float;
/**
* Function that is called once every frame.
*/
override public function update(elapsed:Float):Void
{
if (canMove == false) {
return;
}
if (FlxG.mouse.justPressed) {
this.mouseX = FlxG.mouse.screenX;
this.mouseY = FlxG.mouse.screenY;
}
// if we are pressing down, we should move ronnie
if (FlxG.mouse.pressed)
{
vx = getVelocityX();
vy = getVelocityY();
distance = getDistance(vx, vy);
if (distance > 0) {
this.x += vx * EASING;
this.y += vy * EASING;
}
}
if (FlxG.mouse.justReleased) {
//
}
super.update(elapsed);
}
/**
*
* @return
*/
function getVelocityX():Float
{
return FlxG.mouse.screenX - this.mouseX;
}
/**
*
* @return
*/
function getVelocityY():Float
{
return FlxG.mouse.screenY - this.mouseY;
}
/**
* Return distance
*
* @return
*/
function getDistance(vx:Float, vy:Float):Float
{
return Math.sqrt(vx * vx + vy * vy);
}