cocos2d iphone - What role does contentSize play in positioning and anchor points? -
i've read countless posts , articles on anchor points , affect on positioning, , yet still find myself in situations things make absolutely no sense.
example:
@interface displaytest : ccsprite; @property (nonatomic, strong) ccsprite *body; @end @implementation displaytest -(instancetype)init { if (self = [super initwithfile:@"test.png" rect:cgrectzero]) { self.position = ccp(384, 512); [self addchild:self.body]; self.body.scalex = 384; self.anchorpoint = ccp(0.5f, 0.5f); } return self; } -(ccsprite *)body { if (!_body) { cgrect rect = cgrectmake(1, 1, 1, 16); _body = [[ccsprite alloc] initwithtexture:self.texture rect:rect]; _body.anchorpoint = ccp(0.5f, 0.5f); } return _body; }
i 384px wide, 16px high rectangle centered in screen.
excellent..
now, let's want touch detection on object.. let's going have many other ccsprite children, , want perform touch detection on top level object instead of performing on children...
i can't because:
(lldb) expr self.contentsize (cgsize) $17 = (width = 0, height = 0)
ok, so... no big deal right?
(lldb) expr self.contentsize = self.body.boundingbox.size (cgsize) $19 = (width = 384, height = 16)
now rectangle half way off screen. what??? wtf?? why?!?!?!?!?!
so put should be, have either 1 of following:
self.anchorpoint = ccp(0.0, 0.0);
or
self.position = ccp(384 + (self.contentsize.width/2), 512 + (self.contentsize.height / 2));
although setting anchor point (0.0, 0.0) "fixes" putting in center of screen, makes touch detection impossible because, when call converttonodespace:, if have touched sprite on left side of it, value negative , negative value cannot found in rectangle. see post wrote years ago this: touch detection of ccnode anchored 0,0 center?
so, in order make work, have had go positioning things offset of 1/2 it's content size, , don't understand why have that. anchor point of 0.5, 0.5 supposed mean child sprites placed relative position (centered), assuming child sprites have anchor point of 0.5, 0.5-- in case does.
can please explain me?
Comments
Post a Comment