0

I'm implementing a Contact Listener with Box2d which is a C++ .mm file.

Whenever two bubble's collide, I want to know so I can execute something. Here is my code:

void ContactListener::BeginContact(b2Contact* contact)
{

    b2Body* bodyA = contact->GetFixtureA()->GetBody();
    b2Body* bodyB = contact->GetFixtureB()->GetBody();
    if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL)
    {
        BubbleSprite* bNodeA = (BubbleSprite*)bodyA->GetUserData();
        BubbleSprite* bNodeB = (BubbleSprite*)bodyB->GetUserData();

        BOOL oneIsBeingTouched;
        if(bNodeA.isDrag == YES || bNodeB.isDrag == YES) oneIsBeingTouched = YES;
...

BubbleSprite's have the property BOOL isDrag which indicates whether or not they are currently being dragged by a user. The problem I get is:

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'-[CCSprite isDrag]: unrecognized selector sent to instance 0x1ed504a0'

BubbleSprite is a subclass of CCSprite. isDrag is properly declared and synthesized in its file. Does anyone have any ideas on what's going on? Thanks

user339946
  • 5,961
  • 9
  • 52
  • 97
  • here http://stackoverflow.com/questions/10968425/fxlabel-crash-with-uilabel-error is another similiar problem today, fixed by restarting xcode and reimporting certain files to project. – Rok Jarc Jun 10 '12 at 14:31
  • You mention that this code is for "whenever two bubble's collide", but you are not checking that the collision is between two bubbles. If every body in the world is a bubble then this would still be ok, but otherwise you cannot assume that the collision is between two bubbles. – iforce2d Jun 10 '12 at 20:45

2 Answers2

1

I suggest using a getter, Create your property like so.

@property (nonatomic, assign, getter=isDrag) BOOL dragging;

and of course, @synthesize it. Now you can use -setDragging: and [bNodeA isDrag]. Nothing serious, but should make for easier to understand code.

skram
  • 5,314
  • 1
  • 22
  • 26
0

Try using

 if (bNodeA.drag == YES) ...

or

if ([bNodeA isDrag] == YES) ...
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
  • Same error. Unrecognized selector on CCSprite. It should be run on a BubbleSprite... – user339946 Jun 10 '12 at 11:12
  • Are you adding `drag` property yourself with subclassing? Neither [`CCSprite`](http://www.cocos2d-iphone.org/api-ref/0.99.0/interface_c_c_sprite.html) nor [`CCNode`](http://www.cocos2d-iphone.org/api-ref/0.99.0/interface_c_c_node.html) have it. – Rok Jarc Jun 10 '12 at 11:16
  • Yes, isDrag is the property name. As I mentioned, BubbleSprite is a subclass of CCSprite. – user339946 Jun 10 '12 at 11:21
  • if you named the property `isDrag` it will be accessible via `[node isIsDrag]`. check http://stackoverflow.com/questions/4864239/using-a-bool-property – Rok Jarc Jun 10 '12 at 11:23
  • Ah, interesting, didn't know about the "is" prepending. Anyway, I renamed it to just "drag" for simplicity. However, I still get the same message. Its asking a CCSprite for the drag property when it should be asking a BubbleSprite and then throwing the unrecognized selector error. Not sure why...Thanks – user339946 Jun 10 '12 at 11:46
  • do you by any chance call `[super isDrag]` in `BubbleSprite` implementation? Can you post declaration and implementation of this property? – Rok Jarc Jun 10 '12 at 11:51