question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create a Swipe Tracker

See original GitHub issue

I made this same issue previously, but on an alternate account where I would not be subscribed properly to this issue. I’ve remade it.

It would be useful to have a feature that allows you to track swipes on a mobile device. I’ve created my own using Excaliburs’ current features. I’m not sure if it’s suitable for the engines’ scope, however. The code is below for reference, but I believe that a utility for swipe gestures would be handy.

The math isn’t fantastic in this Swipe Tracker either. I’m still playing around with it.

import { Vector, Class, Engine, GameEvent } from "excalibur";
import { PointerDownEvent, PointerMoveEvent, PointerUpEvent } from "excalibur/dist/Input";

export enum STSensitivity {
    High,
    Medium,
    Low
}

export enum SwipeDirection {
    NONE,
    UP,
    RIGHT,
    DOWN,
    LEFT
}

export class SwipeEvent extends GameEvent<any> {
    constructor(public direction: SwipeDirection) {
      super();
    }
}

export default class SwipeTracker extends Class {    

    public sensitivity: STSensitivity;

    get swipeTimeDelay(): number { 
        return this.sensitivity == STSensitivity.High ? 50 : STSensitivity.Medium ? 100 :  200;
    }

    get swipePositionThreshold(): number { 
        return this.sensitivity == STSensitivity.High ? 80 : STSensitivity.Medium ? 120 :  180;
    }

    private _timerComplete: Boolean;
    private _initialPointerPosition: Vector;
    private _swipeEventEmitted: Boolean;

    constructor(_engine : Engine) {
        super();
        this.sensitivity = STSensitivity.High;

        _engine.input.pointers.primary.on('down', (pe) => {
            let castPE = pe as PointerDownEvent;
            this._initialPointerPosition = new Vector(castPE.screenPos.x, castPE.screenPos.x);
            this._swipeEventEmitted = false;
            this._timerComplete = false;

            setTimeout(() => {
                this._timerComplete = true
            }, this.swipeTimeDelay);
        });

        _engine.input.pointers.primary.on('move', (pe) => {
            let castPE = pe as PointerMoveEvent;
            this.emitSwipeIfNecessary(castPE);
        });

        _engine.input.pointers.primary.on('up', (pe) => {
            let castPE = pe as PointerMoveEvent;
            this.emitSwipeIfNecessary(castPE);
        });
    }

    private emitSwipeIfNecessary( pe: PointerMoveEvent | PointerUpEvent) {
        let swipeDirection = SwipeDirection.NONE;
        
        if (this._timerComplete && !this._swipeEventEmitted) {
            if (pe.screenPos.x > this._initialPointerPosition.x + this.swipePositionThreshold) {
                swipeDirection = SwipeDirection.RIGHT;
            } else if (pe.screenPos.x < this._initialPointerPosition.x - this.swipePositionThreshold) {
                swipeDirection = SwipeDirection.LEFT;
            } else if (pe.screenPos.y > this._initialPointerPosition.y + this.swipePositionThreshold) {
                swipeDirection = SwipeDirection.DOWN;
            } else if (pe.screenPos.y < this._initialPointerPosition.y - this.swipePositionThreshold) {
                swipeDirection = SwipeDirection.UP;
            }
            
            if (swipeDirection != undefined) {
                this.emit('swipe', new SwipeEvent(swipeDirection));
                this._swipeEventEmitted = true;
            }
        }
    }
}

Usage:

let tracker = new SwipeTracker(_engine);
tracker.on('swipe', (sw: SwipeEvent) => { console.log(sw.direction) })

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
eonarheimcommented, Apr 14, 2019

@justjoeyuk As for working on the engine while building a game I’ve done a couple approaches

  • Git submodule in your game project with a fork of excalibur included, then reference the build files in your game.
  • NPM file reference in your game’s package.json to a local build of excalibur on your machine

I’ll make an issue later to update our readme to include this in our documentation 👍

0reactions
github-actions[bot]commented, Dec 27, 2020

This issue hasn’t had any recent activity lately and is being marked as stale automatically.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Element swipe tracking in GTM and GA4 -
In this post, I will share one way how we can track slider swipe events in GTM and GA4. Since those event types...
Read more >
Google Tag Manager: tracking mobile swipes
Learn how to track mobile swipe gestures via GTM. Enrich your data set and obtain valuable insigths about your users' behavior to improve ......
Read more >
Swipe Pages: Landing Page Builder
Quickly create insanely fast, mobile optimized AMP landing pages without any code using Swipe Pages, the landing page builder for ROI driven marketers....
Read more >
Swipe Cards and Attendance Tracking: What You Need to Know
Swipe cards are one type of employee attendance tracking technology that can help businesses effectively monitor and follow the attendance ...
Read more >
Build High Converting Landing Pages with Swipe Pages
If you're in the market for an easy tool to build eye-catching and high converting lading pages - Check out Swipe Pages in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found