我正在尝试使用 a 在静止背景上创建移动背景NSTimer
,它工作正常,直到我调用命令添加另一个孩子。这是我得到的错误。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: <SKSpriteNode> name:'(null)' texture:[<SKTexture> 'groundGrass.png' (53 x 71)] position:{159, 35} size:{808, 71}
这是我移动地面的代码:
-(void)moveGroundGrass{
groundGrass.position = CGPointMake(groundGrass.position.x -1, groundGrass.position.y);
if (groundGrass.position.x < 160){
[self addChild:groundGrass];
}
这是其余的:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
background.xScale = 0.9;
background.yScale = 1.1837f;
[self addChild:background];
groundGrass = [SKSpriteNode spriteNodeWithImageNamed:@"groundGrass"];
groundGrass.position = CGPointMake (404, 35);
[self addChild:groundGrass];
moveGround = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(moveGroundGrass) userInfo:nil repeats:YES];
}
return self;
}
当 groundGrass.png 移过屏幕上应该添加另一个子节点的点时,它会崩溃...