2014-02-24 42 views
1

我有非常簡單的代碼:物理機構沒有反彈嗎?

-(id)initWithSize:(CGSize)size { 
    if (self = [super initWithSize:size]) { 
     /* Setup your scene here */ 

     self.backgroundColor = [SKColor whiteColor]; 
     self.anchorPoint = CGPointMake(0.5, 0.5); 
     self.physicsWorld.gravity = CGVectorMake(0, -1.0); 
     self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]; 
     self.physicsBody.dynamic = YES; 
     self.physicsBody.affectedByGravity = NO; 
     self.physicsBody.categoryBitMask = wallCategory; 
     self.physicsBody.collisionBitMask = nodeCategory; 
     self.physicsBody.contactTestBitMask = nodeCategory; 
     self.physicsWorld.contactDelegate = self; 
     SKSpriteNode *node1 = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(50, 50)]; 
     SKSpriteNode *node2 = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(20, 20)]; 
     node2.position = CGPointMake(0, -node1.frame.size.height/2+node2.frame.size.height/2); 
     node1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:node1.frame.size]; 
     node1.physicsBody.dynamic = YES; 
     node1.physicsBody.categoryBitMask = nodeCategory; 
     node1.physicsBody.collisionBitMask = wallCategory; 
     node1.physicsBody.contactTestBitMask = wallCategory; 
     node1.physicsBody.restitution = 1.0; 
     node1.physicsBody.friction = 0.0; 
     node1.physicsBody.linearDamping = 0.0; 
     node1.physicsBody.angularDamping = 0.0; 

     [node1 addChild:node2]; 
     [self addChild:node1]; 
    } 
    return self; 
} 

然而,節點1只下降到(受重力影響),而反彈底部的屏幕。我究竟做錯了什麼?

回答

0

給節點1的位置:

node1.position = CGPointMake(self.frame.size.width/2,self.frame.size.height/2); 
+0

這工作,但它是因爲我定義的場景的物理身體是不正確的? – Strelok

+0

物理機構很好。我不完全知道根本原因 - 它看起來像一個節點需要它的位置設置爲啓用物理,但我需要測試更多以確保。 – AndyOS