Note that there are some explanatory texts on larger screens.

plurals
  1. PObody null when accessed from another method
    primarykey
    data
    text
    <p>I have a contact listener that handles contact between two box2d bodies. I am accessing the bodies from the Contacter in the HelloWorldLayer since box2d recommends that contacting bodies should be saved and changes implemented after the timestep. Please see the code below:</p> <p>Contacter.h:</p> <pre><code>#import "CCPhysicsSprite.h" @interface Contacter : CCPhysicsSprite { } @property(nonatomic, assign) NSMutableArray* arrayOfBodies; @property(nonatomic, assign) CCPhysicsSprite* spriteToDestroy; -(void)physicsSpritesContact:(CCPhysicsSprite*)onePhysicsSprite otherSprite:(CCPhysicsSprite*)twoPhysicsSprite; @end </code></pre> <p>Contacter.mm:</p> <pre><code>#import "Contacter.h" #import "Box2D.h" @implementation Contacter @synthesize arrayOfBodies = _arrayOfBodies; @synthesize spriteToDestroy = _spriteToDestroy; -(void)destroyBodies:(b2Body*)body { _arrayOfBodies = [[NSMutableArray alloc] init]; NSValue *bodyValue = [NSValue valueWithPointer:body]; [_arrayOfBodies addObject:bodyValue]; } -(void)physicsSpritesContact:(CCPhysicsSprite*)onePhysicsSprite otherSprite: (CCPhysicsSprite*)twoPhysicsSprite; { int firstTag = onePhysicsSprite.tag; int secondTag = twoPhysicsSprite.tag; if (((firstTag == 90) &amp;&amp; (secondTag == 101 )) || ((firstTag == 101) &amp;&amp; (secondTag == 90))) { if (tag1 == 90) { [self destroyBodies:onePhysicsSprite.b2Body];// adds body to array to be destroyed spriteToDestroy = onePhysicsSprite; // taking note of sprite to be destroyed } else if (tag2 == 90) { [self destroyBodies:twoPhysicsSprite.b2Body]; spriteToDestroy = twoPhysicsSprite; } } </code></pre> <p>}</p> <p>The following method within HelloWorldLayer.mm is called in the update method:</p> <pre><code>-(void)removeDestroyedBodiesAndSprites { bodyContact = [Contacter node]; if ([bodyContact arrayOfBodies]) { for (NSValue* bodyValue in [bodyContact arrayOfBodies]) { b2Body *removeBody; removeBody = (b2Body*)[bodyValue pointerValue]; world-&gt;DestroyBody(removeBody); removeBody = NULL; [self removeChild:[bodyContact spriteToDestroy]]; } } } </code></pre> <p>There is contact but the sprite is not removed and body is not destroyed in removeDestroyedBodiesAndSprites. After testing with a CCLOG I found that the for loop was not satisfied meaning that the arrayOfBodies could be null. Which is surprising since the contact was established. I would appreciate your assistance.</p> <p><strong>UPDATED</strong></p> <p>Below is the contact listener:</p> <p>TestContactListener.h:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "cocos2d.h" #import "Box2D.h" #import "GameObjects.h" #import "Contacter.h" class TestContactListener : public b2ContactListener { public: Contacter* contacter; void BeginContact(b2Contact* contact); }; </code></pre> <p>TestContactListener.mm:</p> <pre><code>#import "TestContactListener.h" void TestContactListener:: BeginContact(b2Contact *contact) { contacter = [Contacter node]; b2Fixture *fixtureA = contact-&gt;GetFixtureA(); b2Fixture *fixtureB = contact-&gt;GetFixtureB(); b2Body *fixtureABody = fixtureA-&gt;GetBody(); b2Body *fixtureBBody = fixtureB-&gt;GetBody(); CCPhysicsSprite* physicsSprite = (CCPhysicsSprite*)fixtureABody-&gt;GetUserData(); CCPhysicsSprite* physicsSprite2 = (CCPhysicsSprite*)fixtureBBody-&gt;GetUserData(); [contacter physicsSpritesContact:physicsSprite otherSprite:physicsSprite2]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload