2013-10-06 78 views
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節點作爲輔助體的形狀,從而該形狀可以由兩個凸多邊形進行定製精靈節點時,它創建,因爲它被認爲形狀,但當場景開始播放關節不起作用,因爲它應該是這樣,並將小三角形的方式從其開始的位置移開

回答

0

我解決了我的問題,其實我沒有找到答案,但我想你可以說我的工作在

我意識到的是Bodyjoint的點只服從x座標,但總是將y座標移到0,讓我感動的身體到0,0協調,並再次被移動身體it's先前的位置

CGPoint *prevPosition = self.position; 
    //positioning of the node so that the joint lands exactly 0,0 
    self.position = CGPointMake(-3*self.size.width/8.0,0); 

    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]; 
    //now we asssign the frame, the position does not matter as it is determined by the joint 
    self.auxiliaryShapeNode.frame = CGRectMake(0,0,sel.size.width/8.0,self.height/2); 
    self.auxiliaryShapeNode.anchor = CGPointMake(0,0.5); 
    CGPathMoveToPoint (path_aux, NULL, 0,0); 
    CGPathAddLineToPoint(path_aux, NULL, self.auxiliaryShapeNode.size.width, self.auxiliaryShapeNode.size.width/2.0); 
    CGPathAddLineToPoint(path_aux, NULL, self.auxiliaryShapeNode.size.width, -self.auxiliaryShapeNode.size.width/2.0); 
    CGPathCloseSubpath (path_aux); 
    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:CGPointZero]]; 
    self.position=prevPosition; 

所以,我設法「使工作」這種方式,希望it's對某人有用

+0

您是否使用此代碼獲取內存泄漏?我使用多邊形窗體路徑創建物理體,並在樂器中向我展示PhysicsKit中的奇怪內存泄漏(負責的幀包含有關PKPoint對象的信息)。我的實現看起來與你非常相似,但我沒有關節,創建主體後,我調用'CGPathRelease(path)'。我不知道這是Sprite Kit中的錯誤,還是我做錯了什麼。 – Darrarski

+0

我忘記補充說,當我使用'[SKPhysicsBody bodyWithCircleOfRadius:radius]'或[SKPhysicsBody bodyWithRectangleOfSize:size]'方法創建主體時,我根本沒有得到內存泄漏。 – Darrarski

+0

嗯...也許你應該分享你的代碼,我沒有發現任何內存泄漏,它仍然工作得很好......也許它與你的多邊形的複雜性有關,因爲最複雜的是, ve使用的是梯形,但如果你分享你的代碼,我會很樂意幫助,如果我可以 – heczaco