javascript从对象

时间:2018-04-15 20:34:21

标签: javascript function-call

我不知道为什么会收到此错误。 有人知道吗?

function Moving(gameboard){

    this.canvas = document.getElementById("field");
    this.board = gameboard;

    console.log("moving on board added");

    this.touchStartX = 0;
    this.touchStartY = 0;

    this.drag = function(x, y){
        var distX = x - this.touchStartX;
        var distY = y - this.touchStartY;

        this.board.xStart += distX;
        this.board.yStart += distY;
    }


    this.handleTouchStart = function(event){
        console.log(this.drag);
        event.preventDefault();
        if(event.targetTouches.length == 1) {
            this.touchStartX = parseInt(event.targetTouches[0].clientX);
            this.touchStartY = parseInt(event.targetTouches[0].clientY);
        }
    }


    this.handleTouchMove = function(event){
        event.preventDefault();
        if(event.targetTouches.length == 1){
            this.drag(parseInt(event.targetTouches[0].clientX), parseInt(event.targetTouches[0].clientY));
        }
    }


    this.canvas.addEventListener("touchstart", this.handleTouchStart, false);
    this.canvas.addEventListener("touchmove", this.handleTouchMove, false);

}
句柄中的

移动我调用拖动函数,但它打印我拖动没有函数,如下所示:

未捕获的TypeError:this.drag不是函数     在HTMLCanvasElement.Moving.handleTouchMove(moving.js:34)

我很高兴每个答案:) LG

1 个答案:

答案 0 :(得分:0)

变化:

   this.canvas.addEventListener("touchstart", this.handleTouchStart, false);
  this.canvas.addEventListener("touchmove", this.handleTouchMove, false);

  this.canvas.addEventListener("touchstart", e => this.handleTouchStart(e), false);
  this.canvas.addEventListener("touchmove", e => this.handleTouchMove(e), false);