cocos2d v3 碰撞检测 [英] cocos2d v3 collision detection

查看:33
本文介绍了cocos2d v3 碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查两个物体的碰撞碰撞,但碰撞检测回调没有被触发.这是我的代码:

I'm trying to inspect collision collision of two bodies, but collision detection callbacks are not being fired. Here is my code:

1) 我的 CCScene 实现了 CCPhysicsCollisionDelegate 协议

1) My CCScene implements CCPhysicsCollisionDelegate protocol

2) 我为物理设置碰撞委托

2) I set collision delegate for physics

_physics = [CCPhysicsNode node];
_physics.gravity = PHYSICS_GRAVITY;
_physics.debugDraw = YES;
_physics.collisionDelegate = self;
[self addChild:_physics];

3) 我为两个身体中的每一个设置一个碰撞类型

3) For each of two body I set a collision type

body1.collisionType       = @"body1";
body2.collisionType       = @"body2";

4) 就是这样,当这两个物体发生碰撞时,不会调用任何 CCPhysicsCollisionDelegate 回调方法.

4) That's it, when these two bodies collide none of CCPhysicsCollisionDelegate callback methods is being called.

- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair typeA:(CCNode *)nodeA     typeB:(CCNode *)nodeB
{
    NSLog(@"HELLO");
    return YES;
}

你能帮我解决这个问题吗?你在 cocos2d v3 中能接收到碰撞回调吗?

Could you please help me with this? Have you been able to receive collision callbacks in cocos2d v3?

提前致谢

推荐答案

在 cocos2d v3 物理中,collisionType 消除了设置整数位掩码来定义碰撞类型的需要.CCPhysicsCollisionDelegate 方法的参数名称必须是您要自己处理的碰撞类型.所以在你的情况下,碰撞回调方法应该是

In cocos2d v3 physics, collisionType eliminates the need to set integer bit masks to define the type of collision. The parameter name CCPhysicsCollisionDelegate methods must be the collisionTypes that you want to deal with yourself. So in your case , the collision callback method should be

- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair body1:(CCNode *)nodeA     body2:(CCNode *)nodeB
{
    NSLog(@"HELLO");
    return YES;
}

cocos2d 中默认一切都会发生碰撞,但是如果你将两个 body 的 collisionGroup 设置为相同,那么它们就不会发生碰撞.

By default everything collides in cocos2d, but if you set the collisionGroup of two bodies to be the same then they wouldn't collide.

这篇关于cocos2d v3 碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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