【发布时间】:2014-04-20 09:33:56
【问题描述】:
使用apple spritekit,如果20个节点具有相同的艺术作品并且要随机放置在屏幕上,我如何唯一标识节点?有没有像cocos2d那样更快的方法有一个“标签”功能来识别?
【问题讨论】:
-
每个节点都有一个“名称”属性。你可以使用它。
标签: ios xcode5 sprite-kit
使用apple spritekit,如果20个节点具有相同的艺术作品并且要随机放置在屏幕上,我如何唯一标识节点?有没有像cocos2d那样更快的方法有一个“标签”功能来识别?
【问题讨论】:
标签: ios xcode5 sprite-kit
我不是最精通精灵,但这是你要找的东西吗?
保存:
NSArray * nodesArray; // some array of nodes.
for (int x = 0; x < nodesArray.count; x++) {
SKNode * node = nodesArray[x];
node.name = [NSString stringWithFormat:@"%i", x];
}
检索:
int nodeToRetrieveTag = 2; // or whatever
SKNode* nodeToRetrieve = [self.scene childNodeWithName:[NSString stringWithFormat:@"%i", nodeToRetrieveTag]];
【讨论】:
我通常使用属性名:
SKNode *myNode = [[SKNode alloc]init];
myNode.name = @"uniqueName";
[self addChild:myNode];
在 SKScene 中,要恢复节点你可以这样做:
[self childNodeWithName:@"uniqueName"]; // self is SKScene
如果出于某种原因您不想使用名称,您始终可以将一个 SKNode 子类化并添加您的个人唯一标识符:
MySpriteNode.h
@interface MySpriteNode : SKSpriteNode
@property (nonatomic, strong) NSString *personalIdentifier;
@end
和 MySpriteNode.m
#import "MySpriteNode.h"
@implementation MySpriteNode
@end
使用第二个选项,您可以:
for (MySpriteNode *sprite in [self children]) personalIdentifier
{
if ([sprite.personalIdentifier isEqualToString:@"something"])
{
//do something
}
}
编辑 1:
尝试遵循这些教程,我真的认为它们很棒。我从他们那里学到了很多:
【讨论】:
【讨论】: