0
votes

Okay I have a little game going on Phaser with a slider which the player can move up or down:

image of slider on a track while a stick figure looks on

As you can see, the slider is on a track which one would assume restricts where the slider can go. At the moment, this isn't the case and the slider can run right off the rail.

How can I detect if there's a slider track in the target location before moving the slider?


Here's where I'm creating the static groups for slider and slider track.

    sliders = this.physics.add.staticGroup();
    slider_tracks = this.physics.add.staticGroup();

Here's where the objects themselves are being added to the game:

  add_slider: function (x, y, data) {
    map.add_slider_track(x, y, data);
    var slider = sliders.create(x, y, data.direction + '_slider');
    for (var key in data) {
      slider[key] = data[key];
    }
  },
  add_slider_track: function (x, y, data) {
    slider_tracks.create(x, y, data.direction + '_track');
  },

And here's the functions which move it:

  hitSlider: function (player, slider) {
    if (slider.direction == 'vertical') {
      if (player.body.onFloor() && player.slamming) {
        interaction.moveSliderDown(slider)
      } else if (player.body.onCeiling()) {
        interaction.moveSliderUp(slider);
      }
    }
    player.slamming = false;
  },
  moveSliderUp: function (slider) {
    slider.setY(slider.y - block_size);
    slider.body.position.y = (slider.y - (block_size / 2));
    player.setVelocityY(100);
  },
  moveSliderDown: function (slider) {
    slider.setY(slider.y + block_size);
    slider.body.position.y = (slider.y - (block_size / 2));
  }

I've tried using slider_track.getFirst (https://rexrainbow.github.io/phaser3-rex-notes/docs/site/group/) but it seems to change the location of a given piece of track, not just detect if there's one there.

1
Can you please explain a bit more addSlieder and addSliderTrack ? - nazimboudeffa
@nazimboudeffa they’re pretty standard parts of the Phaser JS library. Adding a sprite to a group. They pass on the x and y coordinates and build the name of the sprite, which will be “horizontal_slider” or “vertical_slider”. - AJFaraday
@nazimboudeffa they are adding to static groups, yes. Otherwise they fall away from their position. - AJFaraday
oups sorry I've suppressed my last comment, I can figure out what happens but I don't get for example var slider = sliders.create(x, y, data.direction + '_slider'); I guess it's a class that extends the Phaser Sprite class - nazimboudeffa
nvm I start to get what you've done ... you add a group called sliders than you create a slider depending on the direction given in data.direction - nazimboudeffa

1 Answers

1
votes

Just to don't let this question without answer as we normally start a chat, effectively I see in the js/slider_actions.js the solution but I can just say you can use velocity but seriously my level of coding even if I am for a long time in the Phaser community is lower than yours ;)

sliderTrackRight: function (slider) {
  track = slider_tracks.children.entries.find(
    function (track) {
      return (
        track.body.y == slider.body.y &&
        track.body.x == (slider.body.x + block_size) &&
        track.direction == 'horizontal'
      )
    }
  );
  return (typeof track != 'undefined');
},