弗里斯特了,謝謝你@Smick初始車輛代碼張貼在這裏: Sprite Kit pin joints appear to have an incorrect anchor雪碧套件 - 春接頭(減震器)
我想添加的車輪之間的滑動接頭(左輪)和底盤,以及一個彈簧接頭,以產生減震效果。
用我的代碼下面,我沒有得到壓縮。我意識到這個文檔展示了一個將兩個節點拉到一起的彈簧接頭 - 與我想要的相反。這在SK中可能嗎?
我認爲銷接頭可能是罪魁禍首?當我註銷銷接頭時,汽車零件會變得不合時宜 - 所有東西都會飛濺在屏幕上。原本,銷釘將車輪固定在底盤上,但顯然我想將車輪固定在「減震器」上。
另外,SKPhysicsJointSliding的「axis」參數讓我有點困惑。它想要一個矢量。一個向量相對於?
預先感謝您。
- (SKShapeNode*) makeWheel
{
SKShapeNode *wheel = [[SKShapeNode alloc] init];
CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, 0,0, 16, 0, M_PI*2, YES);
wheel.path = myPath;
wheel.physicsBody.mass = 0.5;
return wheel;
}
- (void) createCar{
// 1. car body
SKSpriteNode *carBody = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(120, 8)];
carBody.position = CGPointMake(200, 700);
carBody.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carBody.size];
carBody.physicsBody.mass = 1.0;
[self addChild:carBody];
// 2. wheels
SKShapeNode *leftWheel = [self makeWheel];
leftWheel.position = CGPointMake(carBody.position.x - carBody.size.width/2, carBody.position.y-40);
leftWheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:16];
[self addChild:leftWheel];
SKShapeNode *rightWheel = [self makeWheel];
rightWheel.position = CGPointMake(carBody.position.x + carBody.size.width/2, carBody.position.y);
rightWheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:16];
[self addChild:rightWheel];
/* Build left shock absorber and attach wheel */
CGVector av =CGVectorMake(0.0, 5.0);
SKPhysicsJointSliding *leftSlide = [SKPhysicsJointSliding jointWithBodyA:carBody.physicsBody
bodyB:leftWheel.physicsBody
anchor:leftWheel.position
axis:av];
SKPhysicsJointSpring *leftSpring = [SKPhysicsJointSpring jointWithBodyA:carBody.physicsBody bodyB:leftWheel.physicsBody
anchorA:CGPointMake(carBody.position.x - carBody.size.width/2, carBody.position.y)
anchorB:leftWheel.position];
SKPhysicsJointPin *leftPin = [SKPhysicsJointPin jointWithBodyA:leftSpring.bodyA
bodyB:leftSpring.bodyB
anchor:leftWheel.position];
[self.physicsWorld addJoint:leftSlide];
[self.physicsWorld addJoint:leftSpring];
[self.physicsWorld addJoint:leftPin];
[self.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:carBody.physicsBody bodyB:rightWheel.physicsBody anchor:rightWheel.position]];
}