SCNSphere不能正常旋转

时间:2015-03-29 02:58:45

标签: ios objective-c scenekit

我正在使用SpriteKit制作游戏,并且在该游戏的主菜单中是SK3DNode,其中包含旨在包含旋转行星的SCNScene。我这样设置了

//create scene
SCNScene *planetScene = [[SCNScene alloc] init];
SCNSphere *planet = [SCNSphere sphereWithRadius:2.0];
planet.firstMaterial.diffuse.contents = [UIImage imageNamed:@"Planet_2_d.png"];
SCNNode *plNode = [SCNNode nodeWithGeometry:planet];
[planetScene.rootNode addChildNode:plNode];

//animate planet
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"rotation"];
rotationAnimation.toValue = [NSValue valueWithSCNVector4:SCNVector4Make(0, 1, 0, M_PI * 2)];
rotationAnimation.duration = 6; // One revolution in ten seconds.
rotationAnimation.repeatCount = FLT_MAX; // Repeat the animation forever.
[plNode addAnimation:rotationAnimation forKey:nil]; // Attach the animation to the node to start it.

//create and add sprite kit node
SK3DNode *planetNode = [[SK3DNode alloc] initWithViewportSize:CGSizeMake(125, 125)];
planetNode.scnScene = planetScene;
planetNode.position = CGPointMake(loadGameButton.position.x - 200, CGRectGetMidY(self.frame));
planetNode.autoenablesDefaultLighting = YES;
planetNode.playing = YES;

id s1 = [planetNode valueForKey:@"_scnRenderer"];
NSLog(@"%@", s1);

[self addChild:planetNode];

这是按计划运作的,除了行星不仅仅是旋转的事实。除旋转外,它还可以放大和缩小。我无法在上面的代码中看到任何会导致它以这种方式运行的内容。如何才能使行星旋转而不缩放?

1 个答案:

答案 0 :(得分:1)

通过像这样添加相机到场景来解决这个问题

SCNCamera *camera = [SCNCamera camera];
camera.xFov = 0;
camera.yFov = 0;
camera.zNear = 0.0;
camera.zFar = 10.0;
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = camera;
cameraNode.position = SCNVector3Make(0, 0, 3);
[planetScene.rootNode addChildNode:cameraNode];

planetNode.pointOfView = cameraNode;