cocos2d iphone - Registering touches on CCSprite characters of a CCLabelBMFont label -
the problem encounter positions of cclabelbmfont label , 1 of ccsprite characters composing label seem different cannot manage touch events...
to test more in details issue tried :
-(id)init { if ((self = [super init]) { cclabelbmfont *label = [cclabelbmfont labelwithstring:"welcome" fntfile:@"arial.fnt"]; cgsize size = [[ccdirector shareddirector] winsize]; label.position = ccp(size.width/2, size.height/2); [self addchild: self.label]; self.istouchenabled = yes; } return self; } -(void)cctouchesbegan:(nsset *)touches withevent:(uievent *)event { uitouch *touch = [touches anyobject]; cgpoint touchlocation = [touch locationinview:[touch view]]; touchlocation = [[ccdirector shareddirector] converttogl:touchlocation]; nslog(@"point touched: x:%f, y:%f", touchlocation.x, touchlocation.y); (ccsprite *character in [label children]) { if (cgrectcontainspoint([character boundingbox], touchlocation)) { [character setcolor:ccred]; } } }
and whatever letter of label touch didn't turn red should.
and reason why think position of sprites composing cclabelbmfont , label different when insert :
nslog(@"character position: x:%f y: %f", character.position.x, character.position.y);
in cctouchesbegan touch handling, position of sprites listed on lower left corner of screen.
so maybe i'm missing simple there way convert sprites position @ same place label enable recognition of touches on letters of label?
thanks in advance.
the position of child nodes relative parent. have use sprite's converttonodespace or converttoworldspace methods convert position accordingly. in case have use converttonodespace touchlocation input each sprite before using test bounding box collisions.
this should fix it:
for (ccsprite *character in [label children]) { cgpoint touchlocationnodespace = [character converttonodespace:touchlocation]; if (cgrectcontainspoint([character boundingbox], touchlocationnodespace)) { [character setcolor:ccred]; } }
Comments
Post a Comment