我已经创建了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
提前致谢.
解决方法
从
SKPhysicsWorld文档:
A contact is created when two physics bodies overlap and one of the
physics bodies has acontactTestBitMask
property that overlaps with
the other body’scategoryBitMask
property.
你必须为物理实体分配一个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文档注:
For best performance,only set bits in the contacts mask for interactions you are interested in.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。