How to make a dynamic body static in Cocos2d v3.0 with Chipmunk

对着背影说爱祢 提交于 2019-12-04 22:02:54

As the error message states, you need to implement a post-step callback. To do this on Cocos2d 3.0 and with Objective Chipmunk you first need to import a new header file to access advanced chipmunk properties:

#import "CCPhysics+ObjectiveChipmunk.h"

Then add the callback in your collision handler:

-(void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair *)pair static:(CCNode *)nodeA wildcard:(CCNode *)nodeB
{
    [[_physicsNode space] addPostStepBlock:^{
        _player.physicsBody.type = CCPhysicsBodyTypeStatic;
    } key:_player];
}

Note that I assume you have access to your CCPhysicsNode in _physicsNode. The Chipmunk Space of CCPhysicsNodeis locked while the a physics step is calculated. During a calculation of a step collisions are resolved and objects are moved around - changing the body type during this calculation could result in unexpected behaviour.

Therefore you add the postStepBlockcallback. This is a place where a body type can be safely changed.

The key value you pass into the callback is used to ensure that the code is only called once (especially useful when removing objects, but it also makes sense in this case).

If also added an example implementation: https://www.makegameswith.us/gamernews/367/make-a-dynamic-body-static-in-cocos2d-30-with-chi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!