我試着用這個簡單的場景在SpriteKit(即SKPhysicsJointSpring
)的春天聯合。很多時候,我有一個紅色的精靈作爲「天花板」,然後是一個橙色精靈,作爲一個質量「塊」,應該由一個彈簧懸掛(注意:我沒有畫任何東西來連接兩個廣場,但只是想象那裏有一個春天)。如何從SKPhysicsJointSpring獲得預期行爲?
使用默認的嚴重性,我希望的是,橙色塊將開始反彈向上和向下,但實際上,它只是坐在那裏。爲了進一步說明我的疑惑,如果我在現場的-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
這些接頭具有內部屬性(與Box2d相同),就像SK不會泄露 – LearnCocos2D