如何在 Cocos2d v3.0 中使用 Chipmunk 使动态体静态化 [英] How to make a dynamic body static in Cocos2d v3.0 with Chipmunk
问题描述
我正在使用 Cocos2d v3 并且想在与另一个物体碰撞后将一个物体从动态变为静态.目前我有:
I’m using Cocos2d v3 and want to change a body from dynamic to static after colliding with another body. At the moment I’ve got:
-(void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair *)pair static:(CCNode *)nodeA wildcard:(CCNode *)nodeB
{
_player.physicsBody.type = CCPhysicsBodyTypeStatic;
}
或
-(BOOL)ccPhysicsCollisionPreSolve:(CCPhysicsCollisionPair *)pair static:(CCNode *)nodeA wildcard:(CCNode *)nodeB
{
_player.physicsBody.type = CCPhysicsBodyTypeStatic;
return YES;
}
但两者都不起作用.我得到了错误:
but neither works. I get the error:
由于 Chipmunk 错误而中止:在调用 cpSpaceStep() 或查询期间无法安全地执行此操作.将这些调用放入步骤后回调中.失败条件:!space->locked
Aborting due to Chipmunk error: This operation cannot be done safely during a call to cpSpaceStep() or during a query. Put these calls into a post-step callback. Failed condition: !space->locked
然后我尝试在碰撞点建立一个关节,但它不能正常工作.
I then tried to make a joint at the point of collision but it doesn’t work right.
有没有办法在 v3 中碰撞后立即将主体更改为动态?我可以在以后的版本中使用 Box2D 做到这一点.我想阻止重力和其他力,所以它不会移动.我想让它看起来像粘在表面上.
Is there a way to change a body to dynamic as soon as it collides in v3? I can do it in later versions using Box2D. I want to stop gravity and other forces so it doesn’t move. I want to make it look like it stuck to a surface.
阅读一些关于步骤后回调的信息,但我不熟悉如何使用它们.
Read a little on post-step callbacks but i'm unfamiliar with how to use them.
任何帮助将不胜感激.
推荐答案
如错误信息所述,您需要实现一个 post-step 回调.要在 Cocos2d 3.0 和 Objective Chipmunk 上执行此操作,您首先需要导入一个新的头文件以访问高级 Chipmunk 属性:
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];
}
请注意,我假设您可以访问 _physicsNode
中的 CCPhysicsNode.CCPhysicsNode
的 Chipmunk 空间在计算物理步时被锁定.在计算步数期间,解决了碰撞并移动了对象 - 在此计算期间更改身体类型可能会导致意外行为.
Note that I assume you have access to your CCPhysicsNode in _physicsNode
.
The Chipmunk Space of CCPhysicsNode
is 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.
因此,您添加了 postStepBlock
回调.这是一个可以安全地改变体型的地方.
Therefore you add the postStepBlock
callback. This is a place where a body type can be safely changed.
您传递给回调的 key
值用于确保代码只被调用一次(在删除对象时特别有用,但在这种情况下也很有意义).
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).
如果还添加了示例实现:https://www.makegameswith.us/gamernews/367/make-a-dynamic-body-static-in-cocos2d-30-with-chi
If also added an example implementation: https://www.makegameswith.us/gamernews/367/make-a-dynamic-body-static-in-cocos2d-30-with-chi
这篇关于如何在 Cocos2d v3.0 中使用 Chipmunk 使动态体静态化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!