gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:21:59 24 4
gpt4 key购买 nike

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

}

有人能找出导致 Sprite 未添加到场景中的原因吗?另外,如何将它添加到场景中?谢谢!

最佳答案

正如@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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com