我刚刚学习Cocos2d和Objective-C,并且在触摸检测方面存在一些问题。触摸检测问题
我在HelloWorldLayer
和一些draggableSprites(NSMutableArray)有几个精灵。其中两个可拖放的精灵位于另一个之上(最下面的一个更大)。
然后,我有一些代码,震动触摸精灵:
- (void)selectSpriteForTouch:(CGPoint)touchLocation {
CCSprite *touchSprite = nil;
for (CCSprite *sprite in dragableSprites) {
if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {
touchSprite = sprite;
break;
}
}
if (touchSprite != selSprite) {
[selSprite stopAllActions];
[selSprite runAction:[CCRotateTo actionWithDuration:0.1 angle:0]];
CCRotateTo * rotLeft = [CCRotateBy actionWithDuration:0.1 angle:-1.0];
CCRotateTo * rotCenter = [CCRotateBy actionWithDuration:0.1 angle:0.0];
CCRotateTo * rotRight = [CCRotateBy actionWithDuration:0.1 angle:1.0];
CCSequence * rotSeq = [CCSequence actions:rotLeft, rotCenter, rotRight, rotCenter, nil];
[touchSprite runAction:[CCRepeatForever actionWithAction:rotSeq]];
selSprite = touchSprite;
}
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
[self selectSpriteForTouch:touchLocation];
return TRUE;
}
但是当我接触的顶部或底部的精灵,只有底部有一个惊天(这是第一次加入INIT)。
我认为我需要替换代码CGRectContainsPoint(sprite.boundingBox, touchLocation)
,但我只是不知道如何。
我花了几个晚上的论坛,但我仍然无法理解如何让顶级精灵抖动......
谢谢!删除Break语句有助于顶级精灵。 – OnOff 2012-01-15 21:10:20
很高兴能帮到你! – 2012-01-15 21:13:15