1
\$\begingroup\$

Please consider this scene. Developed using PhaserJS 3.

slider above player

I want to make the slider move left when the player touches the right hand side of it, and move right when the player touches the left hand side of it.

This is the code I'm currently using in the collision callback to decide what to do:

  hitSlider: function (player, slider) {
    // ...
      if (player.body.touching.left) {
        slider_actions.moveSliderLeft(slider)
      } else if (player.body.touching.right) {
        slider_actions.moveSliderRight(slider);
      }
  // ...
  }

This works in most cases, EXCEPT that it doesn't check if the player is touching the slider on the left, just that they're touching something on the left.

My screenshot contains an example. If the player stands on top of the slider, and walks left, they are both colliding with the sider and touching something on the left. So the slider moves left.

How can I detect the direction of the collision event itself instead?

\$\endgroup\$
1
\$\begingroup\$

Since the slider can only collide with the player and the player can collide with many other objects, it would be best to do your checks on the slider(s) instead of the player.

This means that your code will look something like so:

  hitSlider: function (player, slider) {
    // ...
      if (slider.body.touching.left) {
        slider_actions.moveSliderLeft(slider)
      } else if (slider.body.touching.right) {
        slider_actions.moveSliderRight(slider);
      }
  // ...
  }
\$\endgroup\$
1
  • \$\begingroup\$ I don't think you'll need the player param anymore, though \$\endgroup\$ – Kitanga Nday Jun 17 '19 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.