Jump to content

Dragging and copying a sprite in one click


Mikkiz
 Share

Recommended Posts

I'm trying to make a menu like option to where the user can drag a sprite on screen and make a clone out of it leaving the other sprite on it's original location. This is what I have up untill now.

var box = game.add.sprite(100, 200, 'box', 0);
    box.anchor.set(0);
    box.inputEnabled = true;
    box.events.onInputDown.add(clone, this, 0, box);

function clone(obj){
    var clone = game.add.sprite(obj.x, obj.y, obj.key, obj.frame);
    clone.inputEnabled = true;
    clone.input.enableDrag(true);
}

 

Only down side is I have to click twice on the sprite in order for there to be a copy of it which kind of takes away the whole purpose of a copy on drag function. Any help would be greatly appreciated. (I'm quite new to Phaser and js so  I'm sorry for any obvious mistakes beforehand) 

 

Thanks in advance!

Link to comment
Share on other sites

You could use startDrag(pointer) to make it work, but the the spawn point must be set to zero:

  var clone = game.add.sprite(0, 0, obj.key, obj.frame);
  clone.inputEnabled = true;
  clone.input.enableDrag(true);
  clone.input.startDrag(game.input.activePointer);

An alternative is to trigger the event of hovering over the cloned object and then startDrag:

  var clone = game.add.sprite(obj.x, obj.y, obj.key, obj.frame);
  clone.inputEnabled = true;
  clone.input.enableDrag(true);
  clone.events.onInputOver.add(function(clone, pointer){
    clone.input.startDrag(pointer);
  },this);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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