How to stop an update method when something happens?

so far I have used in my game scheduleUpdate(), schedule() and scheduleOnce(). But I think they way I am using some of those methods is not optimal. For intance, in the game scene update() method I have somethinglike this:

void GameScene::update(float dt)
    // is this overkill ??
	if (someSprite)
    //...more code

This works fines in my game, but checking every frame for something that will not exist anymore at somepoint like someSprite seems an overkill. Basically I want to unschedule the update of someClass if someSprite doesn’t exist anymore.
I looked into the overload of the schedule methods, but I am not sure if any does what I am asking.

There is this one

void schedule(const std::function<void(float)>& callback, const std::string &key);

it says: @param key The key of the lambda function. To be used if you want to unschedule it.
But I not quite sure how it works.

In other words, I have an item that the player can pick. The item has an update method that checks each frame for an intersection with the player. When it intersects, the item disappears. Hence the if (someSprite) do something. But this force my code to keep checking if someSprite exists.
Any more optimal suggestions or is this fine?


Thats why you should use custom update method, not normal update.

// Define any method with `float dt` param
void GameScene::updateSprite(float dt) {
// logic

// To register

// To unregister

I do use custom update though. Instead of SEL_SCHEDULE I use CC_SCHEDULE_SELECTOR. According to the definition it does a cast as SEL_SCHEDULE.


Is there any preference making chose one over the other?