[英]Adding custom properties to SKNode and using them with 'enumerateChildNodesWithName:usingBlock:'
What I wish to do is: to extend SKNode with a custom property, a plain int, and later use it with enumerateChildNodesWithName:usingBlock:
. 我想做的是:使用自定义属性(普通int)扩展SKNode,然后将其与
enumerateChildNodesWithName:usingBlock:
。
Since I am a total beginner I failed to understand tutorials of how to do this with the 'userData' property of SKNode. 由于我是一个初学者,因此我无法理解如何使用SKNode的'userData'属性执行此操作的教程。 I also attempted to create a subclass of NSObject/SKNode and adding the property there.
我还尝试创建NSObject / SKNode的子类并在其中添加属性。 But then I failed to make it work with
enumerateChildNodesWithName:usingBlock:
. 但是后来我无法使其与
enumerateChildNodesWithName:usingBlock:
。
Extending SKNode seemed more straight forward to me. 对我来说,扩展SKNode似乎更直接。
the compiler does not complain about the following code: 编译器不会抱怨以下代码:
SKNode_weight.h SKNode_weight.h
@interface SKNode ()
@property int weight;
@end
MyScene.h MyScene.h
#import "SKNode_weight.h"
MyScene.m MyScene.m
#import "MyScene.h"
#import "SKNode_weight.h"
@implementation MyScene
-(void) spawnBall {
SKNode *ballNode = [SKNode node];
ballNode.weight = 10; // fixed value for simplicity
ballNode.name = @"ball";
[self addChild:ballNode];
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
[self spawnBall];
}
return self;
}
-(void) sumWeight{
[self enumerateChildNodesWithName:@"ball" usingBlock:^(SKNode *node, BOOL *stop) {
NSLog(@"%i", node.weight);
}];
}
@end
the build is successful, but I get the following error: 构建成功,但是出现以下错误:
-[SKNode setWeight:]: unrecognised selector sent to instance 0x96328f0
I hope somebody can give a an example of a quick and painless solution. 我希望有人可以举出一个快速而轻松的解决方案的例子。 Thank you, Flo
谢谢你,弗洛
It is not possible to add members and properties to an existing class via a category — only methods. 无法通过类别(仅方法)将成员和属性添加到现有类。
http://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Category.html http://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Category.html
Best way would be to subclass SKNode and add weight property to the subclass. 最好的方法是将SKNode子类化,并将weight属性添加到子类中。
SKNodeWeighted.h
@interface SKNodeWeighted : SKNode
@property int weight;
@end
Then use it like you would use the category with slight differences. 然后像使用稍有差异的类别一样使用它。
#import "MyScene.h"
#import "SKNodeWeighted.h"
@implementation MyScene
-(void) spawnBall {
SKNodeWeighted *ballNode = [SKNodeWeighted node];
ballNode.weight = 10; // fixed value for simplicity
ballNode.name = @"ball";
[self addChild:ballNode];
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
[self spawnBall];
}
return self;
}
-(void) sumWeight{
[self enumerateChildNodesWithName:@"ball" usingBlock:^(SKNode *node, BOOL *stop) {
if ([node isKindOfClass:[SKNodeWeighted class]]) {
NSLog(@"%i", ((SKNodeWeighted*)node).weight);
}
}];
}
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.