正确设置 SKFieldNode.categoryBitMask & physicsBody.contactTestBitMask
Properly setting up SKFieldNode.categoryBitMask & physicsBody.contactTestBitMask
我正在尝试使用发送 NSLog(@"Contact registered between bird & field node.") 的 SKFieldNode 创建一个不可见的边界;当精灵在 SKFieldNode 半径内时通过 didBeginContact 方法。我想我正确地设置了代码,但是当鸟精灵的 physicsBody 接触场节点的半径时什么也没有发生:
-(void)didBeginContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody; //Create 2 placeholder reference's for the contacting objects.
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) //If bodyA has smallest of 2 bits...
{
firstBody = contact.bodyA; //...it is then the firstBody reference [Smallest of two (category) bits.].
secondBody = contact.bodyB; //...and bodyB is then secondBody reference [Largest of two bits.].
}
else //This is the reverse of the above code (just in case so we always know what's what).
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if ((firstBody.categoryBitMask == birdCategory) && (secondBody.categoryBitMask == fieldNodeCategory))
{
NSLog(@"Contact registered between bird & field node.");
}
}
-(void)spriteWithFieldNode
{
//This sprite looks like a worm.
_sprite = [Sprite node];
_sprite.position = CGPointMake(self.sprite2.position.x, self.sprite2.position.y);
[self addChild:_sprite];
//Field node is attached to the worm sprite & registers a contact if a bird is in its radius.
SKFieldNode *fieldNode = [SKFieldNode node];
fieldNode.enabled = YES;
fieldNode.strength = 0.5f;
fieldNode.region = [[SKRegion alloc] initWithRadius:50];
//fieldNode.falloff = 50;
fieldNode.categoryBitMask = fieldNodeCategory;
[_sprite addChild:fieldNode];
}
-(void)birdSprite
{
_bird = [Bird node];
_bird.position = CGPointMake(self.size.width/2, self.size.height/2);
[self addChild:_bird];
}
_bird class physicsBody 设置:
minDiam = MIN(self.size.width, self.size.height);
minDiam = MAX(minDiam - 16, 4);
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:minDiam/3.0 center:CGPointMake(-9, -2)];
self.physicsBody.categoryBitMask = birdyCategory;
self.physicsBody.contactTestBitMask = boundaryCategory | spriteCategory | someOtherCategory;
self.physicsBody.collisionBitMask = boundaryCategory;
self.physicsBody.fieldBitMask = fieldNodeCategory;
self.physicsBody.allowsRotation = NO;
self.physicsBody.linearDamping = 300;
self.physicsBody.restitution = 0.0f; //bounciness.
self.zPosition = 3.1;
您可以尝试将 radius
的隐形 static physics body
与 fieldNode
相等,并检测鸟与此 body 的接触。
SKSpriteNode *fieldBody = [[SKSpriteNode alloc] initWithColor:[UIColor redColor] size:CGSizeZero];
fieldBody.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:50];
fieldBody.position = _sprite.position
fieldBody.physicsBody.dynamic = NO;
fieldBody.physicsBody.categoryBitMask = fieldNodeCategory;
[self addChild:fieldBody];
并在 birdNode
设置中
self.physicsBody.contactTestBitMask = boundaryCategory | spriteCategory | someOtherCategory | fieldNodeCategory;
我正在尝试使用发送 NSLog(@"Contact registered between bird & field node.") 的 SKFieldNode 创建一个不可见的边界;当精灵在 SKFieldNode 半径内时通过 didBeginContact 方法。我想我正确地设置了代码,但是当鸟精灵的 physicsBody 接触场节点的半径时什么也没有发生:
-(void)didBeginContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody; //Create 2 placeholder reference's for the contacting objects.
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) //If bodyA has smallest of 2 bits...
{
firstBody = contact.bodyA; //...it is then the firstBody reference [Smallest of two (category) bits.].
secondBody = contact.bodyB; //...and bodyB is then secondBody reference [Largest of two bits.].
}
else //This is the reverse of the above code (just in case so we always know what's what).
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if ((firstBody.categoryBitMask == birdCategory) && (secondBody.categoryBitMask == fieldNodeCategory))
{
NSLog(@"Contact registered between bird & field node.");
}
}
-(void)spriteWithFieldNode
{
//This sprite looks like a worm.
_sprite = [Sprite node];
_sprite.position = CGPointMake(self.sprite2.position.x, self.sprite2.position.y);
[self addChild:_sprite];
//Field node is attached to the worm sprite & registers a contact if a bird is in its radius.
SKFieldNode *fieldNode = [SKFieldNode node];
fieldNode.enabled = YES;
fieldNode.strength = 0.5f;
fieldNode.region = [[SKRegion alloc] initWithRadius:50];
//fieldNode.falloff = 50;
fieldNode.categoryBitMask = fieldNodeCategory;
[_sprite addChild:fieldNode];
}
-(void)birdSprite
{
_bird = [Bird node];
_bird.position = CGPointMake(self.size.width/2, self.size.height/2);
[self addChild:_bird];
}
_bird class physicsBody 设置:
minDiam = MIN(self.size.width, self.size.height);
minDiam = MAX(minDiam - 16, 4);
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:minDiam/3.0 center:CGPointMake(-9, -2)];
self.physicsBody.categoryBitMask = birdyCategory;
self.physicsBody.contactTestBitMask = boundaryCategory | spriteCategory | someOtherCategory;
self.physicsBody.collisionBitMask = boundaryCategory;
self.physicsBody.fieldBitMask = fieldNodeCategory;
self.physicsBody.allowsRotation = NO;
self.physicsBody.linearDamping = 300;
self.physicsBody.restitution = 0.0f; //bounciness.
self.zPosition = 3.1;
您可以尝试将 radius
的隐形 static physics body
与 fieldNode
相等,并检测鸟与此 body 的接触。
SKSpriteNode *fieldBody = [[SKSpriteNode alloc] initWithColor:[UIColor redColor] size:CGSizeZero];
fieldBody.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:50];
fieldBody.position = _sprite.position
fieldBody.physicsBody.dynamic = NO;
fieldBody.physicsBody.categoryBitMask = fieldNodeCategory;
[self addChild:fieldBody];
并在 birdNode
设置中
self.physicsBody.contactTestBitMask = boundaryCategory | spriteCategory | someOtherCategory | fieldNodeCategory;