2014-05-02 31 views
2

我有點問這個問題的稱號,但假設我有一個的UIColor(即[的UIColor redColor]怎麼辦我得到一個NSString等於@「紅」要不我怎麼能找出是顏色相同[的UIColor redColor]我如何獲得的UIColor/SKColor的名稱(即[的UIColor redColor] - > @「紅」

例如我都試過了,

SKSpriteNode *player = [SKSpriteNode spriteNodeWithImageNamed:@"image"]; 
[player runAction:[SKAction colorWithColor:[SKColor blueColor] withColorBlendFactor:1 duration:0.01]; 
[self addChild:player]; 

再後來:

if (player.color == [SKColor blueColor]) { //Also I have tried 'isEqual' and that didn't work either! 
} 

請幫幫我!在此先感謝!

+1

沒有標準的方法來獲取顏色的名稱。你可以創建一個顏色和名字的字典。但這隻適用於有限的顏色列表。您必須使用'isEqual:'來比較兩個顏色對象。什麼是'player.color'?它是一個字符串還是'SKColor'? – rmaddy

+0

這是一個SKSpriteNode,因此它是一個SKColor。我如何製作字典? – user3473834

+0

你的意思是'player'是一個'SKSpriteNode'?你用'NSDictionary'製作一本字典。 – rmaddy

回答

1

我以爲你不知道用戶數據作爲rmaddy已經建議,所以我會給你一些示例代碼。首先,這是Apple對用戶數據所說的內容:

您可以使用此屬性將自己的數據存儲在節點中。例如,您可以在遊戲邏輯中存儲關於每個節點的遊戲特定數據。這可以成爲創建自己的節點子類來保存遊戲數據的有用替代方法。 Sprite Kit不會對存儲在節點中的數據執行任何操作。但是,當節點歸檔時,數據將被歸檔。

@implementation MyScene 
{ 
    SKSpriteNode *frank; 
    SKSpriteNode *sally; 
} 

-(id)initWithSize:(CGSize)size 
{ 
    if (self = [super initWithSize:size]) 
    { 
     frank = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(50, 50)]; 
     frank.position = CGPointMake(100, 100); 
     frank.userData = [NSMutableDictionary dictionary]; 
     [frank.userData setValue:@"blue" forKey:@"color"]; 
     [self addChild:frank]; 

     sally = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)]; 
     sally.position = CGPointMake(200, 100); 
     sally.userData = [NSMutableDictionary dictionary]; 
     [sally.userData setValue:@"red" forKey:@"color"]; 
     [self addChild:sally]; 
    } 
    return self; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInNode: self]; 
    SKNode *node = [self nodeAtPoint:location]; 

    NSMutableDictionary *tempDictionary = node.userData; 
    NSString *tempString = [NSString stringWithFormat:@"%@",[tempDictionary objectForKey:@"color"]]; 

    if([tempString isEqualToString:@"blue"]) 
     NSLog(@"My color is blue"); 

    if([tempString isEqualToString:@"red"]) 
     NSLog(@"My color is red"); 
} 
+0

這將是很好,但我怎麼會這樣做,因爲我有我的顏色從數組中隨機挑選,它不讓我比較顏色。我有它設置像: – user3473834

+0

的NSArray *顏色= @ [[SKColor blueColor],[SKColor redColor],[SKColor黃色]; – user3473834

+0

player.color = colors [rand%colors.count]; – user3473834