【发布时间】:2014-05-13 05:46:13
【问题描述】:
我正在尝试制作一个游戏,其中障碍物从屏幕顶部落下,玩家必须尝试避开它们,但 didBeginContact 方法没有被调用,它让我发疯。这是我的代码中与节点冲突相关的部分......
//myScene.h
@interface MyScene : SKScene <SKPhysicsContactDelegate>
//myScene.m
@implementation MyScene
static const uint32_t playerCategory = 0x1 << 0;
static const uint32_t obstacleCategory = 0x1 << 1;
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
//setup scene
self.backgroundColor = [SKColor whiteColor];
self.physicsWorld.gravity = CGVectorMake(0, 0);
self.physicsWorld.contactDelegate = self;
obstacles = [NSMutableArray array];
//add player node
player = [SKNode node];
SKSpriteNode *spritePlayer = [SKSpriteNode spriteNodeWithImageNamed:@"black.png"];
spritePlayer.size = CGSizeMake(50, 50);
spritePlayer.position = CGPointMake(self.frame.size.width/2, 100);
player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.frame.size];
player.physicsBody.dynamic = NO;
player.physicsBody.mass = 0.02;
[player addChild:spritePlayer];
player.physicsBody.categoryBitMask = playerCategory;
player.physicsBody.contactTestBitMask = obstacleCategory;
player.physicsBody.collisionBitMask = 0;
player.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:player];
[self spawnNewObstacles];
}
return self;
}
- (void)spawnNewObstacles
{
//spawn obstacles
obstacle = [SKSpriteNode spriteNodeWithImageNamed:@"black.png"];
obstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obstacle.frame.size];
obstacle.physicsBody.dynamic = YES;
obstacle.physicsBody.categoryBitMask = obstacleCategory;
obstacle.physicsBody.contactTestBitMask = playerCategory;
obstacle.physicsBody.collisionBitMask = 0;
obstacle.physicsBody.usesPreciseCollisionDetection = YES;
}
-(void)didBeginContact:(SKPhysicsContact *)contact
{
NSLog(@"didBeginContact Called!");
}
没有调用 didBeginContact 方法,我找不到我的代码有什么问题 感谢所有帮助...
【问题讨论】:
-
您能检测到与此代码的冲突吗?您在使用此代码时遇到的确切问题是什么?你需要什么帮助?
-
不,我无法检测到任何碰撞(我检查了 NSLogs)并且没有调用 didBeginContact 方法,plc 帮助
-
不要像您的情况那样不必要地设置类别位掩码,请参阅:stackoverflow.com/a/22804376/201863
-
好吧,这有点道理,所以你建议我去掉类别位掩码行? @LearnCocos2D
标签: objective-c xcode5 collision-detection sprite-kit collision