in code i've used spritea = (__bridge ccsprite *) bodya->getuserdata(); //where spritea ccsprite , bodya b2body. use whatever sprite linked bodya. problem is, how do other way around? have sprite , want find out b2body linked it. how do this?
edit
i don't know wether set right or not, i'm trying remove b2bodies (and sprites) in array called row4 once there no more blue objects (objects in row4bluearray) here part of code in tick method:
//find sprite b2bodies else if (bodya->getuserdata() != null && bodyb->getuserdata() != null) { spritea = (__bridge ccsprite *) bodya->getuserdata(); spriteb = (__bridge ccsprite *) bodyb->getuserdata(); contactpositionx = spritea.position.x; contactpositiony = spriteb.position.y; //if sprite member of row 4 (tag 400) if (spritea.tag == 400 && spriteb.tag == 8) { [self createexplosionblue]; [self addtilescore]; [self removechild:spritea cleanup:yes]; [self removechild:spriteb cleanup:yes]; nslog(@"row 4 count: %d",row4bluearray.count); //remove object array [row4bluearray removelastobject]; todestroy.insert(bodya); todestroy.insert(bodyb); [self unschedule:@selector(tick:)]; ballcount = 0; //if array empty, remove objects array (row4) if (row4bluearray.count == 0) { (b2body * b = _world->getbodylist(); b != null; b = b->getnext()) { box2dsprite * sprite = (__bridge box2dsprite*) b->getuserdata(); b2body * spritebody = sprite.body; //not sure how remove bodies in array (row4)` } } }
one simple way cache body property of class.
you extend ccsprite class class called box2dsprite. in box2dsprite, include property b2body * used create physics sprite. can store reference world if think may need it.
@interface box2dsprite : ccsprite { b2body * body; b2world * world; } // end ivars @property (readwrite) b2body * body; @property (readwrite) b2world * world; also remember rename .m file .mm file
edit
in game loop, obtain box2dsprite need cast box2dsprite.
for (b2body * b = world->getbodylist(); b != null; b = b->getnext()) { box2dsprite * sprite = (__bridge box2dsprite*) b->getuserdata(); b2body * spritebody = sprite.body; // stuff body here } you need ensure whenever create sprite physics body, creating box2dsprite.
hope helps
Comments
Post a Comment