2014-04-07 62 views
0

我在我的應用程序中使用sprite kit進行物理和碰撞檢測。忽略SKPhysicsBody之間的衝突

我有一些球落入一個盒子裏。盒子的內部是用bodyWithPolygonFromPath來定義的

我把一些球放入盒子中,它們直接落下。

下面是用於定義框

SKSpriteNode* boxFront = [SKSpriteNode spriteNodeWithImageNamed: [boxData objectForKey: @"box_image"]]; 
boxFront.position = CGPointMake(0, -screenHeight*0.22); 
boxFront.zPosition = 10; 
[self addChild: boxFront]; 

CGFloat offsetX = boxFront.frame.size.width * boxFront.anchorPoint.x; 
CGFloat offsetY = boxFront.frame.size.height * boxFront.anchorPoint.y; 

CGMutablePathRef path = CGPathCreateMutable(); 

CGPathMoveToPoint(path, NULL, 3 - offsetX, 129 - offsetY); 
CGPathAddLineToPoint(path, NULL, 0 - offsetX, 129 - offsetY); 
CGPathAddLineToPoint(path, NULL, 0 - offsetX, 85 - offsetY); 
CGPathAddLineToPoint(path, NULL, 200 - offsetX, 85 - offsetY); 
CGPathAddLineToPoint(path, NULL, 200 - offsetX, 129 - offsetY); 
CGPathAddLineToPoint(path, NULL, 197 - offsetX, 129 - offsetY); 
CGPathAddLineToPoint(path, NULL, 196 - offsetX, 87 - offsetY); 
CGPathAddLineToPoint(path, NULL, 3 - offsetX, 87 - offsetY); 

CGPathCloseSubpath(path); 

boxFront.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path]; 
boxFront.physicsBody.dynamic = NO; 

SKShapeNode* shape = [[SKShapeNode alloc] init]; 
shape.path = path; 
shape.strokeColor = [UIColor redColor]; 
shape.zPosition = 101; 
shape.lineWidth = 2; 
[boxFront addChild: shape]; 

與SKShapeNode最後一位剛剛抽出用於調試框的輪廓的代碼。繪製的框是我期望碰撞盒的位置。

這是我放入盒子的球的代碼。

SKSpriteNode* circle1 = [SKSpriteNode spriteNodeWithImageNamed: @"ball.png"]; 
circle1.position = CGPointMake(0, screenHeight*0.7); 
circle1.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius: 20]; 
circle1.zPosition = 100; 
[self addChild: circle1]; 

SKSpriteNode* circle2 = [SKSpriteNode spriteNodeWithImageNamed: @"ball.png"]; 
circle2.position = CGPointMake(-10, screenHeight); 
circle2.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius: 20]; 
circle2.zPosition = 100; 
[self addChild: circle2]; 

自我在上面的代碼段的所有實例都是其直接附接至SKScene相同SKNode。

有關可能導致碰撞檢測被忽略的任何建議?

+0

你在哪裏添加boxFront到場景? – nicael

+0

抱歉,我忘了添加該行,它肯定會被添加,因爲我可以在屏幕上看到圖像。我將編輯添加。 – Tiddly

+0

嘗試添加它後設置其physicsBody – nicael

回答

0

我沒有看到你爲你的箱子或球宣佈physicsBody.categoryBitMaskphysicsBody.collisionBitMask屬性。如果你沒有爲你的對象定義這些對象,你的代碼將不知道哪些物理機構應該相互交互。

+0

根據Apple文檔,默認值應與所有內容碰撞。無論如何,我嘗試着設置它們,並沒有任何區別。 – Tiddly