2013-11-04 61 views
0

这是作业,它有一个恼人的错误,经过数小时的努力后我无法解决。如何在layer.destroyChilden()后实现回调

http://canvaseu.chrisloughnane.net/

当欧盟地图点击一个国家道路上触发绑定的事件。

这个事件破坏了图层的孩子和加载国家。但是,如果您点击并快速移动鼠标,并放开该国家加载的按钮,但欧盟的乡村路径依然存在。在屏幕截图中显示的西班牙效果最好。

我希望mapLayer.destroyChildren();之后的回调,然后调用加载函数将解决我的问题。

这可能有点难以复制。

我确定我的控制与我的观点太紧密相关,但我一直无法看到一个将它们整齐分开的解决方案。

enter image description here

**** ****编辑

我想出了工作部分的解决方案,但我认为这是很可怕的黑客代码,我加入到鼠标按下结合down = true;并添加检查到mouseout绑定,请参阅下面。 我认为正在发生的事情是当你移动鼠标,并让按钮非常快,mouseover绑定结束骑马mouseup。

此解决方案并不理想,在加载了多个国家和区域后,画布响应速度变慢。


Event Binding

path.on('mousedown touchstart', function() 
{ 
      down = true; 
    this.setStroke('red'); 
    this.moveTo(topLayer); 
    /**** 
    * Handle if the path we are displaying in canvas is the eu 
    * to allow selection and load of country path point data. 
    */ 
    if (associativeCountryArray[lastCountry].getText() == 'eu') 
    { 
     associativeCountryArray[lastCountry].setFill('#bbb'); 
     associativeCountryArray[lastCountry].setFontStyle('normal'); 
     countrynames[lastCountry].selected = false; 
     this.moveTo(mapLayer); 
     mapLayer.destroyChildren(); 
     lastCountry = this.getName(); 
     countrynames[this.getName()].selected = true; 
     associativeCountryArray[this.getName()].setFill("rgb(" + r + "," + g + "," + b + ")"); 
     associativeCountryArray[this.getName()].setFontStyle('bold'); 
     loadPaths(this.getName().replace(/\s/g, '')); 
     countryNameLayer.draw(); 
    } 
    else 
    { 
     window.open('https://www.google.com/search?q=' + this.getName(),'_blank'); 
    } 
    topLayer.drawScene(); 
}); 



path.on('mouseout', function() 
{ 
    if(!down) 
    { 
     document.body.style.cursor = 'default'; 
     this.setFill('#eee'); 
     this.setStrokeWidth(settings.strokewidthstart); 
     /**** 
     * On hover, only change colour of country names around edge of canvas if we are on the 'eu' map 
     */ 
     if (lastCountry == 'eu') 
     { 
      associativeCountryArray[this.getName()].setFill('#bbb'); 
      associativeCountryArray[this.getName()].setFontStyle('normal'); 
     } 
     this.setStroke('#555'); 
     this.moveTo(mapLayer); 
     writeMessage(''); 
     topLayer.draw(); 
     countryNameLayer.draw(); 
    } 
    else 
    { 
     down = false; 
    } 
}); 
path.on('mouseup touchend', function() 
{ 
    this.setStroke('black'); 
    topLayer.drawScene(); 
    down = false; 
}); 

回答

1

像这样:

发送要清除容器(组层),你想触发回调。

function myDestroyChildren(container,callback) { 
    var children = container.getChildren(); 
    while(children.length > 0) { 
     children[0].destroy(); 
    } 
    callback(); 
} 
+0

cheers markE。我发现回调仍然是'哈?'的来源。只要我读了你的方法,我就明白了。尽管添加你的方法会导致不寻常的行为,但整个事情需要重新编写,而且必须等待考试结束。我用我的解决方案使用布尔作为信号提交。 –