• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 00:04:22 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在制作一个 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap