2014-01-12 49 views
1

目前我正在制作一个非常简单的测试应用程序(我刚开始使用sprite工具包,而且我感觉非常好!)和我会发出警告。该应用程序在我的终端上运行得很好,但黄色线条让我非常讨厌,我想最好是憎恨某些东西并理解它,而不是讨厌某些东西而不理解它。所以这是我的代码,我会解释一下我想要做的事情。不兼容的指针类型用'SKNode *'类型的表达式初始化'SKSpriteNode *'

-(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序列)

警告消息:不兼容的指针类型初始化“SKSpriteNode *”有型“SKNode *”

如果你有兴趣,这里是我的“原始”的声明的表达的物品

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

什么行是警告? – connor

+0

涉及 的行数SKSpriteNode * cloud = [self childNodeWithName:@“Cloud”];''和'SKLabelNode * scoreLabel = [self childNodeWithName:@“scoreLabel”];' – Interprep

+0

SKLabelNode * scoreLabel = [self childNodeWithName:@“ scoreLabel“];将其转换为(SKLabelNode *) –

回答

2

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

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

非常感谢你!那些讨厌的黄线让我非常烦恼,现在他们已经不在了。 – Interprep

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"]; 

所以,是的,这些线路将解决这个问题,但我认为特别注意警告存在的原因将有助于其他人超越这个具体情况。

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

相关问题