问题是接触物理方法
- (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.
以上是内存溢出为你收集整理的ios – didBeginContact:(SKPhysicsContact *)联系人未被调用全部内容,希望文章能够帮你解决ios – didBeginContact:(SKPhysicsContact *)联系人未被调用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)