2013-11-04 79 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。我發現回調仍然是'哈?'的來源。只要我讀了你的方法,我就明白了。儘管添加你的方法會導致不尋常的行爲,但整個事情需要重新編寫,而且必須等待考試結束。我用我的解決方案使用布爾作爲信號提交。 –