在彻底调查该问题后,我得出结论,这是 iOS 9 引入的 SpriteKit 错误。您可以在下面找到一个测试场景,其中包含一个SKEmitterNode
和SKFieldNode
两个SKShapeNodes
标记初始视口和字段的位置。所有节点都是rootNode
用于翻译“世界”的 a 的后代。
虽然场的位置没有改变,但其效果的中心会改变。
SKEmitterNode's
targetNode
仅当属性设置为 时,力计算才正确scene
,这并不总是需要的。

黄色圆圈标记了该字段的位置。然而,粒子被吸引到右上角。以下是目前的一些职位:
Root scene pos: 160x, 284y | parent pos: 160x, 284y
Field scene pos: 80x, 142y | parent pos: -80x, -142y
Target scene pos: 80x, 142y | parent pos: -80x, -142y
如您所见,该字段和黄色圆圈具有相同的位置(和父级)。
当移动场景(即rootNode
唯一的)时,粒子被绘制到另一个靠近左下角的点,同时平移到相反的位置。

翻译后的一些位置:
Root scene pos: 84x, 149y | parent pos: 84x, 149y
Field scene pos: 4x, 7y | parent pos: -80x, -142y
Target scene pos: 4x, 7y | parent pos: -80x, -142y
场地的位置没有改变,但重心却发生了变化。
视频
为了更好地说明这种行为,我上传了一个截屏视频:
https ://youtu.be/EJs4vPYMndU
代码
欢迎任何愿意自己尝试的人,这里是本实验中使用的测试场景的代码:
#import "LEDebugScene.h"
#import "LEPhysicsCategories.h"
@implementation LEDebugScene
SKNode* rootNode;
SKFieldNode* fieldNode;
SKShapeNode* targetPosShape;
- (void) didMoveToView:(SKView *)view {
self.backgroundColor = [SKColor blackColor];
// This will be the desired field's position (in parent space)
CGPoint fieldPosition = CGPointMake(-self.size.width * 0.25, -self.size.height * 0.25);
// 1. Set up a root node to move around to translate the "world"
rootNode = [SKNode node];
rootNode.position = CGPointMake(self.size.width/2.0, self.size.height/2.0);
[self addChild:rootNode];
// 2. Mark the target position of the field with a circle
targetPosShape = [SKShapeNode shapeNodeWithCircleOfRadius:20.0];
targetPosShape.fillColor = [UIColor yellowColor];
targetPosShape.strokeColor = [UIColor brownColor];
[rootNode addChild:targetPosShape];
targetPosShape.position = fieldPosition;
// 3. Mark the initially visible portion of the scene
SKShapeNode* initialViewportShape = [SKShapeNode shapeNodeWithRect: CGRectMake(-self.size.width/2.0,
-self.size.height/2.0,
self.size.width,
self.size.height)];
initialViewportShape.fillColor = [UIColor clearColor];
initialViewportShape.strokeColor = [UIColor redColor];
initialViewportShape.lineWidth = 3.0;
[rootNode addChild:initialViewportShape];
// 4. Add an emitter at the center of the initial viewport
NSString* emitterPath = [[NSBundle mainBundle] pathForResource:@"DebugEmitter" ofType:@"sks"];
SKEmitterNode* emitterNode = [NSKeyedUnarchiver unarchiveObjectWithFile:emitterPath];
emitterNode.fieldBitMask = LECategoryDust;
emitterNode.particlePositionRange = CGVectorMake(self.size.width, self.size.height);
emitterNode.position = CGPointZero;
[rootNode addChild:emitterNode];
// 5. Create a field at the desired position
fieldNode = [SKFieldNode radialGravityField];
fieldNode.userData = [NSMutableDictionary dictionary];
fieldNode.categoryBitMask = LECategoryDust;
fieldNode.falloff = 1.8;
fieldNode.minimumRadius = 20.0;
fieldNode.strength = 2.5;
fieldNode.position = fieldPosition;
[rootNode addChild:fieldNode];
}
- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//Get the touch movement vector
UITouch* touch = [touches anyObject];
CGPoint prevLoc = [touch previousLocationInNode:self];
CGPoint location = [touch locationInNode:self];
CGVector delta = CGVectorMake(location.x - prevLoc.x, location.y - prevLoc.y);
//Translate the world by repositioning the root node
rootNode.position = CGPointMake(rootNode.position.x + delta.dx, rootNode.position.y + delta.dy);
CGPoint rootScenePos = [self convertPoint:rootNode.position fromNode:rootNode.parent];
CGPoint fieldScenePos = [self convertPoint:fieldNode.position fromNode:fieldNode.parent];
CGPoint targetShapeScenePos = [self convertPoint:targetPosShape.position fromNode:targetPosShape.parent];
NSLog(@"");
NSLog(@"Root scene pos: %.0fx, %.0fy | parent pos: %.0fx, %.0fy",
rootScenePos.x, rootScenePos.y, rootNode.position.x, rootNode.position.y);
NSLog(@"Field scene pos: %.0fx, %.0fy | parent pos: %.0fx, %.0fy",
fieldScenePos.x, fieldScenePos.y, fieldNode.position.x, fieldNode.position.y);
NSLog(@"Target scene pos: %.0fx, %.0fy | parent pos: %.0fx, %.0fy",
targetShapeScenePos.x, targetShapeScenePos.y, targetPosShape.position.x, targetPosShape.position.y);
}
@end
尽管尝试了几个小时,但我并没有设法通过重新定位字段节点来补偿计算错误。如果有人这样做,如果他们可以分享它会很棒。
编辑:我还提交了错误报告(23063175)。