我无法将所有精灵节点添加到单个父节点,然后在屏幕上显示该父节点内的所有子节点。
我目前的工作方法是简单地将已添加到精灵方法中的孩子带到- (id)initWithSize:(CGSize)size
方法中进行调用。
所以它目前看起来像这样:
MenuScene.m中的代码:
- (id)initWithSize:(CGSize)size{
if(self = [super initWithSize:size]){
_skyColor = [SKColor colorWithRed:25.0/255.0 green:171.0/225 blue:1 alpha:1];
[self setBackgroundColor:_skyColor];
[self nameMovement];
[self playButtonPushed];
[self groundMovement];
[self cloudMovement];
}
return self;
}
以下代码运行良好,但我宁愿只有一个节点保存由groundMovement,cloudMovement..etc方法持有的所有子代。
我尝试过制作一个名为_moving的新SKNode,然后对剩下的带有精灵运动的方法执行此操作。
下面是groundMovement方法,我试图实现我创建的名为_moving的父节点(我所做的唯一更改是在底部)。当我这样做时,我通过添加_moving = [SKNode node];
在新的initWithSize中初始化了SKNode。
声明了SKNode的新initWithSize并尝试绘制父节点(SKNode * _moving在.h文件中):
- (id)initWithSize:(CGSize)size{
if(self = [super initWithSize:size]){
_skyColor = [SKColor colorWithRed:25.0/255.0 green:171.0/225 blue:1 alpha:1];
[self setBackgroundColor:_skyColor];
SKNode *_moving = [SKNode node];
[self addChild:_moving];
}
return self;
}
groundMovement方法:
- (void)groundMovement{
//Ground texture declaration
groundTexture = [SKTexture textureWithImageNamed:@"Ground"];
groundTexture.filteringMode = SKTextureFilteringNearest;
//SKAction Declarations
groundSpriteMovement = [SKAction moveByX:-groundTexture.size.width/2 y:0 duration:0.04 * 100];
resetgroundSpriteMovement = [SKAction moveByX:groundTexture.size.width/2 y:0 duration:0];
repeatForevergroundSpriteMovement = [SKAction repeatActionForever:[SKAction sequence:@[groundSpriteMovement, resetgroundSpriteMovement]]];
//Ground Sprite declaration
groundSprite = [SKSpriteNode spriteNodeWithTexture:groundTexture];
groundSprite.anchorPoint = CGPointMake(0, 0);
groundSprite.position = CGPointMake(0, 0);
[groundSprite runAction:repeatForevergroundSpriteMovement];
[_moving addChild:groundSprite];
}