首先尝试使用Chipmunk。
没有碰撞检测注册就是问题。
我的代码:
@implementation MainPlayScene
{
CCPhysicsNode *_physics;
CCNode *MyPhysicsBody;
CCNode *bottomBody;
}
+ (instancetype)scene
{
return [[self alloc] init];
}
- (instancetype)init
{
// Apple recommend assigning self with supers return value, and handling self not created
self = [super init];
if (!self) return(nil);
_physics = [CCPhysicsNode node];
_physics.debugDraw = YES;
[self addChild:_physics z:1];
/// BOTTOM
CGRect bottomRect = CGRectMake(0, 0, [CCDirector sharedDirector].viewSize.width, 10);
bottomBody = [CCNode node];
bottomBody.physicsBody = [CCPhysicsBody bodyWithPolylineFromRect:bottomRect cornerRadius:0];
bottomBody.physicsBody.collisionCategories = @[@"Bottom"];
bottomBody.physicsBody.type = CCPhysicsBodyTypeStatic;
[_physics addChild:bottomBody];
/// MyBody to bounce around
MyPhysicsBody = [CCSprite spriteWithImageNamed:@"MyBody-64x64-24.png"];
MyPhysicsBody.position = ccp((self.contentSize.width/2),(self.contentSize.height/2));
MyPhysicsBody = [CCNode node];
MyPhysicsBody.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, MyPhysicsBody.contentSize.height,MyPhysicsBody.contentSize.width} cornerRadius:0];
MyPhysicsBody.physicsBody.collisionCategories = @[@"MyBody"];
[_physics addChild:MyPhysicsBody z:150];
self.userInteractionEnabled = YES;
return self;
}
检测触摸事件并对物理体施加力以使其在底部上下反弹
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CCLOG(@"Touch Detected");
[MyPhysicsBody.physicsBody applyImpulse:ccp(0, 300.f)];
}
现在我尝试在“Bottom”上检测到碰撞,但即使我看到2个对象的调试行触摸,也没有注册任何内容。
/// try onCollisionEnter first ... nothing
-(void)onCollisionEnter:(CCNode *)entity collisionPair:(CCPhysicsCollisionPair *)pair
{
if ([entity.physicsBody.collisionCategories isEqual: @"Bottom"]) {
CCLOG(@"Hit bottomBody");
}
}
/// try ccPhysicsCollisionBegin pair ... nothing
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair MyBody:(CCNode *) MyBody Botton:(CCNode *)Bottom
{
CCLOG(@"Hit bottomBody");
return TRUE;
}
显然我在这里缺少一些关键的东西......
非常感谢任何帮助!
由于
答案 0 :(得分:4)
我发现很难看到你发布的所有内容的相关代码。
我将举例说明2 CCSprite
个物体碰撞检测。一个叫_arrow
,另一个叫_obstacle
。
第一步是定义碰撞类型,如下所示:
_arrow.physicsBody.collisionType = @"arrow";
_obstacle.physicsBody.collisionType = @"obstacle";
第二步是定义回调
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair arrow:(CCNode *)arrow obstacle:(CCNode *)obstacle
{
// Do some cool stuff
return TRUE;
}
根据这些精灵的碰撞类型注意命名,箭头和障碍物。
第三,你忘记做的就是设置委托,这样你就可以在 physicsNode 对象上实际调用这些方法
_physics.collisionDelegate = self;
self
(通常只是你的场景)应该实现CCPhysicsCollisionDelegate
协议。
答案 1 :(得分:3)
Tibor有正确的答案,但有一个重要的线索,我错过了这让我发疯。来自CCPhysicsNode.h
的{{1}}部分中的评论:
CCPhysicsCollisionDelegate
所以我错误地尝试实现这样的东西:
The final two parameter names (typeA/typeB) should be replaced
with names used with CCPhysicsBody.collisionType or
CCPhysicsShape.collisionType.
If both final parameter names are "default" then the collision
method is called when a more specific method isn't found.
但那是错误!!! 我想要实现的正确之处是:
-(BOOL) ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair typeA:(CCNode *)nodeA typeB:(CCNode *)nodeB
{
// Compare if we're ignored collisions
if (([nodeA.physicsBody.collisionType isEqualToString: @"foo"])
|| ([nodeB.physicsBody.collisionType isEqualToString: @"foo"]) )
{
return NO;
}
return YES;
}
请注意忽略的碰撞类型 方法签名的一部分 。
呼!
修改强>
为完整起见,请不要忘记:
-(BOOL) ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair foo:(CCNode *)nodeA foo:(CCNode *)nodeB
{
return NO;
}
答案 2 :(得分:2)
您正在设置碰撞类别,与碰撞遮罩一起允许您放弃某些碰撞。碰撞类型属性适用于委托方法。