0
\$\begingroup\$

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);
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Here's one very simple way to do it:

package;

import flixel.FlxG;
import flixel.FlxSprite;

class Player extends FlxSprite
{
    public static inline var VELOCITY:Float = 4;

    public function new()
    {
        super();
        // enforce subpixel-rendering for smoother movement
        pixelPerfectRender = false;
    }

    override public function update(elapsed:Float):Void 
    {
        velocity.x = getDistanceX() * VELOCITY;
        velocity.y = getDistanceY() * VELOCITY;

        super.update(elapsed);
    }

    function getDistanceX():Float
    {
        return FlxG.mouse.x - x;
    }

    function getDistanceY():Float
    {
        return FlxG.mouse.y - y;
    }
}

This doesn't take FlxG.mouse.pressed into account at all, but I think that makes sense for this style of input. You wouldn't want the movement to stop abruptly.

\$\endgroup\$
2
  • \$\begingroup\$ That works quite well, although if it were touch based, we would need touch press? \$\endgroup\$
    – SMKS_ARTS
    Commented May 25, 2015 at 8:50
  • \$\begingroup\$ Not necessarily, OpenFL fires mouse events for the first touch. So unless you need multitouch, you're fine using FlxG.mouse. \$\endgroup\$
    – Gama11
    Commented May 25, 2015 at 8:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .