2014-01-12 28 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"]; 

所以,是的,這些線路將解決這個問題,但我認爲特別注意警告存在的原因將有助於其他人超越這個具體情況。

方法的返回指針類型必須與您正在使用的變量的指針類型相匹配,否則您將看到該警告。

相關問題