Reputation: 35
Using apple spritekit, how can I uniquely identify nodes if, say 20 nodes, they have same art work and are to be placed randomly on the screen? Is there a faster way like in cocos2d there is a"tag" function to identify?
Upvotes: 2
Views: 209
Reputation: 8225
I'm usually using the property name:
SKNode *myNode = [[SKNode alloc]init];
myNode.name = @"uniqueName";
[self addChild:myNode];
In the SKScene, to recovery the node you can do:
[self childNodeWithName:@"uniqueName"]; // self is SKScene
If for some reason you don't want to use name, you can always subclass one SKNode and add your personal unique identifier:
MySpriteNode.h
@interface MySpriteNode : SKSpriteNode
@property (nonatomic, strong) NSString *personalIdentifier;
@end
and MySpriteNode.m
#import "MySpriteNode.h"
@implementation MySpriteNode
@end
with this second option you can:
for (MySpriteNode *sprite in [self children]) personalIdentifier
{
if ([sprite.personalIdentifier isEqualToString:@"something"])
{
//do something
}
}
EDIT 1:
Try to follow these tutorials, I really think they are great. I learned a lot with them:
Upvotes: 2
Reputation: 53142
I'm not the most proficient with sprites, but is this something you're looking for?
Save:
NSArray * nodesArray; // some array of nodes.
for (int x = 0; x < nodesArray.count; x++) {
SKNode * node = nodesArray[x];
node.name = [NSString stringWithFormat:@"%i", x];
}
Retrieve:
int nodeToRetrieveTag = 2; // or whatever
SKNode* nodeToRetrieve = [self.scene childNodeWithName:[NSString stringWithFormat:@"%i", nodeToRetrieveTag]];
Upvotes: 2
Reputation: 4923
Are you looking for enumerateChildNodesWithName:usingBlock: method?
Upvotes: 0