I have a FPS camera (camera1), which collides with weapons (AnimatedSceneNodes) located in the map.
Weapons are stored as powerups in a list.
When I hit the first weapon everythings works fine, it is attached to camera1.
But when i hit the next one, the old weapon is not detached properly, and some
pointer error occours, which ends up with access violation.
Has anyone an idea whats wrong?
Code: Select all
// This Method works ok, it calls the messy one
void MeshDemo::checkCollisions(){
u32 i=0;
bool hit=false;
while (i<powerupList.size()&& !hit){
if (powerupList[i]->isVisible()&&
powerupList[i]->getPosition().getDistanceFrom(camera1->getPosition())<35){
hit=true;
powerupList[i]->setVisible(false);
waitingList->addNewWaitItem(5000, powerupList[i]->getTimerID());
switch (powerupList[i]->getType()){
case UNKNOWN:std::cout << "??"<<i<<std::endl;
break;
case WEAPON1: std::cout << "w1 "<<i<<std::endl;
case WEAPON2: std::cout << "w2 "<<i<<std::endl;
attachWeapon(powerupList[i]->getMesh(),powerupList[i]->getTexture());
break;
case HEALTH: std::cout << "h"<<i<<std::endl;
break;
}
}
i++;
}
}
// some problem in here
// method should attach the collected weapon to camera1
void MeshDemo::attachWeapon(IAnimatedMesh* wMesh, ITexture* t){
core::vector3df posWeaponCam(10,-15,15);
core::list<ISceneNode*> oldWeapons=camera1->getChildren();
std::cout << "count old weapons:"<<oldWeapons.getSize()<<" active="<<activeWeapon<<std::endl;
// Somethings wrong here?
if (activeWeapon){
camera1->removeChild(activeWeapon);
// activeWeapon->remove();
}
activeWeapon=smgr->addAnimatedMeshSceneNode(wMesh, camera1, 0, posWeaponCam);
activeWeapon->setMaterialTexture(0,t);
camera1->addChild(activeWeapon);
std::cout << "exit attach weapon="<<activeWeapon<<std::endl;
}
/*
OUTPUT:
w2 2
count old weapons::0 active=0
exit attach weapon=0xe0b5bc0
w1 1
w2 1
count old weapons:1 active=0xe0b5bc0
exit attach weapon=0xe0b5bc0
<crash>
*/
Thanks in advance.