JavaScript - extremely confused on removing elements from Container

后端 未结 4 1304
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 05:47

I\'m making a 2D, top-down Zelda style web rpg single player in JavaScript.

When the player (purple shirt) walks near a cat, it will \"rescue\" it... which basically rem

4条回答
  •  耶瑟儿~
    2021-01-25 06:20

    Beware that EaselJS elements aren't DOM elements.

    Supposing you want to remove an element by its id, I'd suggest this :

    function removeElementById(container, id) {
       for (var i=container.getNumChildren(); i-->0;) {
           if (container.getChildAt(i).id===id) {
                container.removeChildAt(i);
                return true;
           }
       }
       return false;
    }
    

    But using the id might not be the best architectural solution. In my EaselJS program I keep a reference to the objects themselves so that I don't have to search them.

提交回复
热议问题