Jump to content

drag sprite following path


sk-iv
 Share

Recommended Posts

Although there is no built-in way to follow a path that I'm aware of, you can use drag events to snap your sprite to a path as you drag it. (Drag events example: http://phaser.io/examples/v2/input/drag-update)

Just calculate where along the path the sprite should be given the cursor position and update the sprite's position accordingly using the onDragUpdate event.

Link to comment
Share on other sites

//  This is the path
          var graphics = game.add.graphics(0, 0);


          graphics.lineStyle(10, 0xffd900, 1);
          graphics.moveTo(495,207);
          graphics.bezierCurveTo(470,178,434,160,393,160);
          graphics.bezierCurveTo(319,160,259,220,259,294);
          graphics.bezierCurveTo(259,368,319,428,393,428);
          graphics.bezierCurveTo(434,428,470,410,495,382);

//  This is the handler, which should move along

handler = game.add.sprite(495,207, 'handler');
          handler.anchor.setTo(0.5, 0.5);

//Also enable sprite for drag
      handler.inputEnabled = true;
      handler.input.enableDrag();


      handler.events.onDragUpdate.add(dragUpdate);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...