Reputation: 103
I kinda asked the question in the title but suppose I have a UIColor (i.e. [UIColor redColor]. How do i get an NSString to equal @"red" OR how can i find out is a color is the same as [UIColor redColor]?
For example I have tried,
SKSpriteNode *player = [SKSpriteNode spriteNodeWithImageNamed:@"image"];
[player runAction:[SKAction colorWithColor:[SKColor blueColor] withColorBlendFactor:1 duration:0.01];
[self addChild:player];
Then later:
if (player.color == [SKColor blueColor]) { //Also I have tried 'isEqual' and that didn't work either!
}
PLEASE HELP ME! Thanks in advance!
Upvotes: 2
Views: 455
Reputation: 11696
I assume you do not know about userData as rmaddy already suggested so I will give you some sample code. First though, this is what Apple says about the userData:
You use this property to store your own data in a node. For example, you might store game-specific data about each node to use inside your game logic. This can be a useful alternative to creating your own node subclasses to hold game data. Sprite Kit does not do anything with the data stored in the node. However, the data is archived when the node is archived.
@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");
}
Upvotes: 1