不兼容的指针类型使用“SKNode *”类型的表达式初始化“SKSpriteNode *”

时间:2014-01-12 18:09:48

标签: objective-c sprite sprite-kit

目前我正在制作一个非常简单的测试应用程序(我刚开始使用sprite kit,我感觉非常好!)我正在收到警告。该应用程序在我的结束时运行100%罚款,但黄线真的很烦我,我想最好讨厌一些东西并理解它而不是讨厌某些东西而不理解它。所以这是我的代码,我将解释我之后要做的事情。

-(void)selectNodeForTouch:(CGPoint)touchlLocation {

// Checks if a cloud was touched
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchlLocation];
if ([[touchedNode name] isEqualToString:@"Cloud"]) {
    // NSLog(@"You touched a cloud!");
    // Allows me to interact with a label that was defined elsewhere

    /* Figure out what is causing this stupid error!  It runs fine, but it's an annoying yellow line */
    SKLabelNode *scoreLabel = [self childNodeWithName:@"scoreLabel"];
    score++;
    scoreLabel.text = [NSString stringWithFormat:@"%d", score];

    // Allows me to interact with a cloud that was defined elsewhere
    SKSpriteNode *cloud = [self childNodeWithName:@"Cloud"];

    // Remove the node with an action (animated of course)
    SKAction *grow = [SKAction scaleTo:1.2 duration:0.1];
    SKAction *shrink = [SKAction scaleTo:0 duration:0.07];
    SKAction *removeNode = [SKAction removeFromParent];
    SKAction *seq = [SKAction sequence:@[grow,shrink,removeNode]];
    [cloud runAction:seq];
    }
}

基本上我正在尝试声明这些'对象'供以后在我的代码中使用,以便我可以改变它们(对于scoreLabel,我希望能够更新分数和我希望能够使用的云)它上面的SKAction序列)

警告消息:使用“SKNode *”类型的表达式初始化“SKSpriteNode *”的指针类型不兼容

如果你有兴趣,这里也是对象的'原始'声明

-(id)initWithSize:(CGSize)size {    
if (self = [super initWithSize:size]) {
    /* Setup your scene here */

    // Create Score Label
    self.backgroundColor = [SKColor colorWithRed:0.204 green:0.596 blue:0.859 alpha:1.0];
    SKLabelNode *label = [SKLabelNode labelNodeWithFontNamed:@"ArialRoundedMTBold"];
    label.fontSize     = 48;
    label.text         = @"0";
    label.position     = CGPointMake(CGRectGetMidX(self.frame), 40);
    label.fontColor    = [SKColor colorWithRed:1 green:1 blue:1 alpha:1.0];
    label.zPosition    = 9999999;
    label.name         = @"scoreLabel";

    [self addChild:label];


    // Create Cloud
    SKSpriteNode *cloud = [SKSpriteNode spriteNodeWithImageNamed:@"Cloud"];
    cloud.position = CGPointMake(50, 50);
    int random = arc4random();
    NSLog([NSString stringWithFormat:@"%d", random]);
    cloud.size     = CGSizeMake(80, 56);
    cloud.name     = @"Cloud";

    [self addChild:cloud];


}
return self;
}

2 个答案:

答案 0 :(得分:2)

您正在将SKSpriteNode或SKLabelNode设置为SKNode。您收到警告,因为SKNode可能不是SKSpriteNode或SKLabelNode。要删除警告,您可以使用这样的强制转换:

SKSpriteNode *cloud = (SKSpriteNode*)[self childNodeWithName:@"Cloud"];
SKLabelNode *scoreLabel = (SKLabelNode*)[self childNodeWithName:@"scoreLabel"];

答案 1 :(得分:2)

我知道有一个答案可以解决出现这些黄色警告线的问题。但是,我认为理解为什么会发生这种情况很重要,否则你将在其他情况下继续看到同样的问题。

SKNode类有一个名为childNodeWithName:的方法,该方法返回一个SKNode指针。您的指针键入为SKSpriteNode

结果消息:

Warning Message: Incompatible pointer types initializing 'SKSpriteNode *' with an expression of type 'SKNode *'

这就是你需要在你的情况下转换为所需指针类型的原因 - SKSpriteNodeSKLabelNode

SKSpriteNode *cloud = (SKSpriteNode*)[self childNodeWithName:@"Cloud"];
SKLabelNode *scoreLabel = (SKLabelNode*)[self childNodeWithName:@"scoreLabel"];

所以,是的,这些线将解决问题,但我认为特别注意警告存在的原因将对这个特定案例之外的其他人有所帮助。

方法的返回指针类型必须与您正在使用的变量的指针类型匹配,否则您将看到该警告。

相关问题