1
我,基本上在蘋果提供的新框架中嘗試一個有洞的矩形,唯一的問題是它默認的方法bodyWithPolygonFromPath不接受凹多邊形,所以我試圖解決這個問題,如下所示:創建凸SKPhisycsBodies
CGPathMoveToPoint (path, NULL, self.size.width/4.0, self.size.height/4.0);
CGPathAddLineToPoint(path, NULL, self.size.width/2.0, -self.size.height/4.0);
CGPathAddLineToPoint(path, NULL, -self.size.width/2.0, -self.size.height/4.0);
CGPathAddLineToPoint(path, NULL, -self.size.width/2.0, self.size.height/4.0);
CGPathCloseSubpath (path);
self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
self.physicsBody.affectedByGravity = YES;
self.physicsBody.categoryBitMask = solidCategory;
self.physicsBody.dynamic = YES;
[self addChild:shape1];
self.auxiliaryShapeNode = [[SKSpriteNode alloc] init];
CGPathMoveToPoint (path_aux, NULL, 3*self.size.width/8.0, 0);
CGPathAddLineToPoint(path_aux, NULL, self.size.width/2.0, 1.5*self.size.height/4.0);
CGPathAddLineToPoint(path_aux, NULL, self.size.width/2.0, -self.size.height/4.0);
CGPathCloseSubpath (path_aux);
self.auxiliaryShapeNode.anchorPoint = CGPointMake(-3.0, 1.0/2.5);
self.auxiliaryShapeNode.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path_aux];
self.auxiliaryShapeNode.physicsBody.dynamic = YES;
self.auxiliaryShapeNode.physicsBody.categoryBitMask = solidCategory;
[self addChild:self.auxiliaryShapeNode];
[self.scene.physicsWorld addJoint:[SKPhysicsJointFixed jointWithBodyA:self.physicsBody bodyB:self.auxiliaryShapeNode.physicsBody anchor:self.position]];
break;
其中自爲i創建具有SKSprite節點作爲輔助體的形狀,從而該形狀可以由兩個凸多邊形進行定製精靈節點時,它創建,因爲它被認爲形狀,但當場景開始播放關節不起作用,因爲它應該是這樣,並將小三角形的方式從其開始的位置移開
您是否使用此代碼獲取內存泄漏?我使用多邊形窗體路徑創建物理體,並在樂器中向我展示PhysicsKit中的奇怪內存泄漏(負責的幀包含有關PKPoint對象的信息)。我的實現看起來與你非常相似,但我沒有關節,創建主體後,我調用'CGPathRelease(path)'。我不知道這是Sprite Kit中的錯誤,還是我做錯了什麼。 – Darrarski
我忘記補充說,當我使用'[SKPhysicsBody bodyWithCircleOfRadius:radius]'或[SKPhysicsBody bodyWithRectangleOfSize:size]'方法創建主體時,我根本沒有得到內存泄漏。 – Darrarski
嗯...也許你應該分享你的代碼,我沒有發現任何內存泄漏,它仍然工作得很好......也許它與你的多邊形的複雜性有關,因爲最複雜的是, ve使用的是梯形,但如果你分享你的代碼,我會很樂意幫助,如果我可以 – heczaco