I have Building CCNode
It’s have property:
in .h file
@property (nonatomic) int testProperty;
in .mm file add tag and set property
[[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
...
Also is the ContactListener class
It’s work correctly with this code and i’m detecting body by tag fine:
#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");
}
}
PROBLEM:
I don understand how get property from ContactListener
I tried to get it so:
if (buildA.tag == 2) {
NSLog(@"Collision with building");
NSLog(@"testProperty == %i", buildA.testProperty);
}
But buildA.testProperty not work, and get error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCSprite testProperty]: unrecognized selector sent to instance 0x434af0'
If your can explain my please how get property from this class. Thanks.
When I create body, I set into user data sprite.
In the Building.h
solved the problem.