我想在我的六邊形小精靈周圍創建觸摸區域(CGMutablePathRefs)。我的目標是創建名稱爲hexTouchArea1,hexTouchArea2等的鍵,所以我開始將它們存儲在NSMutableDictionary中。但我無法在其中存儲CGMutablePathRefs。我將如何解決這個問題?在NSMutableDictionary中存儲CGMutablePathRef?
for (int i = 0; i < hexCount; i++) {
hexTouchAreas = [[NSMutableDictionary alloc] init];
CGPoint touchAreaOrigin = ccp(location.x -22, location.y-40);
NSString *touchAreaKey = [NSString stringWithFormat:@"hexTouchArea%d",i];
CGMutablePathRef hexTouchArea = CGPathCreateMutable();
hexTouchArea = [self drawHexagonTouchArea:touchAreaOrigin];
[hexTouchAreas setObject:hexTouchArea forKey:touchAreaKey];
NSLog(@"the touchareas are %@", hexTouchAreas);
}
drawHexagonTouchArea返回CGMutablePathRef:
-(CGMutablePathRef) drawHexagonTouchArea:(CGPoint)origin
{
CGMutablePathRef path = CGPathCreateMutable();
CGPoint newloc = CGPointMake(origin.x, origin.y);
CGPathMoveToPoint(path, NULL, newloc.x, newloc.y);
CGPathAddLineToPoint(path, NULL, newloc.x -22,newloc.y + 38);
CGPathAddLineToPoint(path, NULL, newloc.x + 0, newloc.y + 76);
CGPathAddLineToPoint(path, NULL, newloc.x + 46, newloc.y + 76);
CGPathAddLineToPoint(path, NULL, newloc.x +66,newloc.y + 40);
CGPathAddLineToPoint(path, NULL, newloc.x +44, newloc.y + 0);
CGPathCloseSubpath(path);
return path;
}
和:我如何分配這些觸摸區域CCSprites所以如果精靈旋轉時,他們不單獨移動?
首先返回,你應該知道,你在漏水上述兩個代碼CGMutablePathRefs。如果使用'CGPathCreateMutable()',則需要將其與'CGPathRelease()'匹配,否則路徑將永遠不會被釋放。另外,不需要在循環內的'hexTouchArea'初始化中創建路徑,因爲您只需使用'-drawHexagonTouchArea:'的結果覆蓋它即可。 –
[CGMutablePathRef到NSMutableArray]的可能的重複(http://stackoverflow.com/questions/4133063/cgmutablepathref-to-nsmutablearray) –
雖然鏈接的問題處理NSMutableArray而不是NSMutableDictionary,但同樣的原則適用於此處。 –