为什么我的SpriteNode没有被添加到场景中?

时间:2014-07-24 02:39:38

标签: ios objective-c sprite-kit skspritenode

我正在创建一个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];

}

有人可以找出导致精灵不被添加到场景的原因。另外,如何将其添加到场景中?谢谢!

1 个答案:

答案 0 :(得分:1)

正如@akashg所说,你不能使用viewWillAppear方法,你也应该使用runAction作为labelNode:

[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