Interprep
Interprep

Reputation: 23

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

At the moment I'm making a very simple testing app (I just started sprite kit, and I'm feeling really good!) and I'm getting a warning. The app runs 100% fine on my end but the yellow line is really annoying me and I guess it's better to hate something and understand it rather than hate something and not understand it. So here's my code, and I'll explain what I'm trying to do after it.

-(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];
    }
}

Basically I'm trying to declare these 'objects' for later use in my code so that I can alter them (for the scoreLabel I want to be able to update the score and for the cloud I want to be able to use an SKAction sequence on it)

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

If you're interested, here's my 'original' declaration of the objects as well

-(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;
}

Upvotes: 1

Views: 1282

Answers (2)

prototypical
prototypical

Reputation: 6751

I know there is an answer that solves the problem of having those yellow warning lines. However, I think it's important to understand why this is happening, otherwise you are going to continue to see this same issue in other situations as well.

The SKNode class has a method called childNodeWithName: and that method returns a SKNode pointer. Your pointer is typed as SKSpriteNode.

Thus the resulting message :

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

That is the reason you need to cast to the desired pointer type - SKSpriteNode or SKLabelNode in your case.

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

So, yes those lines will fix the issue, but I think specifically noting the reason the warning exists will be helpful to others beyond this specific case.

The return pointer type of a method must match the pointer type of your variable you are working with, or you will see that warning.

Upvotes: 2

Connor
Connor

Reputation: 64644

You're setting an SKSpriteNode or SKLabelNode to an SKNode. You are receiving the warning because the SKNode may not be an SKSpriteNode or SKLabelNode. To remove the warnings you can use casting like this:

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

Upvotes: 2

Related Questions