麻烦循环并删除createjs“on”eventListener“off”

时间:2015-04-28 13:47:47

标签: easeljs

我试图在createjs中循环并删除eventlistener:

    Actor = function() {

    this.update = true;
    this.offset;
    this.instance;


    this.member = function(a_obj) {
count++;

        this._container = a_obj.container;
        this._images = a_obj.images;
        this._name = a_obj.name;
        this._top = a_obj.top;
        this._left = a_obj.left;

        getCirTest = new createjs.Bitmap(loader.getResult(this._images));
        getCirTest.name = this._name;
        getCirTest.y = this._top + 10;
        getCirTest.x = this._left + 10;
        getCirTest.scaleX = 0.8;
        getCirTest.scaleY = 0.8;
        getCirTest.cursor = "pointer";
        this._container.addChild(getCirTest);

         holdsAllObj[count]=getCirTest;

       console.log(holdsAllObj);


      md = getCirTest.on("mousedown", activate);

       function activate(evt) {

            myCurrentTarget = evt.currentTarget;
            xyCor.push(evt.currentTarget.x, evt.currentTarget.y);

            this.parent.addChild(this);
            this.offset = {
                x: this.x - evt.stageX,
                y: this.y - evt.stageY
            };
        }

 pm = getCirTest.on("pressmove", move);

     function move(evt) {

            myintersect(evt.currentTarget, getCenterBitmap);

            this.x = evt.stageX + this.offset.x;
            this.y = evt.stageY + this.offset.y;
            update = true;
        }


 cc = getCirTest.on("click", release);


        function release (evt) {
            countClicks++;


          holdsAllObj[1].off("mousedown",md);
          holdsAllObj[1].off("pressmove",pm);

            console.log();

            if (soundPlay == true && countClicks == 1) {


                console.log("im true");
                mySoundInstance = createjs.Sound.play(evt.currentTarget.name);
                mySoundInstance.addEventListener("complete", createjs.proxy(handleSoundComplete, mySoundInstance));


            } else {
                createjs.Sound.stop(evt.currentTarget.name);
                countClicks = 0;
            }

            console.log(countClicks);

            update = true;
        }




    } //**** END: members ****

}

我的getCirTest,它有一个事件:

 md = getCirTest.on("mousedown", activate);

它创建了一个像这样的位图对象的10个实例:

 for (var circles = 0; circles < 10; circles++) {

          var x = Math.round(container.width / 2 + radius * Math.cos(angle) - 150 / 2);
          var y = Math.round(container.width / 2 + radius * Math.sin(angle) - 150 / 2);



          var MP = new Actor();

          MP.member({
              container: container,
              images: myFTMImages[circles],
              name: myFTMNames[circles],
              top: y,
              left: x
          });

          angle += step;



      }

我的问题是我想删除事件中的eventListeners:

cc = getCirTest.on("click", release);


        function release (evt) {

}

这就是为什么我把位图放在一个数组中:

holdsAllObj[count]=getCirTest;

然后尝试了以下内容:

cc = getCirTest.on("click", release);


                function release (evt) {

    for(var ijk=0;ijk<holdsAllObj.length;ijk++){
          holdsAllObj[ijk].off("mousedown",md);
              holdsAllObj[ijk].off("pressmove",pm);
    }


    }

不幸的是......它对我不起作用......

1 个答案:

答案 0 :(得分:0)

这看起来像是js范围的问题。 JS方法在窗口上下文中调用,而不是在创建它们的上下文中调用。您可以使用createjs.proxy方法将范围应用于方法调用,如下所示:

cc = getCirTest.on("click", release, this);

希望有所帮助。