Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swipe input not working? #347

Open
GrimLothar opened this issue Feb 23, 2023 · 7 comments
Open

Swipe input not working? #347

GrimLothar opened this issue Feb 23, 2023 · 7 comments

Comments

@GrimLothar
Copy link

I followed the documentation to enable all the inputs on my game, but although arrows work fine, swiping on a phone doesn't seem to be doing anything...

I have this

        initInput()

            onInput(['arrowleft', 'swipeleft', 'dpadleft'], () => {
                player.playAnimation('walk_left');
                player.resetCollisionBox()
                player.collisionBox.x -= 64
                // don't move the player if he collides with the collision layer
                if (tileEngine.layerCollidesWith('blocks', player.collisionBox)) {
                    return;
                }

                player.x -= player.width;
            });

I also tried adding this:

      initPointer();
        initGesture();

but swiping is still not working

@straker
Copy link
Owner

straker commented Feb 23, 2023

I can take a look at it later today and see if I can spot anything.

@straker
Copy link
Owner

straker commented Feb 24, 2023

So I tested out a basic example and swipe gestures seem to be working for me. I have an android phone so I'm unable to test iOS though.

import kontra from '/src/kontra.defaults.js';

(async () => {
  kontra.init();
  kontra.initInput();

  const sprite = kontra.Sprite({
    x: 100,
    y: 100,
    color: 'red',
    width: 40,
    height: 40
  });

  kontra.onInput(['arrowleft', 'swipeleft', 'dpadleft'], () => {
    sprite.x -= sprite.width;
  });

  kontra.onInput(['arrowright', 'swiperight', 'dpadright'], () => {
    sprite.x += sprite.width;
  });

  kontra.GameLoop({
    render() {
      sprite.render();
    }
  }).start();
})();

@GrimLothar
Copy link
Author

Aha I have an iPhone myself... ok, Will try your example tomorrow and get back to you. If it doesn't work it's def an iOS issue... happy to look into it if you tell me where to look in the source code!

@GrimLothar
Copy link
Author

yeah... that example you shared is not working on iOS :(

@straker
Copy link
Owner

straker commented Feb 24, 2023

Well dang. I don't have any good way to look into that.

@sylfel
Copy link
Contributor

sylfel commented Oct 18, 2023

Hi straker
I try to understand why it's not working on iOs phone, and i may found something :

It's also not working in Firefox adaptive view ! (easier to test)

in Gesture.js there is a test :

kontra/src/gesture.js

Lines 135 to 142 in ae63cba

// ensure that the indices of touches goes from 0..N.
// otherwise a length 1 touch could have an index of 2
// which means there were two other touches that started
// a gesture
// @see https://stackoverflow.com/a/33352604/2124254
[...Array(touches.length).keys()].every(
key => touches[key]
) &&

This "key" corresponding to identifier in Touch object
In webkit, it's 0 for first touch, 1 for second, and in next movement, it restart to 0 for first touch, 0 and 1 if there's two touches...
but in firefox (and i suppose in safari) it's an incremental
So, first movement, it's 0 for first touch, 1 for second touch
in second movement, it's 2 for first touch ...

So test doesn't work.

Question : How to correct this ? if you have an idea, i can try a PR.
I may drop this test, but if it's here, it may be usefull, isn't it ?

@straker
Copy link
Owner

straker commented Oct 21, 2023

@sylfel interesting find. I'll have to think about how to handle touches that way. Probably just need to make sure that a started touch also has an end so we don't skip one on accident.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants