我有建设 CCNode
它有财产:
在 .h 文件中
@property (nonatomic) int testProperty;
在 .mm 文件中添加标签并设置属性
[[GB2ShapeCache sharedShapeCache] addShapesWithFile:@"Objects.plist"];
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
batchNode = [CCSpriteBatchNode batchNodeWithFile:@"sprite.png"];
[self addChild:batchNode];
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"sprite.plist"];
sprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"%@.png", type]];
[batchNode addChild:sprite];
[sprite setTag:2]; //SET TAG
[self setTestProperty:10]; //SET PROPERTY
...
也是 ContactListener 类它可以正常使用此代码,并且我正在按标签检测正文:
#import "ContactListener.h"
#import "Building.h"
void ContactListener::BeginContact(b2Contact* contact)
{
b2Body* bodyA = contact->GetFixtureA()->GetBody();
b2Body* bodyB = contact->GetFixtureB()->GetBody();
Building *buildA = (Building *)bodyA->GetUserData();
Building *buildB = (Building *)bodyB->GetUserData();
if (buildA.tag == 2) {
NSLog(@"Collision with building");
}
}
问题:我不明白如何从 ContactListener 获取属性
我试图得到它:
if (buildA.tag == 2) {
NSLog(@"Collision with building");
NSLog(@"testProperty == %i", buildA.testProperty);
}
但是buildA.testProperty不起作用,并得到错误
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCSprite testProperty]: unrecognized selector sent to instance 0x434af0'
如果你能解释一下我如何从这个班级获得财产。谢谢。