我想在我的六边形精灵周围创建触控区域(CGMutablePathRefs)。我的目标是创建名称为hexTouchArea1,hexTouchArea2等的键,因此我开始将它们存储在NSMutableDictionary中。但是我不能在其中存储CGMutablePathRefs。我该如何解决这个问题?
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;
}
AND:如何将这些触摸区域分配给CCSprites,这样如果精灵旋转,它们不会单独移动?
答案 0 :(得分:1)
您可以使用NSValue封装CGMutablePathRef,然后将其添加到字典中:
NSValue *pathAsValue = [NSValue valueWithPointer:hexTouchArea];
[dictionary setObject:pathAsValue forKey:yourKeyHere];
如果需要,请使用:
NSValue *myPathAsValue = [dictionary objectForKey:yourKeyHere];
CGMutablePathRef pathRef = [myPathAsValue pointerValue];
答案 1 :(得分:1)
变化:
[hexTouchAreas setObject:hexTouchArea forKey:touchAreaKey];
为:
[hexTouchAreas setObject:(id)hexTouchArea forKey:touchAreaKey];
CGPath
和CGMutablePath
只是不透明的CFType
对象类型,可以将它们(通过强制转换为id
)添加到任何免费的Cocoa容器类中桥接到他们的CoreFoundation对应部分。
并观察从drawHexagonTouchArea