Jump to content

Drag a sprite to move another sprite


AndersCan
 Share

Recommended Posts

Hi,

I'm new to Phaser and have had no luck with looking at the documentation to solve my problem.

 

I'm creating a mobile game that will use touch(dragging) to move the player sprite. I want the user to able to actually see the player when moving it, so I want to add a sprite for the sole purpose of controlling the player sprite.

 

The player sprite can only move left or right. This is the code I have created so far, but it doesn't do what I want it to do

    var controller = game.add.sprite(0, 0, cbox);    game.physics.enable(controller, Phaser.Physics.ARCADE);    controller.inputEnabled = true;    controller.input.enableDrag(true);    controller.input.allowHorizontalDrag = true;    controller.input.allowVerticalDrag = false;    controller.body.immovable = true;    controller.body.moves = false;    controller.events.onDragStart.add(doSomething, this);

The first problem is that the Controller sprite is moving when I drag it. The Second problem is that the location of the Player sprite is only moved to the location where I started dragging. It only moves to the click events.

 

Any ideas?

Link to comment
Share on other sites

Figured it out! Need to override the updateDrag function.

controller.input.updateDrag = function(pointer) {    bouncer.x = pointer.x;    // call the generic implementation:    Phaser.InputHandler.prototype.updateDrag.call(this,pointer);}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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