我以爲你不知道用戶數據作爲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");
}
沒有標準的方法來獲取顏色的名稱。你可以創建一個顏色和名字的字典。但這隻適用於有限的顏色列表。您必須使用'isEqual:'來比較兩個顏色對象。什麼是'player.color'?它是一個字符串還是'SKColor'? – rmaddy
這是一個SKSpriteNode,因此它是一個SKColor。我如何製作字典? – user3473834
你的意思是'player'是一個'SKSpriteNode'?你用'NSDictionary'製作一本字典。 – rmaddy