cocos2d iphone - body null when accessed from another method -
i have contact listener handles contact between 2 box2d bodies. accessing bodies contacter in helloworldlayer since box2d recommends contacting bodies should saved , changes implemented after timestep. please see code below:
contacter.h:
#import "ccphysicssprite.h" @interface contacter : ccphysicssprite { } @property(nonatomic, assign) nsmutablearray* arrayofbodies; @property(nonatomic, assign) ccphysicssprite* spritetodestroy; -(void)physicsspritescontact:(ccphysicssprite*)onephysicssprite othersprite:(ccphysicssprite*)twophysicssprite; @end contacter.mm:
#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) && (secondtag == 101 )) || ((firsttag == 101) && (secondtag == 90))) { if (tag1 == 90) { [self destroybodies:onephysicssprite.b2body];// adds body array destroyed spritetodestroy = onephysicssprite; // taking note of sprite destroyed } else if (tag2 == 90) { [self destroybodies:twophysicssprite.b2body]; spritetodestroy = twophysicssprite; } } }
the following method within helloworldlayer.mm called in update method:
-(void)removedestroyedbodiesandsprites { bodycontact = [contacter node]; if ([bodycontact arrayofbodies]) { (nsvalue* bodyvalue in [bodycontact arrayofbodies]) { b2body *removebody; removebody = (b2body*)[bodyvalue pointervalue]; world->destroybody(removebody); removebody = null; [self removechild:[bodycontact spritetodestroy]]; } } } there contact sprite not removed , body not destroyed in removedestroyedbodiesandsprites. after testing cclog found loop not satisfied meaning arrayofbodies null. surprising since contact established. appreciate assistance.
updated
below contact listener:
testcontactlistener.h:
#import <foundation/foundation.h> #import "cocos2d.h" #import "box2d.h" #import "gameobjects.h" #import "contacter.h" class testcontactlistener : public b2contactlistener { public: contacter* contacter; void begincontact(b2contact* contact); }; testcontactlistener.mm:
#import "testcontactlistener.h" void testcontactlistener:: begincontact(b2contact *contact) { contacter = [contacter node]; b2fixture *fixturea = contact->getfixturea(); b2fixture *fixtureb = contact->getfixtureb(); b2body *fixtureabody = fixturea->getbody(); b2body *fixturebbody = fixtureb->getbody(); ccphysicssprite* physicssprite = (ccphysicssprite*)fixtureabody->getuserdata(); ccphysicssprite* physicssprite2 = (ccphysicssprite*)fixturebbody->getuserdata(); [contacter physicsspritescontact:physicssprite othersprite:physicssprite2]; }
move destroybodies method init method, called once:
_arrayofbodies = [[nsmutablearray alloc] init];
Comments
Post a Comment