Saturday, July 2, 2022
HomeGame DevelopmentTips on how to take away a participant when lifeless with out...

Tips on how to take away a participant when lifeless with out null pointer crash – cocos2d-x


Hello,
I’ve a technique that verify collisions between the participant sprite and the enemies.

void CollisionSystem::runPlayerCollision(Vector<Sprite*>& enemies, Sprite* playerSprite)
{
    // I've a loop that checks the collisions and if a collisio is discovered it does this
    CCLOG("Collision discovered");
    playerSprite->removeFromParentAndCleanup(true);
    playerSprite->launch();
}

My understanding is that removeFromParentAndCleanup doesn’t delete the pointer. So I add a launch() to delete it.
Now that is what I don’t get. In the primary replace() loop of the sport I added this

if (GameGlobals::playerSprite != nullptr)
{
     CCLOG("recreation working");
     collisionSystemInstance->runPlayerCollision(GameGlobals::enemies,  Recreation:Globals::participant)
}

The output is

recreation working
recreation working
recreation working
recreation working
recreation working
"Collision discovered"

Exception thrown: learn entry violation.
playerSprite->**** was 0xDDDDDDDD.

So, the collision technique is doing the work proper as a result of the video games retains working whereas the playerSprite exists. And when there’s a collision, the LOG “Collision discovered” proves it’s working okay, and therefor it deletes the playerSprite pointer. The error playerSprite->**** was 0xDDDDDDDD occurs inside
collisionSystemInstance->runPlayerCollision(GameGlobals::enemies, Recreation:Globals::participant), however why is it working that, if I said to working solely if the GameGlobals::playerSprite != nullptr??

The error signifies that playerSprite was delete proper? which is what I would like when it collides. However the situation to run the collision technique is that the playerSprite exists, and it doesn’t, so why is it working it? I’m confused.

R


That’s not right.

It’s crashing since you’re making an attempt to free an object that has already been freed.

A name to launch() must match a name to retain(). Assuming you’re simply utilizing the Sprite::createXXXX() strategies, the sprite is about to auto-release, that means it’s added to the auto-release pool which is checked each loop. When the sprite is faraway from a node (by way of playerSprite->removeFromParentAndCleanup(true); or just playerSprite->removeFromParent();, then its reference depend reduces by 1. If the counter is 1, then given it’s within the auto-release pool, it will likely be freed.

The one time try to be calling launch() on it explicitly is when you known as retain() on it, or created it with a new and by no means set it to be auto-released by way of a name to autorelease().

You, fairly actually, have the supply code proper there in entrance of you. Utilizing the debugger may have led you to the supply of this subject in a short time. Maybe it’s finest you evaluate the code associated to the retain/launch/autorelease performance to get a greater understanding of the way it all works.

I ended up creating a world variable known as gameIsStopped.So the replace() loop solely begins whether it is fasle.
When a collision occurs the variable is about to true, so replace() gained’t preserve going. Unsure if that is one of the best ways however I’ve seen comparable workflows to pause the sport, restart, and so on…
Mainly utilizing world bool variables which can be set relying on a particular situation. And people variables arre utilized in the primary replace() to carry out diferente operations like recreation over, lowering lives, transferring to subsequent scene, and so on… Appears alright to me.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments