bberak / react-native-game-engine

A lightweight Game Engine for React Native 🕹⚡🎮
MIT License
2.86k stars 172 forks source link

Best Way to handle touch as directional control #71

Open ningacoding opened 2 years ago

ningacoding commented 2 years ago

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.

bberak commented 2 years ago

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!

ningacoding commented 2 years ago

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.

helloworq commented 1 year ago

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 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.

Hi,Could you please help me how can i add the gamepad-controller into my own project. i have tried it serval times,but it dose not work