我已经创建了SKScene继承类。问题是物理体接触法
- (void)didBeginContact:(SKPhysicsContact *)contact 不是调用的解决方案可能很简单,但作为初学者使用雪碧工具包,我被困在这个。
下面是代码
#import "MyScene.h"
@interface MyScene ()
@property BOOL contentCreated;
@end
@implementation MyScene
- (id)initWithSize:(CGSize)size {
self = [super initWithSize:size];
if (self) {
self.physicsWorld.contactDelegate = self;
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
}
return self;
}
- (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self buildWorld];
self.physicsWorld.contactDelegate = self;
}
}
#pragma mark - World Building
- (void)buildWorld {
NSLog(@"Building the world");
SKSpriteNode * sprite1 = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(100,100)];
sprite1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(100,100)];
sprite1.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) +100);
SKSpriteNode * sprite2 = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(100,100)];
sprite2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(100,100)];
sprite2.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - 100);
[self addChild:sprite1];
[self addChild:sprite2];
}
- (void)didBeginContact:(SKPhysicsContact *)contact
{
NSLog(@"contact");
}
@end提前谢谢。
发布于 2013-10-30 20:59:34
来自SKPhysicsWorld文档:
当两个物理体重叠,其中一个物理体具有与另一个物理体的
contactTestBitMask属性重叠的categoryBitMask属性时,就会创建一个接触。
你必须给物理团体分配一个categoryBitMask和一个contactTestBitMask。您首先要创建您的类别:
static const uint32_t sprite1Category = 0x1 << 0;
static const uint32_t sprite2Category = 0x1 << 1;接下来,分配类别和联系人测试位掩码:
sprite1.physicsBody.categoryBitMask = sprite1Category;
sprite1.physicsBody.contactTestBitMask = sprite2Category;
sprite2.physicsBody.categoryBitMask = sprite2Category;
sprite2.physicsBody.contactTestBitMask = sprite1Category;SKPhysicsBody文档中的说明:
为了获得最好的性能,只在联系人掩码中设置您感兴趣的交互的位。
https://stackoverflow.com/questions/19675967
复制相似问题