我正在制作一个SpriteNode并尝试将其添加到场景中。即使使用[self addChild: child]也不会显示。我只是看不到哪里出了问题。这是我的View Controller.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Pause the view (and thus the game) when the app is interrupted or backgrounded
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleApplicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleApplicationDidBecomeActive:)  name:UIApplicationDidBecomeActiveNotification  object:nil];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    SKScene * scene = [TitleScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
}

这是我的Scene.m
- (void)viewWillAppear:(BOOL)animated
{
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    SKTexture *YellowLabelTexture = [SKTexture textureWithImageNamed:@"YellowLabel.png"];
    SKTexture *BlueLabelTexture = [SKTexture textureWithImageNamed:@"BlueLabel.png"];
    SKTexture *GreenLabelTexture = [SKTexture textureWithImageNamed:@"GreenLabel.png"];
    SKTexture *RedLabelTexture = [SKTexture textureWithImageNamed:@"RedLabel.png"];
    SKTexture *WhiteLabelTexture = [SKTexture textureWithImageNamed:@"WhiteLabel.png"];

     NSArray *anim = [NSArray arrayWithObjects:YellowLabelTexture, BlueLabelTexture, GreenLabelTexture, RedLabelTexture, WhiteLabelTexture, nil];

    SKSpriteNode *labelNode = [SKSpriteNode spriteNodeWithImageNamed:@"WhiteLabel.png"];
    labelNode.position = CGPointMake(160, 400);

    SKAction *actionAnimate = [SKAction animateWithTextures:anim timePerFrame:.5 resize:YES restore:NO];
    SKAction *actionRepeat = [SKAction repeatActionForever:actionAnimate];
    [self runAction:actionRepeat];

    [self addChild:labelNode];

}

有人可以找出导致子画面未添加到场景的原因吗?另外,如何将其添加到场景中?谢谢!

最佳答案

正如@akashg所说,您不能使用viewWillAppear方法,也应该对labelNode使用runAction:
[self runAction:actionRepeat];[labelNode runAction:actionRepeat];
您的代码应如下所示:

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */


        SKView * skView = (SKView *)self.view;
        skView.showsFPS = YES;
        skView.showsNodeCount = YES;

        SKTexture *YellowLabelTexture = [SKTexture textureWithImageNamed:@"YellowLabel.png"];
        SKTexture *BlueLabelTexture = [SKTexture textureWithImageNamed:@"BlueLabel.png"];
        SKTexture *GreenLabelTexture = [SKTexture textureWithImageNamed:@"GreenLabel.png"];
        SKTexture *RedLabelTexture = [SKTexture textureWithImageNamed:@"RedLabel.png"];
        SKTexture *WhiteLabelTexture = [SKTexture textureWithImageNamed:@"WhiteLabel.png"];

        NSArray *anim = [NSArray arrayWithObjects:YellowLabelTexture, BlueLabelTexture, GreenLabelTexture, RedLabelTexture, WhiteLabelTexture, nil];

        SKSpriteNode *labelNode = [SKSpriteNode spriteNodeWithImageNamed:@"WhiteLabel.png"];
        labelNode.position = CGPointMake(160, 400);

        SKAction *actionAnimate = [SKAction animateWithTextures:anim timePerFrame:.5 resize:YES restore:NO];
        SKAction *actionRepeat = [SKAction repeatActionForever:actionAnimate];
        [labelNode runAction:actionRepeat];

        [self addChild:labelNode];

    }
    return self;
}

@end

10-08 08:12