bberak/react-native-game-engine

Best Way to handle touch as directional control

ningacoding opened this issue · 3 comments

Hello, what's the best Way to handle touch user input? in order to build a simple directional (up,right,down,left) control.

The touches only have type 'move', 'long-press', etc.

Is there a Way the touches gives the orientation natively?

Adding event types like 'move-left', etc.

Hi @ningacoding,

The touch event should have enough data in its payload to help you determine direction. Something like:

touches.filter(t => t.type === "move").forEach(t => {
    let finger = entities[t.id];
    if (finger && finger.position) {
      finger.position = [
        finger.position[0] + t.delta.pageX,
        finger.position[1] + t.delta.pageY
      ];
    }
  });

For something with a little more functionality, you can take a look at the Gamepad Controller System from the template project.

Hope that helps!

Hi @bberak , thank you for your answer.

with Gamepad Controller System, How i detect "up", "left", "down", "right" events?

I just need to call dispatch("up") to fire event, but i don't under stand the code inside gamepad-controler.

I already added the component into my App:

msedge_dfyS9gB3Zu

But i don't understand how it works, I move the D-Pad, it just turns from transparent to white and viceversa. I don't see D-Pad move.