OStack程序员社区-中国程序员成长平台

标题: ios - 为什么我的 SpriteNode 没有被添加到场景中? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 00:04
标题: ios - 为什么我的 SpriteNode 没有被添加到场景中?

我正在制作一个 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 selectorselector(handleApplicationWillResignActive name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selectorselector(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)viewWillAppearBOOL)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];

}

谁能弄清楚是什么导致 Sprite 没有被添加到场景中。另外,如何将其添加到场景中?谢谢!



Best Answer-推荐答案


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

[self runAction:actionRepeat];[labelNode runAction:actionRepeat];

你的代码应该是这样的:

-(id)initWithSizeCGSize)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

关于ios - 为什么我的 SpriteNode 没有被添加到场景中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24924233/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4