2014-03-24 60 views
1

我正在模擬物理到蛋形SKSprite節點。 但是,斷言失敗的錯誤發生:當使用bodyWithPolygonFromPath時,斷言失敗:蛋形狀的方法

斷言失敗:(edge.LengthSquared()> 1.19209290e-7F * 1.19209290e-7F),功能設置,文件/SourceCache/PhysicsKit_Sim/PhysicsKit-6.5。 4/PhysicsKit/Box2D的/碰撞/型材/ b2PolygonShape.cpp,管線180

以下是代碼:

self.egg = [SKSpriteNode spriteNodeWithImageNamed:IMAGE_NAME_EGG]; 
    [self.egg setScale:0.2]; 
    self.egg.position = CGPointMake(self.size.width/2,self.size.height - self.egg.size.height/2); 
    self.egg.name = IMAGE_NAME_EGG; 

    CGPoint startPoint = CGPointMake(0, self.egg.size.height*0.4); 
    CGPoint endPoint = CGPointMake(self.egg.size.width, startPoint.y); 
    CGPoint controlPointLeft = CGPointMake(startPoint.x, self.egg.size.height); 
    CGPoint controlPointRight = CGPointMake(endPoint.x, controlPointLeft.y); 


    CGMutablePathRef pathRef = CGPathCreateMutable();   
    CGPathMoveToPoint(pathRef, NULL, startPoint.x, startPoint.y); 
    CGPathAddQuadCurveToPoint(pathRef, NULL, controlPointLeft.x, controlPointLeft.y, self.egg.size.width/2, controlPointLeft.y); 
    CGPathAddQuadCurveToPoint(pathRef, NULL, controlPointRight.x, controlPointRight.y, endPoint.x,endPoint.y); 
    CGPathAddArc(pathRef, NULL, self.egg.size.width/2, startPoint.y, self.egg.size.width/2, 0, M_PI, NO); 

    self.egg.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:pathRef]; 
    self.egg.physicsBody.dynamic = YES; 
    self.egg.physicsBody.categoryBitMask = eggCategory; 
    self.egg.physicsBody.contactTestBitMask = rabbitCategory; 
    self.egg.physicsBody.collisionBitMask = rabbitCategory; 
    self.egg.physicsBody.allowsRotation = YES; 
    [self addChild:self.egg]; 

有什麼不對?任何人都可以幫我解決?非常感謝你!

回答

1

根據該斷言,其中一個邊緣比sprite套件預期的最小尺寸短。

Assertion failed: (edge.LengthSquared() > 1.19209290e-7F * 1.19209290e-7F) 

檢查路徑座標,並確保它不會太小。

當您使用 「bodyWithPolygonFromPath:」
0

我試圖做一個三角形路徑時,對舊的iOS7 iPhone這個錯誤。事實證明,加入最後一行帶來的路徑返回到導致錯誤的起點IE(雖然新版iOS版本允許這樣。):

CGPathMoveToPoint(path, nil, self.size.width/2, self.size.height/2) 
CGPathAddLineToPoint(path, nil, -self.size.width/2, 0.0) 
CGPathAddLineToPoint(path, nil, self.size.width/2, -self.size.height/2) 
CGPathAddLineToPoint(path, nil, self.size.width/2, self.size.height/2) //REMOVED THIS LINE TO FIX 
CGPathCloseSubpath(path) 

這解釋了爲什麼是太小邊緣評估 - 兩個相同點之間的邊界是0!

+0

墜機的原因是雞蛋不是雞蛋;這是一個新月形狀。如果OP在'CGPathAddArc'調用中將NO更改爲YES,它將成爲一個雞蛋,並且應用程序不會崩潰。 – 0x141E