我遇到的问题是 CCLabelBMFont 标签的位置和组成该标签的 CCSprite 字符之一似乎不同,所以我无法管理触摸事件......
为了更详细地测试这个问题,我尝试了这个:
-(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);
for (CCSprite *character in [label children])
{
if (CGRectContainsPoint([character boundingBox], touchLocation))
{
[character setColor:ccRED];
}
}
}
无论我触摸标签上的哪个字母,它都没有变红。
我认为组成 CCLabelBMFont 的精灵的位置和标签本身不同的原因是当我插入时:
NSLog(@"character position: x:%f y: %f", character.position.x, character.position.y);
在 ccTouchesBegan 触摸处理中,精灵的位置被列出在屏幕的左下角。
所以也许我错过了一些简单的东西,但有没有办法将精灵位置转换为与标签相同的位置,以识别标签字母上的触摸?
提前致谢。