Ios 为什么不是';我的SpriteNode没有被添加到场景中吗?
我正在制作一个SpriteNode并试图将其添加到场景中。即使我使用Ios 为什么不是';我的SpriteNode没有被添加到场景中吗?,ios,objective-c,sprite-kit,skspritenode,Ios,Objective C,Sprite Kit,Skspritenode,我正在制作一个SpriteNode并试图将其添加到场景中。即使我使用[self addChild:child],它也不会显示。我只是看不出哪里出了问题。这是我的视图控制器。m: - (void)viewDidLoad { [super viewDidLoad]; // Pause the view (and thus the game) when the app is interrupted or backgrounded [[NSNotificationCenter
[self addChild:child]
,它也不会显示。我只是看不出哪里出了问题。这是我的视图控制器。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];
}
这是我的场景
- (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所说,您不能使用
viewwillbeen
方法,而且您还应该对labelNode使用runAction:
[self-runAction:actionRepeat]代码>至[labelNode运行操作: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
没有与场景关联的视图:
方法。您需要在initWithSize:
或didMoveToView:
方法中添加上述代码。谢谢!工作起来很有魅力!