如何使动态身体静态在Cocos2d v3.0与Chipmunk [英] How to make a dynamic body static in Cocos2d v3.0 with Chipmunk

查看:298
本文介绍了如何使动态身体静态在Cocos2d v3.0与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;
}

我收到错误:

由于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.

任何帮助将不胜感激。

推荐答案

需要实现一个后步骤回调。
要在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];
}

注意,我假设你可以访问你的CCPhysicsNode _physicsNode
在计算物理步骤时, CCPhysicsNode 的Chipmunk空间被锁定。在计算步骤期间,碰撞被解决,对象被移动 - 在此计算期间更改主体类型可能会导致意外的行为。

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.

因此,您添加 postStepBlock 回调。这是一个可以安全地更改主体类型的地方。

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).

如果还添加了一个示例实现: a href =https://www.makegameswith.us/gamernews/367/make-a-dynamic-body-static-in-cocos2d30-with-chi =nofollow> 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆