2014-03-31 68 views
-1

我試着用這個簡單的場景在SpriteKit(即SKPhysicsJointSpring)的春天聯合。很多時候,我有一個紅色的精靈作爲「天花板」,然後是一個橙色精靈,作爲一個質量「塊」,應該由一個彈簧懸掛(注意:我沒有畫任何東西來連接兩個廣場,但只是想象那裏有一個春天)。如何從SKPhysicsJointSpring獲得預期行爲?

enter image description here

使用默認的嚴重性,我希望的是,橙色塊將開始反彈向上和向下,但實際上,它只是坐在那裏。爲了進一步說明我的疑惑,如果我在現場的-didMoveToView:方法結束時取消註釋某個力的應用,矢量的方向似乎實際上正在影響橙色塊(它開始作爲鐘擺),但方向爲y矢量似乎不影響任何東西。就好像彈簧真的像一根剛性棒一樣。這是否應該發生?

最後,爲什麼鐘擺般的運動最終會衰減?看起來默認的摩擦是0.0,我自己也沒有施加任何摩擦。有人能幫助我更好地瞭解這個SKPhysicsJointSpring

#import "XYZMainScene.h" 

@interface XYZMainScene() 

@property (nonatomic, strong) SKSpriteNode *ceiling; 
@property (nonatomic, strong) SKSpriteNode *block; 

@end 

@implementation XYZMainScene 

- (void)didMoveToView:(SKView *)view { 

    SKSpriteNode *ceiling = self.ceiling; 
    [self addChild:ceiling]; 

    SKSpriteNode *block = self.block; 
    [self addChild:block]; 

    SKPhysicsJointSpring *spring = [SKPhysicsJointSpring jointWithBodyA:ceiling.physicsBody 
                    bodyB:block.physicsBody 
                   anchorA:ceiling.position 
                   anchorB:block.position]; 
    [self.physicsWorld addJoint:spring]; 

// [block.physicsBody applyForce:CGVectorMake(60, -100)]; 
} 

- (SKSpriteNode *)ceiling { 

    if (!_ceiling) { 
     _ceiling = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] 
               size:CGSizeMake(30, 30)]; 

     _ceiling.position = CGPointMake(self.frame.size.width/2, 400); 
     _ceiling.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:_ceiling.position 
                  toPoint:_ceiling.position]; 
    } 
    return _ceiling; 
} 

- (SKSpriteNode *)block { 

    if (!_block) { 
     _block = [SKSpriteNode spriteNodeWithColor:[SKColor orangeColor] 
               size:CGSizeMake(50,50)]; 

     _block.position = CGPointMake(self.ceiling.position.x, self.ceiling.position.y - 200); 
     _block.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_block.frame.size]; 
    } 
    return _block; 
} 

@end 
+0

這些接頭具有內部屬性(與Box2d相同),就像SK不會泄露 – LearnCocos2D

回答

2

您需要更改SKPhysicsJointSpringfrequencydamping性能。

SKPhysicsJointSpring *spring = [SKPhysicsJointSpring jointWithBodyA:ceiling.physicsBody 
                    bodyB:block.physicsBody 
                   anchorA:ceiling.position 
                   anchorB:block.position]; 
spring.frequency = 1.0; //gives the spring some elasticity. 
spring.damping = 0.0; //Will remove damping to create the 'pendulum' 
[self.physicsWorld addJoint:spring]; 

閱讀SKPhysicsJointSpring類參考here

+0

根據類參考頁,「damping」的默認值爲「0.0」。我也嘗試過使用「頻率」,但是「阻尼」和「頻率」似乎都不影響運動。就好像這個值被忽略了......你能否驗證設置頻率和阻尼會解決我的問題? – akivajgordon

+0

是的,我複製了你的代碼,並在我的系統上檢查它。頻率值使它像一個彈簧一樣升降。 – ZeMoon

+0

我還設置了塊的物理特性BodyBody – ZeMoon