简体   繁体   English

移相器拖放多个文本项

[英]phaser drag and drop on multiple text items

In a game I want to add the numbers 1-9 that are draggable, and on the drag and drop events I want to call some functions. 在游戏中,我想添加可拖动的数字1-9,在拖放事件中,我想调用一些函数。 But in a loop events are not working. 但在循环中,事件不起作用。 Any solution will be nice. 任何解决方案都会很好。

Here is the code: 这是代码:

var count = 0;
points.forEach(function(item){
    var one = game.add.text(item.centerX, item.centerY, count, this.style);
    one.anchor.setTo(0.5)
    one.inputEnabled = true;
    one.input.enableDrag();
    one.input.startDrag(game.input.activePointer);
    one.events.onInputDown.add(this.clone, this, 0, one);
    one.events.onDragStop.add(this.fixLocation);
    count++;
});

This gives me the error: 这给了我错误:

Phaser.Signal: listener is a required param of add() and should be a Function. Phaser.Signal:侦听器是add()的必需参数,应为Function。

this is the fixlocation function 这是fixlocation功能

fixLocation: function(item){

         if(rectangle.contains(item.x, item.y)){
            itemAdded += 1;
        } else{
            item.destroy()
        }
    },

My fault. 我的错。 Acutally the this inside foreach in in wrong context. 在错误的上下文中,将this里面的foreach主动提出。 It is the inside of the loop. 它是循环的内部。 All i have to do was to call function outside of the loop. 我要做的就是在循环之外调用函数。 that is: 那是:

var count = 0;
            var me = this;// get this here
            points.forEach(function(item){
                var one = game.add.text(item.centerX, item.centerY, count, this.style);
                one.anchor.setTo(0.5)
                one.inputEnabled = true;
                one.input.enableDrag();
                one.input.startDrag(game.input.activePointer);
                one.events.onInputDown.add(me.clone, this, 0, one); // now call the function
                one.events.onDragStop.add(me.fixLocation);
                count++;
            })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM