objective c - cocos2d collision detection BOOL Flag -
i trying head round collision detection, here game details: have character can move freely around screen , fire bullets enemies generated off screen , ‘hero’ can shoot them (when bullets , enemies created stored in arrays , removed @ collision) if enemy makes contact hero nothing happens, life removed on contact , believe done via collision detection. using
(cgrectintersectsrect([hero boundingbox], [enemy boundingbox]))
but not collisions detected , 3 detected. believe caused multiple collisions detected objects pass through each other. have tried use bool flag don’t believe doing correctly, code:
.h bool collision; .m -(void)update:(cctime)deltatime { if (collision == no) { if (cgrectintersectsrect([hero boundingbox], [enemy boundingbox])) { cclog(@”collision detected!!!!!!!!!!!!!!!!”); collision = yes; } } }
is best way deal collision detection , if how implement bool flag?
you set boolean on yes when collide , no when stop colliding know next time when it's new collision.
if (cgrectintersectsrect([hero boundingbox], [enemy boundingbox])) { cclog(@”collision detected!!!!!!!!!!!!!!!!”); if(!collision) { // remove life } collision = yes; } else { collision = no; }
Comments
Post a Comment