2015-04-28 52 views
0

我试着去循环和删除事件侦听:麻烦循环和createjs“的”事件监听删除createjs与“关”

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; 



     } 

我的问题是,我想删除事件在事件监听器:

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); 
    } 


    } 

不幸.. it'does不为我工作...

回答

0

这看起来像一个JS范围的问题。 JS方法在窗口上下文中调用,而不是在创建的上下文中调用。您可以使用createjs.proxy方法的范围适用于一个方法调用,就像这样:

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

希望有所帮助。

+0

我修复了代码片段。 'on()'方法有一个scope参数,通过代理/绑定调用推荐使用。请注意,要删除监听器,您需要存储'on()'调用的结果,并使用该代替回调函数。 – Lanny

+0

不知道我是否可以关注你:-) – user3414539

+0

你说过:“你需要存储on()调用的结果,并用它来代替回调函数”。你能举一个例子吗?兰尼。 – user3414539