Hi,
I have a method that check collisions between the player sprite and the enemies.
void CollisionSystem::runPlayerCollision(Vector<Sprite*>& enemies, Sprite* playerSprite)
{
// I have a loop that checks the collisions and if a collisio is found it does this
CCLOG("Collision found");
playerSprite->removeFromParentAndCleanup(true);
playerSprite->release();
}
My understanding is that removeFromParentAndCleanup doesn’t delete the pointer. So I add a release() to delete it.
Now this is what I don’t get. In the main update() loop of the game I added this
if (GameGlobals::playerSprite != nullptr)
{
CCLOG("game running");
collisionSystemInstance->runPlayerCollision(GameGlobals::enemies, Game:Globals::player)
}
The output is
game running
game running
game running
game running
game running
"Collision found"
Exception thrown: read access violation.
playerSprite->**** was 0xDDDDDDDD.
So, the collision method is doing the work right because the games keeps running while the playerSprite exists. And when there is a collision, the LOG “Collision found” proves it is working ok, and therefor it deletes the playerSprite pointer. The error playerSprite->**** was 0xDDDDDDDD happens inside
collisionSystemInstance->runPlayerCollision(GameGlobals::enemies, Game:Globals::player), but why is it running that, if I stated to running only if the GameGlobals::playerSprite != nullptr??
The error means that playerSprite was delete right? which is what I want when it collides. But the condition to run the collision method is that the playerSprite exists, and it doesn’t, so why is it running it? I am confused.
R