0
\$\begingroup\$

I spent a day trying to hack my own Action class, but given the hurdles I've hit trying to do so, I'm hoping there's some way to work around my problem while still using the built-in (and admittedly powerful) Action class.

To wit, I need to check collision on a linear movement type Action (like MoveTo). It appears the function simply calculates a vector and increases the Node's position on that vector as time elapses. All fine.

Now, I have a collisionHandler function set up within both my Player class and my custom Move class. However, cocos2d automatically schedules actions when they're added to a Node using the runAction method. Because of this, it seems that the collisionHandler never gets called on the Move class. The code, as I imagine it updates, looks like this:

Main{ //main loop
objects->update();
collisionManager->update();
} // all other cocos2d scheduled updates
actions->update(); // the collision doesn't get checked til next frame

Running the collision loop twice per frame must be the wrong answer. So, how do I get it so that both the objects and their actions update before the collision loop runs?

\$\endgroup\$
1
\$\begingroup\$

I did some more digging through the cocos2d lib files, and figured out how to substitute my own "ActionManager" class for the default one, thereby allowing me to determine when all of the Actions get updated. I haven't had any adverse side effects, as yet. Here's the implementation:

Scene::init{
this->ActionManager = new (std::nothrow) ActionManager(); // Director will handle safe deletion
Director::getInstance()->getScheduler()->unscheduleUpdate(Director::getInstance()->getActionManager());
Director::getInstance()->setActionManager(this->ActionManager);
Director::getInstance()->getScheduler()->unscheduleUpdate(ActionManager);
}

Scene::updateMethod{
// update players, monsters, etc.
this->ActionManager->update(); // update actions
// update collision here.
}

Hope this helps anyone with the same issue.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.