//------------------------------------------------------------------------------------- bool Space::destroy(ENTITY_ID entityID) { if(this->entities().size() == 0) return true; SPACE_ENTITIES entitieslog; Entity* creator = NULL; SPACE_ENTITIES::const_iterator iter = this->entities().begin(); for(; iter != this->entities().end(); iter++) { const Entity* entity = (*iter).get(); if(entity->getID() == this->creatorID()) creator = const_cast<Entity*>(entity); else entitieslog.push_back((*iter)); } iter = entitieslog.begin(); for(; iter != entitieslog.end(); iter++) { Entity* entity = const_cast<Entity*>((*iter).get()); entity->destroyEntity(); } // 最后销毁创建者 if(creator) creator->destroyEntity(); return true; }
//------------------------------------------------------------------------------------- bool Space::destroy(ENTITY_ID entityID) { if(destroyed_) return true; destroyed_ = true; if(this->entities().size() == 0) return true; SPACE_ENTITIES entitieslog; Entity* creator = NULL; SPACE_ENTITIES::const_iterator iter = this->entities().begin(); for(; iter != this->entities().end(); iter++) { const Entity* entity = (*iter).get(); if(entity->id() == this->creatorID()) creator = const_cast<Entity*>(entity); else entitieslog.push_back((*iter)); } iter = entitieslog.begin(); for(; iter != entitieslog.end(); iter++) { (*iter)->destroyEntity(); } // 最后销毁创建者 if(creator) { if(Cellapp::getSingleton().findEntity(creator->id()) != NULL) { creator->destroyEntity(); } else { // 之所以会这样是因为可能spaceEntity在调用destroy销毁的时候onDestroy中调用了destroySpace // 那么就会出现在spaceEntity-destroy过程中导致这里继续调用creator->destroyEntity() // 此时就会出现EntityApp::destroyEntity: not found. // 然后再spaceEntity析构的时候销毁pEntityCoordinateNode_会出错, 这里应该设置为NULL。 creator->pEntityCoordinateNode(NULL); } } pNavHandle_.clear(); entities_.clear(); return true; }